Passed
Branch master (145c86)
by Whallysson
01:22
created

Imagick::toImage()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 22
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace CodeBlog\ToWebP\Convert\Converters;
4
5
use CodeBlog\ToWebP\AbstractConverter;
6
use Exception;
7
8
/**
9
 * Class Imagick
10
 *
11
 * Converts an image to WebP via ImageMagick
12
 *
13
 * @author Whallysson Avelino <https://github.com/whallysson>
14
 * @package CodeBlog\ToWebP\Convert\Converters
15
 */
16
class Imagick extends AbstractConverter
17
{
18
    /**
19
     * @return bool|mixed
20
     * @throws Exception
21
     */
22
    public function checkRequirements()
23
    {
24
        if (!extension_loaded('imagick')) {
25
            throw new Exception('Required iMagick extension is not available.');
26
        }
27
28
        if (!class_exists('Imagick')) {
29
            throw new Exception('iMagick is installed but cannot handle source file.');
30
        }
31
32
        return true;
33
    }
34
35
    /**
36
     * @return bool|mixed
37
     */
38
    public function convert()
39
    {
40
        try {
41
            $this->checkRequirements();
42
            $im = new \Imagick($this->source);
43
            // Throws an exception if iMagick does not support WebP conversion
44
            if (!in_array('WEBP', $im->queryFormats())) {
45
                throw new Exception('iMagick was compiled without WebP support.');
46
            }
47
            $im->setImageFormat('WEBP');
48
        } catch (Exception $e) {
49
            return false; // TODO: `throw` custom \Exception $e & handle it smoothly on top-level.
50
        }
51
52
        // Apply losless compression for PNG images
53
        switch ($this->extension) {
54
            case 'png':
55
                $im->setOption('webp:lossless', 'true');
56
                break;
57
            default:
58
                break;
59
        }
60
61
        return $this->toImage($im);
62
    }
63
64
    /**
65
     * @param $image
66
     * @return bool
67
     */
68
    private function toImage($image)
69
    {
70
        /*
71
         * More about iMagick's WebP options:
72
         * http://www.imagemagick.org/script/webp.php
73
         * https://stackoverflow.com/questions/37711492/imagemagick-specific-webp-calls-in-php
74
         */
75
        $image->setOption('webp:method', '6');
76
        $image->setOption('webp:low-memory', 'true');
77
        $image->setImageCompressionQuality($this->quality);
78
79
        // TODO: Check out other iMagick methods, see http://php.net/manual/de/imagick.writeimage.php#114714
80
        // 1. file_put_contents($destination, $im)
81
        // 2. $im->writeImage($destination)
82
        $wh = fopen($this->destination, 'wb');
83
84
        $success = $wh !== false ? $image->writeImageFile($wh) : false;
85
86
        // TODO: Check whether Imagick::writeImageFile returns false if conversion was unsuccessful
87
        if (!$success) { return false; }
88
89
        return true;
90
    }
91
}
92