| Total Complexity | 48 |
| Total Lines | 292 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Resizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Resizer, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 24 | class Resizer |
||
| 25 | { |
||
| 26 | public string $sourceFile = ''; |
||
| 27 | public string $endFile = ''; |
||
| 28 | public int $maxWidth = 0; |
||
| 29 | public int $maxHeight = 0; |
||
| 30 | public string $imageMimetype = ''; |
||
| 31 | public int $jpgQuality = 90; |
||
| 32 | public int $mergeType = 0; |
||
| 33 | public int $mergePos = 0; |
||
| 34 | public int $degrees = 0; |
||
| 35 | public string $error = ''; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * resize image if size exceed given width/height |
||
| 39 | * @return string|bool |
||
| 40 | */ |
||
| 41 | public function resizeImage() |
||
| 42 | { |
||
| 43 | // check file extension |
||
| 44 | switch ($this->imageMimetype) { |
||
| 45 | case 'image/png': |
||
| 46 | $img = \imagecreatefrompng($this->sourceFile); |
||
| 47 | break; |
||
| 48 | case 'image/jpeg': |
||
| 49 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
| 50 | if (!$img) { |
||
| 51 | $img = \imagecreatefromstring(\file_get_contents($this->sourceFile)); |
||
| 52 | } |
||
| 53 | break; |
||
| 54 | case 'image/gif': |
||
| 55 | $img = \imagecreatefromgif($this->sourceFile); |
||
| 56 | break; |
||
| 57 | default: |
||
| 58 | return 'Unsupported format'; |
||
| 59 | } |
||
| 60 | $width = \imagesx($img); |
||
| 61 | $height = \imagesy($img); |
||
| 62 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
| 63 | // recalc image size based on this->maxWidth/this->maxHeight |
||
| 64 | if ($width > $height) { |
||
| 65 | if ($width < $this->maxWidth) { |
||
| 66 | $newWidth = $width; |
||
| 67 | } else { |
||
| 68 | $newWidth = $this->maxWidth; |
||
| 69 | $divisor = $width / $newWidth; |
||
| 70 | $newHeight = (int)\floor($height / $divisor); |
||
| 71 | } |
||
| 72 | } elseif ($height < $this->maxHeight) { |
||
| 73 | $newHeight = (int)$height; |
||
| 74 | } else { |
||
| 75 | $newHeight = $this->maxHeight; |
||
| 76 | $divisor = $height / $newHeight; |
||
| 77 | $newWidth = (int)\floor($width / $divisor); |
||
| 78 | } |
||
| 79 | // Create a new temporary image. |
||
| 80 | $tmpimg = \imagecreatetruecolor($newWidth, $newHeight); |
||
| 81 | \imagealphablending($tmpimg, false); |
||
| 82 | \imagesavealpha($tmpimg, true); |
||
| 83 | // Copy and resize old image into new image. |
||
| 84 | \imagecopyresampled( |
||
| 85 | $tmpimg, |
||
| 86 | $img, |
||
| 87 | 0, |
||
| 88 | 0, |
||
| 89 | 0, |
||
| 90 | 0, |
||
| 91 | $newWidth, |
||
| 92 | $newHeight, |
||
| 93 | $width, |
||
| 94 | $height |
||
| 95 | ); |
||
| 96 | \unlink($this->endFile); |
||
| 97 | //compressing the file |
||
| 98 | switch ($this->imageMimetype) { |
||
| 99 | case 'image/png': |
||
| 100 | \imagepng($tmpimg, $this->endFile, 0); |
||
| 101 | break; |
||
| 102 | case 'image/jpeg': |
||
| 103 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
| 104 | break; |
||
| 105 | case 'image/gif': |
||
| 106 | \imagegif($tmpimg, $this->endFile); |
||
| 107 | break; |
||
| 108 | } |
||
| 109 | // release the memory |
||
| 110 | \imagedestroy($tmpimg); |
||
| 111 | } else { |
||
| 112 | return 'copy'; |
||
| 113 | } |
||
| 114 | \imagedestroy($img); |
||
| 115 | |||
| 116 | return true; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return bool|string |
||
| 121 | */ |
||
| 122 | public function resizeAndCrop() |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function mergeImage(): void |
||
| 214 | { |
||
| 215 | $dest = \imagecreatefromjpeg($this->endFile); |
||
| 216 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
| 217 | if (4 === $this->mergeType) { |
||
| 218 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
| 219 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 220 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
| 221 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 222 | switch ($this->mergePos) { |
||
| 223 | case 1: |
||
| 224 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 225 | break; |
||
| 226 | case 2: |
||
| 227 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 228 | break; |
||
| 229 | case 3: |
||
| 230 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 231 | break; |
||
| 232 | case 4: |
||
| 233 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 234 | break; |
||
| 235 | } |
||
| 236 | } |
||
| 237 | if (6 === $this->mergeType) { |
||
| 238 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
| 239 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 240 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
| 241 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
| 242 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 243 | switch ($this->mergePos) { |
||
| 244 | case 1: |
||
| 245 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 246 | break; |
||
| 247 | case 2: |
||
| 248 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
| 249 | break; |
||
| 250 | case 3: |
||
| 251 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 252 | break; |
||
| 253 | case 4: |
||
| 254 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 255 | break; |
||
| 256 | case 5: |
||
| 257 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
| 258 | break; |
||
| 259 | case 6: |
||
| 260 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 261 | break; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | \imagejpeg($dest, $this->endFile); |
||
| 265 | \imagedestroy($src); |
||
| 266 | \imagedestroy($dest); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return bool|string |
||
| 271 | */ |
||
| 272 | public function rotateImage() |
||
| 316 | } |
||
| 317 | } |
||
| 318 |