| Conditions | 17 |
| Paths | 48 |
| Total Lines | 84 |
| Code Lines | 47 |
| 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 |
||
| 32 | public static function generateThumbnail( |
||
| 33 | string $imageStr, |
||
| 34 | string $target, |
||
| 35 | int $maxWidth, |
||
| 36 | int $maxHeight, |
||
| 37 | bool $crop = false, |
||
| 38 | string $resizeMode = WebThumbnailer::RESAMPLE |
||
| 39 | ): void { |
||
| 40 | if (!touch($target)) { |
||
| 41 | throw new ImageConvertException('Target file is not writable.'); |
||
| 42 | } |
||
| 43 | |||
| 44 | if ($crop && ($maxWidth == 0 || $maxHeight == 0)) { |
||
| 45 | throw new ImageConvertException('Both width and height must be provided for cropping'); |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($maxWidth < 0 || $maxHeight < 0) { |
||
| 49 | throw new ImageConvertException('Height and width must be zero or positive'); |
||
| 50 | } |
||
| 51 | |||
| 52 | $sourceImg = static::imageCreateFromString($imageStr); |
||
| 53 | if ($sourceImg === false) { |
||
| 54 | throw new NotAnImageException(); |
||
| 55 | } |
||
| 56 | |||
| 57 | $originalWidth = imagesx($sourceImg); |
||
| 58 | $originalHeight = imagesy($sourceImg); |
||
| 59 | if ($maxWidth > $originalWidth) { |
||
| 60 | $maxWidth = $originalWidth; |
||
| 61 | } |
||
| 62 | if ($maxHeight > $originalHeight) { |
||
| 63 | $maxHeight = $originalHeight; |
||
| 64 | } |
||
| 65 | |||
| 66 | list($finalWidth, $finalHeight) = static::calcNewSize( |
||
| 67 | $originalWidth, |
||
| 68 | $originalHeight, |
||
| 69 | $maxWidth, |
||
| 70 | $maxHeight, |
||
| 71 | $crop |
||
| 72 | ); |
||
| 73 | |||
| 74 | $targetImg = imagecreatetruecolor($finalWidth, $finalHeight); |
||
| 75 | if ($targetImg === false) { |
||
| 76 | throw new ImageConvertException('Could not generate the thumbnail from source image.'); |
||
| 77 | } |
||
| 78 | |||
| 79 | $resizeFunction = $resizeMode === WebThumbnailer::RESIZE ? 'imagecopyresized' : 'imagecopyresampled'; |
||
| 80 | if ( |
||
| 81 | !$resizeFunction( |
||
| 82 | $targetImg, |
||
| 83 | $sourceImg, |
||
| 84 | 0, |
||
| 85 | 0, |
||
| 86 | 0, |
||
| 87 | 0, |
||
| 88 | $finalWidth, |
||
| 89 | $finalHeight, |
||
| 90 | $originalWidth, |
||
| 91 | $originalHeight |
||
| 92 | ) |
||
| 93 | ) { |
||
| 94 | static::imageDestroy($sourceImg); |
||
| 95 | static::imageDestroy($targetImg); |
||
|
|
|||
| 96 | |||
| 97 | throw new ImageConvertException('Could not generate the thumbnail from source image.'); |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($crop) { |
||
| 101 | $targetImg = imagecrop($targetImg, [ |
||
| 102 | 'x' => $finalWidth >= $finalHeight ? (int) floor(($finalWidth - $maxWidth) / 2) : 0, |
||
| 103 | 'y' => $finalHeight <= $finalWidth ? (int) floor(($finalHeight - $maxHeight) / 2) : 0, |
||
| 104 | 'width' => $maxWidth, |
||
| 105 | 'height' => $maxHeight |
||
| 106 | ]); |
||
| 107 | } |
||
| 108 | |||
| 109 | if (false === $targetImg) { |
||
| 110 | throw new ImageConvertException('Could not generate the thumbnail.'); |
||
| 111 | } |
||
| 112 | |||
| 113 | imagedestroy($sourceImg); |
||
| 114 | imagejpeg($targetImg, $target); |
||
| 115 | imagedestroy($targetImg); |
||
| 116 | } |
||
| 223 |