Completed
Branch master (0235df)
by Bjørn
13:03 queued 09:47
created

Imagick::doActualConvert()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 76
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 8
nop 0
dl 0
loc 76
ccs 0
cts 13
cp 0
crap 20
rs 9.8333
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace WebPConvert\Convert\Converters;
4
5
use WebPConvert\Convert\BaseConverters\AbstractConverter;
6
use WebPConvert\Convert\Exceptions\ConversionFailedException;
7
use WebPConvert\Convert\Exceptions\ConversionFailed\FileSystemProblems\CreateDestinationFileException;
8
use WebPConvert\Convert\Exceptions\ConversionFailed\ConverterNotOperational\SystemRequirementsNotMetException;
9
10
//use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\TargetNotFoundException;
11
12
/**
13
 * Convert images to webp using Imagick extension.
14
 *
15
 * @package    WebPConvert
16
 * @author     Bjørn Rosell <[email protected]>
17
 * @since      Class available since Release 2.0.0
18
 */
19
class Imagick extends AbstractConverter
20
{
21
    protected $supportsLossless = false;
22
23 1
    protected function getOptionDefinitionsExtra()
24
    {
25 1
        return [];
26
    }
27
28
    /**
29
     * Check operationality of Imagick converter.
30
     *
31
     * Note:
32
     * It may be that Gd has been compiled without jpeg support or png support.
33
     * We do not check for this here, as the converter could still be used for the other.
34
     *
35
     * @throws SystemRequirementsNotMetException  if system requirements are not met
36
     */
37 1
    public function checkOperationality()
38
    {
39 1
        if (!extension_loaded('imagick')) {
40
            throw new SystemRequirementsNotMetException('Required iMagick extension is not available.');
41
        }
42
43 1
        if (!class_exists('\\Imagick')) {
44
            throw new SystemRequirementsNotMetException(
45
                'iMagick is installed, but not correctly. The class Imagick is not available'
46
            );
47
        }
48
49 1
        $im = new \Imagick();
50
51 1
        if (!in_array('WEBP', $im->queryFormats())) {
52 1
            throw new SystemRequirementsNotMetException('iMagick was compiled without WebP support.');
53
        }
54
    }
55
56
    /**
57
     * Check if specific file is convertable with current converter / converter settings.
58
     *
59
     * @throws SystemRequirementsNotMetException  if Imagick does not support image type
60
     */
61
    public function checkConvertability()
62
    {
63
        $im = new \Imagick();
64
        $mimeType = $this->getMimeTypeOfSource();
65
        switch ($mimeType) {
66
            case 'image/png':
67
                if (!in_array('PNG', $im->queryFormats())) {
68
                    throw new SystemRequirementsNotMetException(
69
                        'Imagick has been compiled without PNG support and can therefore not convert this PNG image.'
70
                    );
71
                }
72
                break;
73
            case 'image/jpeg':
74
                if (!in_array('JPEG', $im->queryFormats())) {
75
                    throw new SystemRequirementsNotMetException(
76
                        'Imagick has been compiled without Jpeg support and can therefore not convert this Jpeg image.'
77
                    );
78
                }
79
                break;
80
        }
81
    }
82
83
    /**
84
     *
85
     * It may also throw an ImagickException if imagick throws an exception
86
     * @throws CreateDestinationFileException if imageblob could not be saved to file
87
     */
88
    protected function doActualConvert()
89
    {
90
        $options = $this->options;
91
92
        // This might throw - we let it!
93
        $im = new \Imagick($this->source);
94
95
        //$im = new \Imagick();
96
        //$im->readImage($this->source);
97
98
        $im->setImageFormat('WEBP');
99
100
        /*
101
         * More about iMagick's WebP options:
102
         * http://www.imagemagick.org/script/webp.php
103
         * https://developers.google.com/speed/webp/docs/cwebp
104
         * https://stackoverflow.com/questions/37711492/imagemagick-specific-webp-calls-in-php
105
         */
106
107
        // TODO: We could easily support all webp options with a loop
108
109
        /*
110
        After using getImageBlob() to write image, the following setOption() calls
111
        makes settings makes imagick fail. So can't use those. But its a small price
112
        to get a converter that actually makes great quality conversions.
113
114
        $im->setOption('webp:method', strval($options['method']));
115
        $im->setOption('webp:low-memory', strval($options['low-memory']));
116
        $im->setOption('webp:lossless', strval($options['lossless']));
117
        */
118
119
        if ($options['metadata'] == 'none') {
120
            // Strip metadata and profiles
121
            $im->stripImage();
122
        }
123
124
        if ($this->isQualityDetectionRequiredButFailing()) {
125
            // Luckily imagick is a big boy, and automatically converts with same quality as
126
            // source, when the quality isn't set.
127
            // So we simply do not set quality.
128
            // This actually kills the max-quality functionality. But I deem that this is more important
129
            // because setting image quality to something higher than source generates bigger files,
130
            // but gets you no extra quality. When failing to limit quality, you at least get something
131
            // out of it
132
            $this->logLn('Converting without setting quality in order to achieve auto quality');
133
        } else {
134
            $im->setImageCompressionQuality($this->getCalculatedQuality());
135
        }
136
137
        // https://stackoverflow.com/questions/29171248/php-imagick-jpeg-optimization
138
        // setImageFormat
139
140
        // TODO: Read up on
141
        // https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/
142
        // https://github.com/nwtn/php-respimg
143
144
        // TODO:
145
        // Should we set alpha channel for PNG's like suggested here:
146
        // https://gauntface.com/blog/2014/09/02/webp-support-with-imagemagick-and-php ??
147
        // It seems that alpha channel works without... (at least I see completely transparerent pixels)
148
149
        // TODO: Check out other iMagick methods, see http://php.net/manual/de/imagick.writeimage.php#114714
150
        // 1. file_put_contents($destination, $im)
151
        // 2. $im->writeImage($destination)
152
153
        // We used to use writeImageFile() method. But we now use getImageBlob(). See issue #43
154
        //$success = $im->writeImageFile(fopen($destination, 'wb'));
155
156
157
        // This might throw - we let it!
158
        $imageBlob = $im->getImageBlob();
159
160
        $success = file_put_contents($this->destination, $imageBlob);
161
162
        if (!$success) {
163
            throw new CreateDestinationFileException('Failed writing file');
164
        }
165
166
        // Btw: check out processWebp() method here:
167
        // https://github.com/Intervention/image/blob/master/src/Intervention/Image/Imagick/Encoder.php
168
    }
169
}
170