IconExtractor::getBetterQuality()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 0
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