| Conditions | 4 |
| Paths | 8 |
| Total Lines | 76 |
| Code Lines | 13 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 79 | protected function doActualConvert() |
||
| 80 | { |
||
| 81 | $options = $this->options; |
||
| 82 | |||
| 83 | // This might throw - we let it! |
||
| 84 | $im = new \Imagick($this->source); |
||
| 85 | |||
| 86 | //$im = new \Imagick(); |
||
| 87 | //$im->readImage($this->source); |
||
| 88 | |||
| 89 | $im->setImageFormat('WEBP'); |
||
| 90 | |||
| 91 | /* |
||
| 92 | * More about iMagick's WebP options: |
||
| 93 | * http://www.imagemagick.org/script/webp.php |
||
| 94 | * https://developers.google.com/speed/webp/docs/cwebp |
||
| 95 | * https://stackoverflow.com/questions/37711492/imagemagick-specific-webp-calls-in-php |
||
| 96 | */ |
||
| 97 | |||
| 98 | // TODO: We could easily support all webp options with a loop |
||
| 99 | |||
| 100 | /* |
||
| 101 | After using getImageBlob() to write image, the following setOption() calls |
||
| 102 | makes settings makes imagick fail. So can't use those. But its a small price |
||
| 103 | to get a converter that actually makes great quality conversions. |
||
| 104 | |||
| 105 | $im->setOption('webp:method', strval($options['method'])); |
||
| 106 | $im->setOption('webp:low-memory', strval($options['low-memory'])); |
||
| 107 | $im->setOption('webp:lossless', strval($options['lossless'])); |
||
| 108 | */ |
||
| 109 | |||
| 110 | if ($options['metadata'] == 'none') { |
||
| 111 | // Strip metadata and profiles |
||
| 112 | $im->stripImage(); |
||
| 113 | } |
||
| 114 | |||
| 115 | if ($this->isQualityDetectionRequiredButFailing()) { |
||
| 116 | // Luckily imagick is a big boy, and automatically converts with same quality as |
||
| 117 | // source, when the quality isn't set. |
||
| 118 | // So we simply do not set quality. |
||
| 119 | // This actually kills the max-quality functionality. But I deem that this is more important |
||
| 120 | // because setting image quality to something higher than source generates bigger files, |
||
| 121 | // but gets you no extra quality. When failing to limit quality, you at least get something |
||
| 122 | // out of it |
||
| 123 | $this->logLn('Converting without setting quality in order to achieve auto quality'); |
||
| 124 | } else { |
||
| 125 | $im->setImageCompressionQuality($this->getCalculatedQuality()); |
||
| 126 | } |
||
| 127 | |||
| 128 | // https://stackoverflow.com/questions/29171248/php-imagick-jpeg-optimization |
||
| 129 | // setImageFormat |
||
| 130 | |||
| 131 | // TODO: Read up on |
||
| 132 | // https://www.smashingmagazine.com/2015/06/efficient-image-resizing-with-imagemagick/ |
||
| 133 | // https://github.com/nwtn/php-respimg |
||
| 134 | |||
| 135 | // TODO: |
||
| 136 | // Should we set alpha channel for PNG's like suggested here: |
||
| 137 | // https://gauntface.com/blog/2014/09/02/webp-support-with-imagemagick-and-php ?? |
||
| 138 | // It seems that alpha channel works without... (at least I see completely transparerent pixels) |
||
| 139 | |||
| 140 | // TODO: Check out other iMagick methods, see http://php.net/manual/de/imagick.writeimage.php#114714 |
||
| 141 | // 1. file_put_contents($destination, $im) |
||
| 142 | // 2. $im->writeImage($destination) |
||
| 143 | |||
| 144 | // We used to use writeImageFile() method. But we now use getImageBlob(). See issue #43 |
||
| 145 | //$success = $im->writeImageFile(fopen($destination, 'wb')); |
||
| 146 | |||
| 147 | |||
| 148 | // This might throw - we let it! |
||
| 149 | $imageBlob = $im->getImageBlob(); |
||
| 150 | |||
| 151 | $success = file_put_contents($this->destination, $imageBlob); |
||
| 152 | |||
| 153 | if (!$success) { |
||
| 154 | throw new CreateDestinationFileException('Failed writing file'); |
||
| 155 | } |
||
| 161 |