| Total Complexity | 48 |
| Total Lines | 270 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 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() |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @return bool|string |
||
| 117 | */ |
||
| 118 | public function resizeAndCrop() |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * |
||
| 186 | */ |
||
| 187 | public function mergeImage() |
||
| 188 | { |
||
| 189 | $dest = \imagecreatefromjpeg($this->endFile); |
||
| 190 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
| 191 | if (4 == $this->mergeType) { |
||
| 192 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
| 193 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 194 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
| 195 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 196 | switch ($this->mergePos) { |
||
| 197 | case 1: |
||
| 198 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 199 | break; |
||
| 200 | case 2: |
||
| 201 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 202 | break; |
||
| 203 | case 3: |
||
| 204 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 205 | break; |
||
| 206 | case 4: |
||
| 207 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 208 | break; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | if (6 == $this->mergeType) { |
||
| 212 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
| 213 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
| 214 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
| 215 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
| 216 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
| 217 | |||
| 218 | switch ($this->mergePos) { |
||
| 219 | case 1: |
||
| 220 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
| 221 | break; |
||
| 222 | case 2: |
||
| 223 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
| 224 | break; |
||
| 225 | case 3: |
||
| 226 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
| 227 | break; |
||
| 228 | case 4: |
||
| 229 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
| 230 | break; |
||
| 231 | case 5: |
||
| 232 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
| 233 | break; |
||
| 234 | case 6: |
||
| 235 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
| 236 | break; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | \imagejpeg($dest, $this->endFile); |
||
| 240 | |||
| 241 | \imagedestroy($src); |
||
| 242 | \imagedestroy($dest); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return bool|string |
||
| 247 | */ |
||
| 248 | public function rotateImage() |
||
| 295 | } |
||
| 296 | } |
||
| 297 |