| Conditions | 12 |
| Paths | 21 |
| Total Lines | 43 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 93 | public function saveImage(string $imgName, string $imgLoc, string $imgSavePath, int $imgMaxWidth = 0, int $imgMaxHeight = 0, bool $saveThumb = false): int |
||
| 94 | { |
||
| 95 | $cover = $this->fetchImage($imgLoc); |
||
| 96 | |||
| 97 | if ($cover === false) { |
||
| 98 | return 0; |
||
| 99 | } |
||
| 100 | |||
| 101 | // Check if we need to resize it. |
||
| 102 | if ($imgMaxWidth !== 0 && $imgMaxHeight !== 0) { |
||
| 103 | $width = $cover->width(); |
||
| 104 | $height = $cover->height(); |
||
| 105 | if ($width !== 0 || $height !== 0) { |
||
| 106 | $ratio = min($imgMaxHeight / $height, $imgMaxWidth / $width); |
||
| 107 | // New dimensions |
||
| 108 | $new_width = $ratio * $width; |
||
| 109 | $new_height = $ratio * $height; |
||
| 110 | if ($new_width < $width && $new_width > 10 && $new_height > 10) { |
||
| 111 | $cover->resize($new_width, $new_height); |
||
| 112 | |||
| 113 | if ($saveThumb) { |
||
| 114 | $cover->toJpeg(100)->save($imgSavePath.$imgName.'_thumb.jpg'); |
||
| 115 | //Optimize the thumbnail. |
||
| 116 | ImageOptimizer::optimize($imgSavePath.$imgName.'_thumb.jpg'); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | // Store it on the hard drive. |
||
| 122 | $coverPath = $imgSavePath.$imgName.'.jpg'; |
||
| 123 | try { |
||
| 124 | $cover->toJpeg(100)->save($coverPath); |
||
| 125 | //Optimize the image. |
||
| 126 | ImageOptimizer::optimize($coverPath); |
||
| 127 | } catch (NotWritableException $e) { |
||
| 128 | return 0; |
||
| 129 | } |
||
| 130 | // Check if it's on the drive. |
||
| 131 | if (! File::isReadable($coverPath)) { |
||
| 132 | return 0; |
||
| 133 | } |
||
| 134 | |||
| 135 | return 1; |
||
| 136 | } |
||
| 150 |