Total Complexity | 72 |
Total Lines | 525 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Image_Transform_Driver_GD 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 Image_Transform_Driver_GD, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | Class Image_Transform_Driver_GD extends Image_Transform |
||
49 | { |
||
50 | /** |
||
51 | * Holds the image file for manipulation |
||
52 | */ |
||
53 | public $imageHandle = ''; |
||
54 | |||
55 | /** |
||
56 | * Holds the original image file |
||
57 | */ |
||
58 | public $old_image = ''; |
||
59 | |||
60 | /** |
||
61 | * Check settings |
||
62 | * |
||
63 | * @return mixed true or or a PEAR error object on error |
||
64 | * |
||
65 | * @see PEAR::isError() |
||
66 | */ |
||
67 | public function Image_Transform_GD() |
||
68 | { |
||
69 | return; |
||
70 | } // End function Image |
||
71 | |||
72 | /** |
||
73 | * Load image |
||
74 | * |
||
75 | * @param string filename |
||
|
|||
76 | * |
||
77 | * @return mixed none or a PEAR error object on error |
||
78 | * @see PEAR::isError() |
||
79 | */ |
||
80 | public function load($image) |
||
81 | { |
||
82 | $this->uid = md5($_SERVER['REMOTE_ADDR']); |
||
83 | $this->image = $image; |
||
84 | $this->_get_image_details($image); |
||
85 | $functionName = 'ImageCreateFrom' . $this->type; |
||
86 | |||
87 | if (function_exists($functionName)) { |
||
88 | $this->imageHandle = $functionName($this->image); |
||
89 | if ('png' == $this->type) { |
||
90 | imageAlphaBlending($this->imageHandle, false); |
||
91 | imageSaveAlpha($this->imageHandle, true); |
||
92 | } |
||
93 | } |
||
94 | } // End load |
||
95 | |||
96 | /** |
||
97 | * addText |
||
98 | * |
||
99 | * @param array options Array contains options |
||
100 | * array( |
||
101 | * 'text' The string to draw |
||
102 | * 'x' Horizontal position |
||
103 | * 'y' Vertical Position |
||
104 | * 'Color' Font color |
||
105 | * 'font' Font to be used |
||
106 | * 'size' Size of the fonts in pixel |
||
107 | * 'resize_first' Tell if the image has to be resized |
||
108 | * before drawing the text |
||
109 | * ) |
||
110 | * |
||
111 | * @return none |
||
112 | * @see PEAR::isError() |
||
113 | */ |
||
114 | public function addText($params) |
||
115 | { |
||
116 | $default_params = [ |
||
117 | 'text' => 'This is Text', |
||
118 | 'x' => 10, |
||
119 | 'y' => 20, |
||
120 | 'color' => [255, 0, 0], |
||
121 | 'font' => 'Arial.ttf', |
||
122 | 'size' => '12', |
||
123 | 'angle' => 0, |
||
124 | 'resize_first' => false // Carry out the scaling of the image before annotation? Not used for GD |
||
125 | ]; |
||
126 | $params = array_merge($default_params, $params); |
||
127 | extract($params); |
||
128 | |||
129 | if (!is_array($color)) { |
||
130 | if ('#' == $color[0]) { |
||
131 | $this->colorhex2colorarray($color); |
||
132 | } else { |
||
133 | include_once('Image/Transform/Driver/ColorsDefs.php'); |
||
134 | $color = isset($colornames[$color]) ? $colornames[$color] : false; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | $c = imagecolorresolve($this->imageHandle, $color[0], $color[1], $color[2]); |
||
139 | |||
140 | if ('ttf' == substr($font, -3)) { |
||
141 | ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); |
||
142 | } else { |
||
143 | ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text); |
||
144 | } |
||
145 | return true; |
||
146 | } // End addText |
||
147 | |||
148 | /** |
||
149 | * Rotate image by the given angle |
||
150 | * Uses a fast rotation algorythm for custom angles |
||
151 | * or lines copy for multiple of 90 degrees |
||
152 | * |
||
153 | * @param int $angle Rotation angle |
||
154 | * @param array $options array( 'autoresize'=>true|false, |
||
155 | * 'color_mask'=>array(r,g,b), named color or #rrggbb |
||
156 | * ) |
||
157 | * @return mixed none or a PEAR error object on error |
||
158 | * @author Pierre-Alain Joye |
||
159 | * @see PEAR::isError() |
||
160 | */ |
||
161 | public function rotate($angle, $options = null) |
||
162 | { |
||
163 | if (function_exists('imagerotate')) { |
||
164 | $white = imagecolorallocatealpha($this->imageHandle, 255, 255, 255, 127); |
||
165 | $this->imageHandle = imagerotate($this->imageHandle, $angle, $white); |
||
166 | return true; |
||
167 | } |
||
168 | |||
169 | if (null == $options) { |
||
170 | $autoresize = true; |
||
171 | $color_mask = [105, 255, 255]; |
||
172 | } else { |
||
173 | extract($options); |
||
174 | } |
||
175 | |||
176 | while ($angle <= -45) { |
||
177 | $angle += 360; |
||
178 | } |
||
179 | while ($angle > 270) { |
||
180 | $angle -= 360; |
||
181 | } |
||
182 | |||
183 | $t = deg2rad($angle); |
||
184 | |||
185 | if (!is_array($color_mask)) { |
||
186 | if ('#' == $color[0]) { |
||
187 | $this->colorhex2colorarray($color_mask); |
||
188 | } else { |
||
189 | include_once('Image/Transform/Driver/ColorDefs.php'); |
||
190 | $color = isset($colornames[$color_mask]) ? $colornames[$color_mask] : false; |
||
191 | } |
||
192 | } |
||
193 | |||
194 | // Do not round it, too much lost of quality |
||
195 | $cosT = cos($t); |
||
196 | $sinT = sin($t); |
||
197 | |||
198 | $img =& $this->imageHandle; |
||
199 | |||
200 | $width = $max_x = $this->img_x; |
||
201 | $height = $max_y = $this->img_y; |
||
202 | $min_y = 0; |
||
203 | $min_x = 0; |
||
204 | |||
205 | $x1 = round($max_x / 2, 0); |
||
206 | $y1 = round($max_y / 2, 0); |
||
207 | |||
208 | if ($autoresize) { |
||
209 | $t = abs($t); |
||
210 | $a = round($angle, 0); |
||
211 | switch ((int)($angle)) { |
||
212 | case 0: |
||
213 | $width2 = $width; |
||
214 | $height2 = $height; |
||
215 | break; |
||
216 | case 90: |
||
217 | $width2 = $height; |
||
218 | $height2 = $width; |
||
219 | break; |
||
220 | case 180: |
||
221 | $width2 = $width; |
||
222 | $height2 = $height; |
||
223 | break; |
||
224 | case 270: |
||
225 | $width2 = $height; |
||
226 | $height2 = $width; |
||
227 | break; |
||
228 | default: |
||
229 | $width2 = (int)(abs(sin($t) * $height + cos($t) * $width)); |
||
230 | $height2 = (int)(abs(cos($t) * $height + sin($t) * $width)); |
||
231 | } |
||
232 | |||
233 | $width2 -= $width2 % 2; |
||
234 | $height2 -= $height2 % 2; |
||
235 | |||
236 | $d_width = abs($width - $width2); |
||
237 | $d_height = abs($height - $height2); |
||
238 | $x_offset = $d_width / 2; |
||
239 | $y_offset = $d_height / 2; |
||
240 | $min_x2 = -abs($x_offset); |
||
241 | $min_y2 = -abs($y_offset); |
||
242 | $max_x2 = $width2; |
||
243 | $max_y2 = $height2; |
||
244 | } |
||
245 | |||
246 | $img2 = @$this->newImgPreserveAlpha(imagecreateTrueColor($width2, $height2)); |
||
247 | |||
248 | if (!is_resource($img2)) { |
||
249 | return false;/*PEAR::raiseError('Cannot create buffer for the rotataion.', |
||
250 | null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/ |
||
251 | } |
||
252 | |||
253 | $this->img_x = $width2; |
||
254 | $this->img_y = $height2; |
||
255 | |||
256 | imagepalettecopy($img2, $img); |
||
257 | |||
258 | $mask = imagecolorallocatealpha($img2, $color_mask[0], $color_mask[1], $color_mask[2], 127); |
||
259 | // use simple lines copy for axes angles |
||
260 | switch ((int)($angle)) { |
||
261 | case 0: |
||
262 | imagefill($img2, 0, 0, $mask); |
||
263 | for ($y = 0; $y < $max_y; $y++) { |
||
264 | for ($x = $min_x; $x < $max_x; $x++) { |
||
265 | $c = @imagecolorat($img, $x, $y); |
||
266 | imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); |
||
267 | } |
||
268 | } |
||
269 | break; |
||
270 | case 90: |
||
271 | imagefill($img2, 0, 0, $mask); |
||
272 | for ($x = $min_x; $x < $max_x; $x++) { |
||
273 | for ($y = $min_y; $y < $max_y; $y++) { |
||
274 | $c = imagecolorat($img, $x, $y); |
||
275 | imagesetpixel($img2, $max_y - $y - 1, $x, $c); |
||
276 | } |
||
277 | } |
||
278 | break; |
||
279 | case 180: |
||
280 | imagefill($img2, 0, 0, $mask); |
||
281 | for ($y = 0; $y < $max_y; $y++) { |
||
282 | for ($x = $min_x; $x < $max_x; $x++) { |
||
283 | $c = @imagecolorat($img, $x, $y); |
||
284 | imagesetpixel($img2, $max_x2 - $x - 1, $max_y2 - $y - 1, $c); |
||
285 | } |
||
286 | } |
||
287 | break; |
||
288 | case 270: |
||
289 | imagefill($img2, 0, 0, $mask); |
||
290 | for ($y = 0; $y < $max_y; $y++) { |
||
291 | for ($x = $max_x; $x >= $min_x; $x--) { |
||
292 | $c = @imagecolorat($img, $x, $y); |
||
293 | imagesetpixel($img2, $y, $max_x - $x - 1, $c); |
||
294 | } |
||
295 | } |
||
296 | break; |
||
297 | // simple reverse rotation algo |
||
298 | default: |
||
299 | $i = 0; |
||
300 | for ($y = $min_y2; $y < $max_y2; $y++) { |
||
301 | // Algebra :) |
||
302 | $x2 = round((($min_x2 - $x1) * $cosT) + (($y - $y1) * $sinT + $x1), 0); |
||
303 | $y2 = round((($y - $y1) * $cosT - ($min_x2 - $x1) * $sinT + $y1), 0); |
||
304 | |||
305 | for ($x = $min_x2; $x < $max_x2; $x++) { |
||
306 | // Check if we are out of original bounces, if we are |
||
307 | // use the default color mask |
||
308 | if ($x2 >= 0 && $x2 < $max_x && $y2 >= 0 && $y2 < $max_y) { |
||
309 | $c = imagecolorat($img, $x2, $y2); |
||
310 | } else { |
||
311 | $c = $mask; |
||
312 | } |
||
313 | imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c); |
||
314 | |||
315 | // round verboten! |
||
316 | $x2 += $cosT; |
||
317 | $y2 -= $sinT; |
||
318 | } |
||
319 | } |
||
320 | break; |
||
321 | } |
||
322 | $this->old_image = $this->imageHandle; |
||
323 | $this->imageHandle = $img2; |
||
324 | return true; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Resize Action |
||
329 | * |
||
330 | * For GD 2.01+ the new copyresampled function is used |
||
331 | * It uses a bicubic interpolation algorithm to get far |
||
332 | * better result. |
||
333 | * |
||
334 | * @param int $new_x new width |
||
335 | * @param int $new_y new height |
||
336 | * |
||
337 | * @return true on success or pear error |
||
338 | * @see PEAR::isError() |
||
339 | */ |
||
340 | public function _resize($new_x, $new_y) |
||
341 | { |
||
342 | if (true === $this->resized) { |
||
343 | return false; /*PEAR::raiseError('You have already resized the image without saving it. Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/ |
||
344 | } |
||
345 | if (function_exists('ImageCreateTrueColor')) { |
||
346 | $new_img = $this->newImgPreserveAlpha(ImageCreateTrueColor($new_x, $new_y)); |
||
347 | } else { |
||
348 | $new_img = ImageCreate($new_x, $new_y); |
||
349 | } |
||
350 | |||
351 | if (function_exists('ImageCopyResampled')) { |
||
352 | ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); |
||
353 | } else { |
||
354 | ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y); |
||
355 | } |
||
356 | |||
357 | $this->old_image = $this->imageHandle; |
||
358 | $this->imageHandle = $new_img; |
||
359 | $this->resized = true; |
||
360 | |||
361 | $this->new_x = $new_x; |
||
362 | $this->new_y = $new_y; |
||
363 | return true; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * Crop the image |
||
368 | * |
||
369 | * @param int $crop_x left column of the image |
||
370 | * @param int $crop_y top row of the image |
||
371 | * @param int $crop_width new cropped image width |
||
372 | * @param int $crop_height new cropped image height |
||
373 | */ |
||
374 | public function crop($new_x, $new_y, $new_width, $new_height) |
||
375 | { |
||
376 | if (function_exists('ImageCreateTrueColor')) { |
||
377 | $new_img = $this->newImgPreserveAlpha(ImageCreateTrueColor($new_width, $new_height)); |
||
378 | } else { |
||
379 | $new_img = ImageCreate($new_width, $new_height); |
||
380 | } |
||
381 | if (function_exists('ImageCopyResampled')) { |
||
382 | ImageCopyResampled($new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width, $new_height, $new_width, $new_height); |
||
383 | } else { |
||
384 | ImageCopyResized($new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width, $new_height, $new_width, $new_height); |
||
385 | } |
||
386 | $this->old_image = $this->imageHandle; |
||
387 | $this->imageHandle = $new_img; |
||
388 | $this->resized = true; |
||
389 | |||
390 | $this->new_x = $new_x; |
||
391 | $this->new_y = $new_y; |
||
392 | return true; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Flip the image horizontally or vertically |
||
397 | * |
||
398 | * @param bool $horizontal true if horizontal flip, vertical otherwise |
||
399 | */ |
||
400 | public function flip($horizontal) |
||
401 | { |
||
402 | if (!$horizontal) { |
||
403 | $this->rotate(180); |
||
404 | } |
||
405 | |||
406 | $width = imagesx($this->imageHandle); |
||
407 | $height = imagesy($this->imageHandle); |
||
408 | |||
409 | for ($j = 0; $j < $height; $j++) { |
||
410 | $left = 0; |
||
411 | $right = $width - 1; |
||
412 | |||
413 | while ($left < $right) { |
||
414 | //echo " j:".$j." l:".$left." r:".$right."\n<br>"; |
||
415 | $t = imagecolorat($this->imageHandle, $left, $j); |
||
416 | imagesetpixel($this->imageHandle, $left, $j, imagecolorat($this->imageHandle, $right, $j)); |
||
417 | imagesetpixel($this->imageHandle, $right, $j, $t); |
||
418 | $left++; |
||
419 | $right--; |
||
420 | } |
||
421 | } |
||
422 | |||
423 | return true; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Adjust the image gamma |
||
428 | * |
||
429 | * @param float $outputgamma |
||
430 | * |
||
431 | * @return none |
||
432 | */ |
||
433 | public function gamma($outputgamma = 1.0) |
||
434 | { |
||
435 | ImageGammaCorrect($this->imageHandle, 1.0, $outputgamma); |
||
436 | } |
||
437 | |||
438 | public function paletteToTrueColorWithTransparency() |
||
439 | { |
||
440 | $oldImg = $this->imageHandle; |
||
441 | $newImg = $this->newImgPreserveAlpha(imagecreatetruecolor($this->img_x, $this->img_y)); |
||
442 | imagecopy($newImg, $oldImg, 0, 0, 0, 0, $this->img_x, $this->img_y); |
||
443 | |||
444 | $this->imageHandle = $newImg; |
||
445 | } |
||
446 | |||
447 | public function newImgPreserveAlpha($newImg) |
||
448 | { |
||
449 | if ('jpeg' == $this->type) { |
||
450 | return $newImg; |
||
451 | } |
||
452 | |||
453 | // Turn off transparency blending (temporarily) |
||
454 | imagealphablending($newImg, false); |
||
455 | |||
456 | // Create a new transparent color for image |
||
457 | if ($transparent = imagecolortransparent($this->imageHandle) >= 0) { |
||
458 | if (imageistruecolor($this->imageHandle)) { |
||
459 | $red = ($transparent & 0xFF0000) >> 16; |
||
460 | $green = ($transparent & 0x00FF00) >> 8; |
||
461 | $blue = ($transparent & 0x0000FF); |
||
462 | $color_values = ['red' => $red, 'green' => $green, 'blue' => $blue]; |
||
463 | } else { |
||
464 | $color_values = imagecolorsforindex($this->imageHandle, $transparent); |
||
465 | } |
||
466 | $color_values = imagecolorsforindex($this->imageHandle, $transparent); |
||
467 | $color = imagecolorallocatealpha($newImg, $color_values['red'], $color_values['green'], $color_values['blue'], 127); |
||
468 | $colort = imagecolorallocate($newImg, $color_values['red'], $color_values['green'], $color_values['blue']); |
||
469 | } else { |
||
470 | $color = imagecolorallocatealpha($newImg, 252, 2, 252, 127); |
||
471 | $colort = imagecolorallocate($newImg, 252, 2, 252); |
||
472 | } |
||
473 | imagecolortransparent($newImg, $colort); |
||
474 | |||
475 | // Completely fill the background of the new image with allocated color. |
||
476 | imagefill($newImg, 0, 0, $color); |
||
477 | |||
478 | // Restore transparency blending |
||
479 | imagesavealpha($newImg, true); |
||
480 | |||
481 | return $newImg; |
||
482 | } |
||
483 | |||
484 | public function preserveTransparencyForPalette() |
||
485 | { |
||
486 | $new_img = imagecreatetruecolor($this->img_x, $this->img_y); |
||
487 | $truecolor = imageistruecolor($this->imageHandle); |
||
488 | $transparent = imagecolorallocate($new_img, 252, 2, 252); // nasty pinkish purple that hopefully doesn't exist in the image |
||
489 | |||
490 | imagecolortransparent($new_img, $transparent); |
||
491 | for ($i = 0; $i < $this->img_y; $i++) { |
||
492 | for ($j = 0; $j < $this->img_x; $j++) { |
||
493 | $c = imagecolorat($this->imageHandle, $j, $i); |
||
494 | if ($truecolor) { |
||
495 | $a = ($c >> 24) & 0xFF; |
||
496 | $r = ($c >> 16) & 0xFF; |
||
497 | $g = ($c >> 8) & 0xFF; |
||
498 | $b = $c & 0xFF; |
||
499 | $color_values = ['red' => $r, 'green' => $g, 'blue' => $b, 'alpha' => $a]; |
||
500 | } else { |
||
501 | $color_values = imagecolorsforindex($this->imageHandle, $c); |
||
502 | } |
||
503 | if ($color_values['alpha'] >= 126) { |
||
504 | imagesetpixel($new_img, $j, $i, $transparent); |
||
505 | } else { |
||
506 | imagesetpixel($new_img, $j, $i, $c); |
||
507 | } |
||
508 | } |
||
509 | } |
||
510 | $this->imageHandle = $new_img; |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * Save the image file |
||
515 | * |
||
516 | * @param string $filename the name of the file to write to |
||
517 | * @param int $quality output DPI, default is 85 |
||
518 | * @param string $types define the output format, default |
||
519 | * is the current used format |
||
520 | * |
||
521 | * @return none |
||
522 | */ |
||
523 | public function save($filename, $type = '', $quality = 85) |
||
524 | { |
||
525 | $type = '' == $type ? $this->type : $type; |
||
526 | $functionName = 'image' . $type; |
||
527 | |||
528 | if (function_exists($functionName)) { |
||
529 | $this->old_image = $this->imageHandle; |
||
530 | if ('jpeg' == $type) { |
||
531 | $functionName($this->imageHandle, $filename, $quality); |
||
532 | } else { |
||
533 | $functionName($this->imageHandle, $filename); |
||
534 | } |
||
535 | $this->imageHandle = $this->old_image; |
||
536 | $this->resized = false; |
||
537 | } |
||
538 | } // End save |
||
539 | |||
540 | /** |
||
541 | * Display image without saving and lose changes |
||
542 | * |
||
543 | * @param string type (JPG,PNG...); |
||
544 | * @param int quality 75 |
||
545 | * |
||
546 | * @return none |
||
547 | */ |
||
548 | public function display($type = '', $quality = 75) |
||
549 | { |
||
550 | if ('' != $type) { |
||
551 | $this->type = $type; |
||
552 | } |
||
553 | $functionName = 'Image' . $this->type; |
||
554 | if (function_exists($functionName)) { |
||
555 | header('Content-type: image/' . strtolower($this->type)); |
||
556 | $functionName($this->imageHandle, '', $quality); |
||
557 | $this->imageHandle = $this->old_image; |
||
558 | $this->resized = false; |
||
559 | ImageDestroy($this->old_image); |
||
560 | $this->free(); |
||
561 | } |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Destroy image handle |
||
566 | * |
||
567 | * @return none |
||
568 | */ |
||
569 | public function free() |
||
573 | } |
||
574 | } |
||
575 | |||
576 | } // End class ImageGD |
||
577 | |||
578 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths