Total Complexity | 48 |
Total Lines | 272 |
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 |
||
26 | class Resizer |
||
27 | { |
||
28 | public $sourceFile = ''; |
||
29 | public $endFile = ''; |
||
30 | public $maxWidth = 0; |
||
31 | public $maxHeight = 0; |
||
32 | public $imageMimetype = ''; |
||
33 | public $jpgQuality = 80; |
||
34 | public $mergeType = 0; |
||
35 | public $mergePos = 0; |
||
36 | public $degrees = 0; |
||
37 | public $error = ''; |
||
38 | |||
39 | /** |
||
40 | * resize image if size exceed given width/height |
||
41 | * @return string|bool |
||
42 | */ |
||
43 | public function resizeImage() |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return bool|string |
||
120 | */ |
||
121 | public function resizeAndCrop() |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return void |
||
189 | */ |
||
190 | public function mergeImage() |
||
191 | { |
||
192 | $dest = \imagecreatefromjpeg($this->endFile); |
||
193 | $src = \imagecreatefromjpeg($this->sourceFile); |
||
194 | if (4 === $this->mergeType) { |
||
195 | $imgWidth = (int)\round($this->maxWidth / 2 - 1); |
||
196 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
197 | $posCol2 = (int)\round($this->maxWidth / 2 + 1); |
||
198 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
199 | switch ($this->mergePos) { |
||
200 | case 1: |
||
201 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
202 | break; |
||
203 | case 2: |
||
204 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
205 | break; |
||
206 | case 3: |
||
207 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
208 | break; |
||
209 | case 4: |
||
210 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
211 | break; |
||
212 | } |
||
213 | } |
||
214 | if (6 === $this->mergeType) { |
||
215 | $imgWidth = (int)\round($this->maxWidth / 3 - 1); |
||
216 | $imgHeight = (int)\round($this->maxHeight / 2 - 1); |
||
217 | $posCol2 = (int)\round($this->maxWidth / 3 + 1); |
||
218 | $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1); |
||
219 | $posRow2 = (int)\round($this->maxHeight / 2 + 1); |
||
220 | |||
221 | switch ($this->mergePos) { |
||
222 | case 1: |
||
223 | \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left |
||
224 | break; |
||
225 | case 2: |
||
226 | \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center |
||
227 | break; |
||
228 | case 3: |
||
229 | \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right |
||
230 | break; |
||
231 | case 4: |
||
232 | \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left |
||
233 | break; |
||
234 | case 5: |
||
235 | \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center |
||
236 | break; |
||
237 | case 6: |
||
238 | \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right |
||
239 | break; |
||
240 | } |
||
241 | } |
||
242 | \imagejpeg($dest, $this->endFile); |
||
243 | |||
244 | \imagedestroy($src); |
||
245 | \imagedestroy($dest); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return bool|string |
||
250 | */ |
||
251 | public function rotateImage() |
||
298 | } |
||
299 | } |
||
300 |