| Total Complexity | 48 |
| Total Lines | 305 |
| 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); |
||
| 25 | class Resizer |
||
| 26 | { |
||
| 27 | public $sourceFile = ''; |
||
| 28 | public $endFile = ''; |
||
| 29 | public $maxWidth = 0; |
||
| 30 | public $maxHeight = 0; |
||
| 31 | public $imageMimetype = ''; |
||
| 32 | public $jpgQuality = 90; |
||
| 33 | public $mergeType = 0; |
||
| 34 | public $mergePos = 0; |
||
| 35 | public $degrees = 0; |
||
| 36 | public $error = ''; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * resize image if size exceed given width/height |
||
| 40 | * @return string|bool |
||
| 41 | */ |
||
| 42 | public function resizeImage() |
||
| 43 | { |
||
| 44 | // check file extension |
||
| 45 | switch ($this->imageMimetype) { |
||
| 46 | case 'image/png': |
||
| 47 | $img = \imagecreatefrompng($this->sourceFile); |
||
| 48 | break; |
||
| 49 | case 'image/jpeg': |
||
| 50 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
| 51 | if (!$img) { |
||
|
|
|||
| 52 | $img = \imagecreatefromstring(file_get_contents($this->sourceFile)); |
||
| 53 | } |
||
| 54 | break; |
||
| 55 | case 'image/gif': |
||
| 56 | $img = \imagecreatefromgif($this->sourceFile); |
||
| 57 | break; |
||
| 58 | default: |
||
| 59 | return 'Unsupported format'; |
||
| 60 | } |
||
| 61 | |||
| 62 | $width = \imagesx($img); |
||
| 63 | $height = \imagesy($img); |
||
| 64 | |||
| 65 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
| 66 | // recalc image size based on this->maxWidth/this->maxHeight |
||
| 67 | if ($width > $height) { |
||
| 68 | if ($width < $this->maxWidth) { |
||
| 69 | $new_width = $width; |
||
| 70 | } else { |
||
| 71 | $new_width = $this->maxWidth; |
||
| 72 | $divisor = $width / $new_width; |
||
| 73 | $newHeight = (int)\floor($height / $divisor); |
||
| 74 | } |
||
| 75 | } elseif ($height < $this->maxHeight) { |
||
| 76 | $newHeight = (int)$height; |
||
| 77 | } else { |
||
| 78 | $newHeight = $this->maxHeight; |
||
| 79 | $divisor = $height / $newHeight; |
||
| 80 | $new_width = (int)\floor($width / $divisor); |
||
| 81 | } |
||
| 82 | |||
| 83 | // Create a new temporary image. |
||
| 84 | $tmpimg = \imagecreatetruecolor($new_width, $newHeight); |
||
| 85 | imagealphablending($tmpimg, false); |
||
| 86 | imagesavealpha($tmpimg, true); |
||
| 87 | |||
| 88 | // Copy and resize old image into new image. |
||
| 89 | \imagecopyresampled( |
||
| 90 | $tmpimg, |
||
| 91 | $img, |
||
| 92 | 0, |
||
| 93 | 0, |
||
| 94 | 0, |
||
| 95 | 0, |
||
| 96 | $new_width, |
||
| 97 | $newHeight, |
||
| 98 | $width, |
||
| 99 | $height |
||
| 100 | ); |
||
| 101 | |||
| 102 | \unlink($this->endFile); |
||
| 103 | //compressing the file |
||
| 104 | switch ($this->imageMimetype) { |
||
| 105 | case 'image/png': |
||
| 106 | \imagepng($tmpimg, $this->endFile, 0); |
||
| 107 | break; |
||
| 108 | case 'image/jpeg': |
||
| 109 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
| 110 | break; |
||
| 111 | case 'image/gif': |
||
| 112 | \imagegif($tmpimg, $this->endFile); |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | |||
| 116 | // release the memory |
||
| 117 | \imagedestroy($tmpimg); |
||
| 118 | } else { |
||
| 119 | return 'copy'; |
||
| 120 | } |
||
| 121 | \imagedestroy($img); |
||
| 122 | |||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return bool|string |
||
| 128 | */ |
||
| 129 | public function resizeAndCrop() |
||
| 220 | } |
||
| 221 | |||
| 222 | public function mergeImage() |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return bool|string |
||
| 282 | */ |
||
| 283 | public function rotateImage() |
||
| 330 | } |
||
| 331 | } |
||
| 332 |