| Conditions | 6 |
| Paths | 10 |
| Total Lines | 64 |
| Code Lines | 20 |
| 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 |
||
| 49 | protected function doActualConvert() |
||
| 50 | { |
||
| 51 | /* |
||
| 52 | $im = \Jcupitt\Vips\Image::newFromFile(__DIR__ . '/images/small.jpg'); |
||
| 53 | //$im->writeToFile(__DIR__ . '/images/small-vips.webp', ["Q" => 10]); |
||
| 54 | $im->webpsave(__DIR__ . '/images/small-vips.webp', [ |
||
| 55 | "Q" => 80, |
||
| 56 | 'near_lossless' => true |
||
| 57 | ]); |
||
| 58 | return; |
||
| 59 | */ |
||
| 60 | |||
| 61 | if (function_exists('vips_version')) { |
||
| 62 | $this->logLn('vips version: ' . vips_version()); |
||
| 63 | } |
||
| 64 | |||
| 65 | // We are currently using vips_image_new_from_file(), but we could consider |
||
| 66 | // calling vips_jpegload / vips_pngload instead |
||
| 67 | $result = vips_image_new_from_file($this->source, []); |
||
|
|
|||
| 68 | if ($result === -1) { |
||
| 69 | /*throw new ConversionFailedException( |
||
| 70 | 'Failed creating new vips image from file: ' . $this->source |
||
| 71 | );*/ |
||
| 72 | $message = vips_error_buffer(); |
||
| 73 | throw new ConversionFailedException($message); |
||
| 74 | } |
||
| 75 | |||
| 76 | if (!is_array($result)) { |
||
| 77 | throw new ConversionFailedException( |
||
| 78 | 'vips_image_new_from_file did not return an array, which we expected' |
||
| 79 | ); |
||
| 80 | } |
||
| 81 | |||
| 82 | if (count($result) != 1) { |
||
| 83 | throw new ConversionFailedException( |
||
| 84 | 'vips_image_new_from_file did not return an array of length 1 as we expected ' . |
||
| 85 | '- length was: ' . count($result) |
||
| 86 | ); |
||
| 87 | } |
||
| 88 | |||
| 89 | $im = array_shift($result); |
||
| 90 | |||
| 91 | // for some reason, vips_webpsave function is unavailable on at least one system, so we |
||
| 92 | // use vips_call instead. |
||
| 93 | |||
| 94 | // webpsave options are described here: |
||
| 95 | // https://jcupitt.github.io/libvips/API/current/VipsForeignSave.html#vips-webpsave |
||
| 96 | $result = vips_call('webpsave', $im, $this->destination, [ |
||
| 97 | "Q" => $this->getCalculatedQuality(), |
||
| 98 | //'lossless' => true, |
||
| 99 | //'lossless' => $this->options['lossless'], // boolean |
||
| 100 | //'preset' |
||
| 101 | //'smart_subsample' // boolean |
||
| 102 | |||
| 103 | // hm, when I use near_lossless, I get error: "no property named `near_lossless'" |
||
| 104 | // btw: beware that if this is used, q must be 20, 40, 60 or 80 (according to link above) |
||
| 105 | //'near_lossless' => true, // boolean |
||
| 106 | |||
| 107 | //'alpha_q' // int |
||
| 108 | 'strip' => $this->options['metadata'] == 'none' |
||
| 109 | ]); |
||
| 110 | if ($result === -1) { |
||
| 111 | $message = vips_error_buffer(); |
||
| 112 | throw new ConversionFailedException($message); |
||
| 113 | } |
||
| 116 |