| Total Complexity | 48 |
| Total Lines | 370 |
| 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 |
||
| 27 | class Resizer |
||
| 28 | { |
||
| 29 | public $sourceFile = ''; |
||
| 30 | |||
| 31 | public $endFile = ''; |
||
| 32 | |||
| 33 | public $maxWidth = 0; |
||
| 34 | |||
| 35 | public $maxHeight = 0; |
||
| 36 | |||
| 37 | public $imageMimetype = ''; |
||
| 38 | |||
| 39 | public $jpgQuality = 90; |
||
| 40 | |||
| 41 | public $mergeType = 0; |
||
| 42 | |||
| 43 | public $mergePos = 0; |
||
| 44 | |||
| 45 | public $degrees = 0; |
||
| 46 | |||
| 47 | public $error = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * resize image if size exceed given width/height |
||
| 51 | * @return string|bool |
||
| 52 | */ |
||
| 53 | |||
| 54 | public function resizeImage() |
||
| 55 | { |
||
| 56 | // check file extension |
||
| 57 | |||
| 58 | switch ($this->imageMimetype) { |
||
| 59 | case 'image/png': |
||
| 60 | $img = \imagecreatefrompng($this->sourceFile); |
||
| 61 | break; |
||
| 62 | case 'image/jpeg': |
||
| 63 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
| 64 | if (!$img) { |
||
|
|
|||
| 65 | $img = \imagecreatefromstring(file_get_contents($this->sourceFile)); |
||
| 66 | } |
||
| 67 | break; |
||
| 68 | case 'image/gif': |
||
| 69 | $img = \imagecreatefromgif($this->sourceFile); |
||
| 70 | break; |
||
| 71 | default: |
||
| 72 | return 'Unsupported format'; |
||
| 73 | } |
||
| 74 | |||
| 75 | $width = \imagesx($img); |
||
| 76 | |||
| 77 | $height = \imagesy($img); |
||
| 78 | |||
| 79 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
| 80 | // recalc image size based on this->maxWidth/this->maxHeight |
||
| 81 | |||
| 82 | if ($width > $height) { |
||
| 83 | if ($width < $this->maxWidth) { |
||
| 84 | $newWidth = $width; |
||
| 85 | } else { |
||
| 86 | $newWidth = $this->maxWidth; |
||
| 87 | |||
| 88 | $divisor = $width / $newWidth; |
||
| 89 | |||
| 90 | $newHeight = (int)\floor($height / $divisor); |
||
| 91 | } |
||
| 92 | } elseif ($height < $this->maxHeight) { |
||
| 93 | $newHeight = (int)$height; |
||
| 94 | } else { |
||
| 95 | $newHeight = $this->maxHeight; |
||
| 96 | |||
| 97 | $divisor = $height / $newHeight; |
||
| 98 | |||
| 99 | $newWidth = (int)\floor($width / $divisor); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Create a new temporary image. |
||
| 103 | |||
| 104 | $tmpimg = \imagecreatetruecolor($newWidth, $newHeight); |
||
| 105 | |||
| 106 | imagealphablending($tmpimg, false); |
||
| 107 | |||
| 108 | imagesavealpha($tmpimg, true); |
||
| 109 | |||
| 110 | // Copy and resize old image into new image. |
||
| 111 | |||
| 112 | \imagecopyresampled( |
||
| 113 | $tmpimg, |
||
| 114 | $img, |
||
| 115 | 0, |
||
| 116 | 0, |
||
| 117 | 0, |
||
| 118 | 0, |
||
| 119 | $newWidth, |
||
| 120 | $newHeight, |
||
| 121 | $width, |
||
| 122 | $height |
||
| 123 | ); |
||
| 124 | |||
| 125 | \unlink($this->endFile); |
||
| 126 | |||
| 127 | //compressing the file |
||
| 128 | |||
| 129 | switch ($this->imageMimetype) { |
||
| 130 | case 'image/png': |
||
| 131 | \imagepng($tmpimg, $this->endFile, 0); |
||
| 132 | break; |
||
| 133 | case 'image/jpeg': |
||
| 134 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
| 135 | break; |
||
| 136 | case 'image/gif': |
||
| 137 | \imagegif($tmpimg, $this->endFile); |
||
| 138 | break; |
||
| 139 | } |
||
| 140 | |||
| 141 | // release the memory |
||
| 142 | |||
| 143 | \imagedestroy($tmpimg); |
||
| 144 | } else { |
||
| 145 | return 'copy'; |
||
| 146 | } |
||
| 147 | |||
| 148 | \imagedestroy($img); |
||
| 149 | |||
| 150 | return true; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return bool|string |
||
| 155 | */ |
||
| 156 | |||
| 157 | public function resizeAndCrop() |
||
| 267 | } |
||
| 268 | |||
| 269 | public function mergeImage() |
||
| 270 | { |
||
| 271 | $dest = \imagecreatefromjpeg($this->endFile); |
||
| 272 | |||
| 273 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
| 274 | |||
| 275 | if (4 === $this->mergeType) { |
||
| 276 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
| 277 | |||
| 278 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 279 | |||
| 280 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
| 281 | |||
| 282 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 283 | |||
| 284 | switch ($this->mergePos) { |
||
| 285 | case 1: |
||
| 286 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 287 | break; |
||
| 288 | case 2: |
||
| 289 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 290 | break; |
||
| 291 | case 3: |
||
| 292 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 293 | break; |
||
| 294 | case 4: |
||
| 295 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 296 | break; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | if (6 === $this->mergeType) { |
||
| 301 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
| 302 | |||
| 303 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 304 | |||
| 305 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
| 306 | |||
| 307 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
| 308 | |||
| 309 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 310 | |||
| 311 | switch ($this->mergePos) { |
||
| 312 | case 1: |
||
| 313 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 314 | break; |
||
| 315 | case 2: |
||
| 316 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
| 317 | break; |
||
| 318 | case 3: |
||
| 319 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 320 | break; |
||
| 321 | case 4: |
||
| 322 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 323 | break; |
||
| 324 | case 5: |
||
| 325 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
| 326 | break; |
||
| 327 | case 6: |
||
| 328 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 329 | break; |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | \imagejpeg($dest, $this->endFile); |
||
| 334 | |||
| 335 | \imagedestroy($src); |
||
| 336 | |||
| 337 | \imagedestroy($dest); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return bool|string |
||
| 342 | */ |
||
| 343 | |||
| 344 | public function rotateImage() |
||
| 397 | } |
||
| 398 | } |
||
| 399 |