IconExtractor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A getBetterQuality() 0 21 3
1
<?php
2
3
namespace Imagecow\Utils;
4
5
use Imagick;
6
use Imagecow\Libs\Imagick as ImagickLib;
7
use Imagecow\Image;
8
9
/**
10
 * Simple class to extracts all images from .ico files
11
 * Requires Imagick.
12
 */
13
class IconExtractor
14
{
15
    protected $image;
16
17
    /**
18
     * Contructor.
19
     *
20
     * @param string $filename The path of ico file
21
     */
22
    public function __construct($filename)
23
    {
24
        if (!extension_loaded('imagick')) {
25
            throw new \Exception('IconExtractor needs imagick extension');
26
        }
27
28
        $image = new Imagick();
29
        $image->readImage($filename);
30
31
        $this->image = $image;
32
    }
33
34
    /**
35
     * Get the better quality image found in the icon.
36
     *
37
     * @return Image
38
     */
39
    public function getBetterQuality()
40
    {
41
        $quality = 0;
42
        $better = 0;
43
44
        foreach ($this->image as $index => $image) {
45
            $q = $image->getImageDepth() + ($image->getImageWidth() * $image->getImageHeight());
46
47
            if ($q > $quality) {
48
                $quality = $q;
49
                $better = $index;
50
            }
51
        }
52
53
        $this->image->setIteratorIndex($better);
54
55
        $better = new Image(new ImagickLib($this->image));
56
        $better->format('png');
57
58
        return $better;
59
    }
60
}
61