| Total Complexity | 48 |
| Total Lines | 320 |
| 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 |
||
| 24 | class Resizer |
||
| 25 | { |
||
| 26 | public $sourceFile = ''; |
||
| 27 | public $endFile = ''; |
||
| 28 | public $maxWidth = 0; |
||
| 29 | public $maxHeight = 0; |
||
| 30 | public $imageMimetype = ''; |
||
| 31 | public $jpgQuality = 90; |
||
| 32 | public $mergeType = 0; |
||
| 33 | public $mergePos = 0; |
||
| 34 | public $degrees = 0; |
||
| 35 | public $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 | |||
| 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 | |||
| 64 | $height = \imagesy($img); |
||
| 65 | |||
| 66 | if ($width > $this->maxWidth || $height > $this->maxHeight) { |
||
| 67 | // recalc image size based on this->maxWidth/this->maxHeight |
||
| 68 | |||
| 69 | if ($width > $height) { |
||
| 70 | if ($width < $this->maxWidth) { |
||
| 71 | $new_width = $width; |
||
| 72 | } else { |
||
| 73 | $new_width = $this->maxWidth; |
||
| 74 | |||
| 75 | $divisor = $width / $new_width; |
||
| 76 | |||
| 77 | $new_height = \floor($height / $divisor); |
||
| 78 | } |
||
| 79 | } elseif ($height < $this->maxHeight) { |
||
| 80 | $new_height = $height; |
||
| 81 | } else { |
||
| 82 | $new_height = $this->maxHeight; |
||
| 83 | |||
| 84 | $divisor = $height / $new_height; |
||
| 85 | |||
| 86 | $new_width = \floor($width / $divisor); |
||
| 87 | } |
||
| 88 | |||
| 89 | // Create a new temporary image. |
||
| 90 | |||
| 91 | $tmpimg = \imagecreatetruecolor($new_width, $new_height); |
||
| 92 | |||
| 93 | imagealphablending($tmpimg, false); |
||
| 94 | |||
| 95 | imagesavealpha($tmpimg, true); |
||
| 96 | |||
| 97 | // Copy and resize old image into new image. |
||
| 98 | |||
| 99 | \imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); |
||
| 100 | |||
| 101 | \unlink($this->endFile); |
||
| 102 | |||
| 103 | //compressing the file |
||
| 104 | |||
| 105 | switch ($this->imageMimetype) { |
||
| 106 | case 'image/png': |
||
| 107 | \imagepng($tmpimg, $this->endFile, 0); |
||
| 108 | break; |
||
| 109 | case 'image/jpeg': |
||
| 110 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
| 111 | break; |
||
| 112 | case 'image/gif': |
||
| 113 | \imagegif($tmpimg, $this->endFile); |
||
| 114 | break; |
||
| 115 | } |
||
| 116 | |||
| 117 | // release the memory |
||
| 118 | |||
| 119 | \imagedestroy($tmpimg); |
||
| 120 | } else { |
||
| 121 | return 'copy'; |
||
| 122 | } |
||
| 123 | |||
| 124 | \imagedestroy($img); |
||
| 125 | |||
| 126 | return true; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return bool|string |
||
| 131 | */ |
||
| 132 | public function resizeAndCrop() |
||
| 133 | { |
||
| 134 | // check file extension |
||
| 135 | |||
| 136 | switch ($this->imageMimetype) { |
||
| 137 | case 'image/png': |
||
| 138 | $original = \imagecreatefrompng($this->sourceFile); |
||
| 139 | break; |
||
| 140 | case 'image/jpeg': |
||
| 141 | $original = \imagecreatefromjpeg($this->sourceFile); |
||
| 142 | break; |
||
| 143 | case 'image/gif': |
||
| 144 | $original = \imagecreatefromgif($this->sourceFile); |
||
| 145 | break; |
||
| 146 | default: |
||
| 147 | return 'Unsupported format'; |
||
| 148 | } |
||
| 149 | |||
| 150 | if (!$original) { |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | |||
| 154 | // GET ORIGINAL IMAGE DIMENSIONS |
||
| 155 | |||
| 156 | [$original_w, $original_h] = \getimagesize($this->sourceFile); |
||
| 157 | |||
| 158 | // RESIZE IMAGE AND PRESERVE PROPORTIONS |
||
| 159 | |||
| 160 | $max_width_resize = $this->maxWidth; |
||
| 161 | |||
| 162 | $max_height_resize = $this->maxHeight; |
||
| 163 | |||
| 164 | if ($original_w > $original_h) { |
||
| 165 | $max_height_ratio = $this->maxHeight / $original_h; |
||
| 166 | |||
| 167 | $max_width_resize = (int)\round($original_w * $max_height_ratio); |
||
| 168 | } else { |
||
| 169 | $max_width_ratio = $this->maxWidth / $original_w; |
||
| 170 | |||
| 171 | $max_height_resize = (int)\round($original_h * $max_width_ratio); |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($max_width_resize < $this->maxWidth) { |
||
| 175 | $max_height_ratio = $this->maxWidth / $max_width_resize; |
||
| 176 | |||
| 177 | $max_height_resize = (int)\round($this->maxHeight * $max_height_ratio); |
||
| 178 | |||
| 179 | $max_width_resize = $this->maxWidth; |
||
| 180 | } |
||
| 181 | |||
| 182 | // CREATE THE PROPORTIONAL IMAGE RESOURCE |
||
| 183 | |||
| 184 | $thumb = \imagecreatetruecolor($max_width_resize, $max_height_resize); |
||
| 185 | |||
| 186 | if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $max_height_resize, $original_w, $original_h)) { |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS |
||
| 191 | |||
| 192 | $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight); |
||
| 193 | |||
| 194 | $max_width_offset = 0; |
||
| 195 | |||
| 196 | $max_height_offset = 0; |
||
| 197 | |||
| 198 | if ($this->maxWidth < $max_width_resize) { |
||
| 199 | $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2); |
||
| 200 | } else { |
||
| 201 | $max_height_offset = (int)\round(($max_height_resize - $this->maxHeight) / 2); |
||
| 202 | } |
||
| 203 | |||
| 204 | if (!\imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $max_height_resize)) { |
||
| 205 | return false; |
||
| 206 | } |
||
| 207 | |||
| 208 | // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile |
||
| 209 | |||
| 210 | if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) { |
||
| 211 | return false; |
||
| 212 | } |
||
| 213 | |||
| 214 | return true; |
||
| 215 | } |
||
| 216 | |||
| 217 | public function mergeImage() |
||
| 218 | { |
||
| 219 | $dest = \imagecreatefromjpeg($this->endFile); |
||
| 220 | |||
| 221 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
| 222 | |||
| 223 | if (4 == $this->mergeType) { |
||
| 224 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
| 225 | |||
| 226 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 227 | |||
| 228 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
| 229 | |||
| 230 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 231 | |||
| 232 | switch ($this->mergePos) { |
||
| 233 | case 1: |
||
| 234 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 235 | break; |
||
| 236 | case 2: |
||
| 237 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 238 | break; |
||
| 239 | case 3: |
||
| 240 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 241 | break; |
||
| 242 | case 4: |
||
| 243 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 244 | break; |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | if (6 == $this->mergeType) { |
||
| 249 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
| 250 | |||
| 251 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 252 | |||
| 253 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
| 254 | |||
| 255 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
| 256 | |||
| 257 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 258 | |||
| 259 | switch ($this->mergePos) { |
||
| 260 | case 1: |
||
| 261 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 262 | break; |
||
| 263 | case 2: |
||
| 264 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
| 265 | break; |
||
| 266 | case 3: |
||
| 267 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 268 | break; |
||
| 269 | case 4: |
||
| 270 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 271 | break; |
||
| 272 | case 5: |
||
| 273 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
| 274 | break; |
||
| 275 | case 6: |
||
| 276 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 277 | break; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | \imagejpeg($dest, $this->endFile); |
||
| 282 | |||
| 283 | \imagedestroy($src); |
||
| 284 | |||
| 285 | \imagedestroy($dest); |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @return bool|string |
||
| 290 | */ |
||
| 291 | public function rotateImage() |
||
| 344 | } |
||
| 345 | } |
||
| 346 |