Total Complexity | 48 |
Total Lines | 286 |
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 | public $endFile = ''; |
||
31 | public $maxWidth = 0; |
||
32 | public $maxHeight = 0; |
||
33 | public $imageMimetype = ''; |
||
34 | public $jpgQuality = 90; |
||
35 | public $mergeType = 0; |
||
36 | public $mergePos = 0; |
||
37 | public $degrees = 0; |
||
38 | public $error = ''; |
||
39 | |||
40 | /** |
||
41 | * resize image if size exceed given width/height |
||
42 | * @return string|bool |
||
43 | */ |
||
44 | public function resizeImage() |
||
45 | { |
||
46 | // check file extension |
||
47 | switch ($this->imageMimetype) { |
||
48 | case 'image/png': |
||
49 | $img = \imagecreatefrompng($this->sourceFile); |
||
50 | break; |
||
51 | case 'image/jpeg': |
||
52 | $img = \imagecreatefromjpeg($this->sourceFile); |
||
53 | if (!$img) { |
||
|
|||
54 | $img = \imagecreatefromstring(file_get_contents($this->sourceFile)); |
||
55 | } |
||
56 | break; |
||
57 | case 'image/gif': |
||
58 | $img = \imagecreatefromgif($this->sourceFile); |
||
59 | break; |
||
60 | default: |
||
61 | return 'Unsupported format'; |
||
62 | } |
||
63 | $width = \imagesx($img); |
||
64 | $height = \imagesy($img); |
||
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 | $newWidth = $width; |
||
70 | } else { |
||
71 | $newWidth = $this->maxWidth; |
||
72 | $divisor = $width / $newWidth; |
||
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 | $newWidth = (int)\floor($width / $divisor); |
||
81 | } |
||
82 | // Create a new temporary image. |
||
83 | $tmpimg = \imagecreatetruecolor($newWidth, $newHeight); |
||
84 | imagealphablending($tmpimg, false); |
||
85 | imagesavealpha($tmpimg, true); |
||
86 | // Copy and resize old image into new image. |
||
87 | \imagecopyresampled( |
||
88 | $tmpimg, |
||
89 | $img, |
||
90 | 0, |
||
91 | 0, |
||
92 | 0, |
||
93 | 0, |
||
94 | $newWidth, |
||
95 | $newHeight, |
||
96 | $width, |
||
97 | $height |
||
98 | ); |
||
99 | \unlink($this->endFile); |
||
100 | //compressing the file |
||
101 | switch ($this->imageMimetype) { |
||
102 | case 'image/png': |
||
103 | \imagepng($tmpimg, $this->endFile, 0); |
||
104 | break; |
||
105 | case 'image/jpeg': |
||
106 | \imagejpeg($tmpimg, $this->endFile, 100); |
||
107 | break; |
||
108 | case 'image/gif': |
||
109 | \imagegif($tmpimg, $this->endFile); |
||
110 | break; |
||
111 | } |
||
112 | // release the memory |
||
113 | \imagedestroy($tmpimg); |
||
114 | } else { |
||
115 | return 'copy'; |
||
116 | } |
||
117 | \imagedestroy($img); |
||
118 | return true; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @return bool|string |
||
123 | */ |
||
124 | public function resizeAndCrop() |
||
209 | } |
||
210 | |||
211 | public function mergeImage() |
||
212 | { |
||
213 | $dest = \imagecreatefromjpeg($this->endFile); |
||
214 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
215 | if (4 === $this->mergeType) { |
||
216 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
217 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
218 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
219 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
220 | switch ($this->mergePos) { |
||
221 | case 1: |
||
222 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
223 | break; |
||
224 | case 2: |
||
225 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
226 | break; |
||
227 | case 3: |
||
228 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
229 | break; |
||
230 | case 4: |
||
231 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
232 | break; |
||
233 | } |
||
234 | } |
||
235 | if (6 === $this->mergeType) { |
||
236 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
237 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
238 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
239 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
240 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
241 | switch ($this->mergePos) { |
||
242 | case 1: |
||
243 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
244 | break; |
||
245 | case 2: |
||
246 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
247 | break; |
||
248 | case 3: |
||
249 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
250 | break; |
||
251 | case 4: |
||
252 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
253 | break; |
||
254 | case 5: |
||
255 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
256 | break; |
||
257 | case 6: |
||
258 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
259 | break; |
||
260 | } |
||
261 | } |
||
262 | \imagejpeg($dest, $this->endFile); |
||
263 | \imagedestroy($src); |
||
264 | \imagedestroy($dest); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @return bool|string |
||
269 | */ |
||
270 | public function rotateImage() |
||
313 | } |
||
314 | } |
||
315 |