Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Image 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Image |
||
| 21 | { |
||
| 22 | var $_debug = false; |
||
| 23 | var $_errors = array(); |
||
| 24 | var $gdInfo = array(); //keep all information of GD extension |
||
| 25 | var $_imgOrig = null; //the hanlder of original image |
||
| 26 | var $_imgFinal = null; //the handler of final image |
||
| 27 | var $imageFile = null; |
||
| 28 | var $transparentColorRed = null; |
||
| 29 | var $transparentColorGreen = null; |
||
| 30 | var $transparentColorBlue = null; |
||
| 31 | var $chmod = 0755; |
||
| 32 | var $_imgInfoOrig = array( |
||
| 33 | 'name'=>'', |
||
| 34 | 'ext'=>'', |
||
| 35 | 'size'=>'', |
||
| 36 | 'width'=>'', |
||
| 37 | 'height'=>'', |
||
| 38 | 'type'=>'', |
||
| 39 | 'path'=>'', |
||
| 40 | ); |
||
| 41 | var $_imgInfoFinal = array( |
||
| 42 | 'name'=>'', |
||
| 43 | 'ext'=>'', |
||
| 44 | 'size'=>'', |
||
| 45 | 'width'=>'', |
||
| 46 | 'height'=>'', |
||
| 47 | 'type'=>'', |
||
| 48 | 'path'=>'', |
||
| 49 | ); |
||
| 50 | var $_imgQuality = 90; |
||
| 51 | /** |
||
| 52 | * constructor |
||
| 53 | * |
||
| 54 | * @param boolean $debug |
||
| 55 | * @return Image |
||
| 56 | */ |
||
| 57 | |||
| 58 | function __construct($debug = false) |
||
| 67 | /** |
||
| 68 | * enable to debug |
||
| 69 | * |
||
| 70 | * @param boolean $value |
||
| 71 | */ |
||
| 72 | function enableDebug($value) |
||
| 76 | /** |
||
| 77 | * check if debug enable |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | function _isDebugEnable() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * append to errors array and shown the each error when the debug turned on |
||
| 87 | * |
||
| 88 | * @param string $string |
||
| 89 | * @return void |
||
| 90 | * @access private |
||
| 91 | * @copyright this function originally come from Andy's php |
||
| 92 | */ |
||
| 93 | function _debug($value) |
||
| 101 | /** |
||
| 102 | * show erros |
||
| 103 | * |
||
| 104 | */ |
||
| 105 | function showErrors() |
||
| 115 | /** |
||
| 116 | * Load an image from the file system. |
||
| 117 | * |
||
| 118 | * @param string $filename |
||
| 119 | * @return bool |
||
| 120 | * @access public |
||
| 121 | * @copyright this function originally come from Andy's php |
||
| 122 | */ |
||
| 123 | function loadImage($filename) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Load an image from a string (eg. from a database table) |
||
| 179 | * |
||
| 180 | * @param string $string |
||
| 181 | * @return bool |
||
| 182 | * @access public |
||
| 183 | * @copyright this function originally come from Andy's php |
||
| 184 | */ |
||
| 185 | function loadImageFromString($string) |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * Save the modified image |
||
| 199 | * |
||
| 200 | * @param string $filename |
||
| 201 | * @param int $quality |
||
| 202 | * @param string $forcetype |
||
| 203 | * @return bool |
||
| 204 | * @access public |
||
| 205 | * @copyright this function originally come from Andy's php |
||
| 206 | */ |
||
| 207 | function saveImage($filename, $quality = 90, $forcetype = '') |
||
| 248 | /** |
||
| 249 | * Shows the masked image without any saving |
||
| 250 | * |
||
| 251 | * @param string $type |
||
| 252 | * @param int $quality |
||
| 253 | * @return bool |
||
| 254 | * @access public |
||
| 255 | * @copyright this function originally come from Andy's php |
||
| 256 | */ |
||
| 257 | function showImage($type = '', $quality = '') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Used for cropping image |
||
| 294 | * |
||
| 295 | * @param int $dst_x |
||
| 296 | * @param int $dst_y |
||
| 297 | * @param int $dst_w |
||
| 298 | * @param int $dst_h |
||
| 299 | * @return bool |
||
| 300 | * @access public |
||
| 301 | * @copyright this function originally come from Andy's php |
||
| 302 | */ |
||
| 303 | function crop($dst_x, $dst_y, $dst_w, $dst_h) |
||
| 338 | |||
| 339 | |||
| 340 | /** |
||
| 341 | * Resize the Image in the X and/or Y direction |
||
| 342 | * If either is 0 it will be scaled proportionally |
||
| 343 | * |
||
| 344 | * @access public |
||
| 345 | * |
||
| 346 | * @param mixed $new_x |
||
| 347 | * @param mixed $new_y |
||
| 348 | * @param boolean $constraint keep to resize the image proportionally |
||
| 349 | * @param boolean $unchangeIfsmaller keep the orignial size if the orignial smaller than the new size |
||
| 350 | * |
||
| 351 | * |
||
| 352 | * @return mixed none or PEAR_error |
||
| 353 | */ |
||
| 354 | function resize( $new_x, $new_y, $constraint= false, $unchangeIfsmaller=false) |
||
| 355 | { |
||
| 356 | if(!$this->_imgOrig) |
||
| 357 | { |
||
| 358 | $this->_debug('No image fould.'); |
||
| 359 | return false; |
||
| 360 | } |
||
| 361 | |||
| 362 | $new_x = intval($new_x); |
||
| 363 | $new_y = intval($new_y); |
||
| 364 | if($new_x <=0 || $new_y <= 0) |
||
| 365 | { |
||
| 366 | $this->_debug('either of new width or height can be zeor or less.'); |
||
| 367 | }else |
||
| 368 | { |
||
| 369 | |||
| 370 | if($constraint) |
||
| 371 | { |
||
| 372 | if($new_x < 1 && $new_y < 1) |
||
| 373 | { |
||
| 374 | $new_x = $this->_imgInfoOrig['width']; |
||
| 375 | $new_y = $this->_imgInfoOrig['height']; |
||
| 376 | }elseif($new_x < 1) |
||
| 377 | { |
||
| 378 | $new_x = floor($new_y / $this->_imgInfoOrig['height'] * $this->_imgInfoOrig['width']); |
||
| 379 | |||
| 380 | }elseif($new_y < 1) |
||
| 381 | { |
||
| 382 | $new_y = floor($new_x / $this->_imgInfoOrig['width'] * $this->_imgInfoOrig['height']); |
||
| 383 | }else |
||
| 384 | { |
||
| 385 | $scale = min($new_x/$this->_imgInfoOrig['width'], $new_y/$this->_imgInfoOrig['height']) ; |
||
| 386 | $new_x = floor($scale*$this->_imgInfoOrig['width']); |
||
| 387 | $new_y = floor($scale*$this->_imgInfoOrig['height']); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | if($unchangeIfsmaller) |
||
| 391 | { |
||
| 392 | if($this->_imgInfoOrig['width'] < $new_x && $this->_imgInfoOrig['height'] < $new_y ) |
||
| 393 | { |
||
| 394 | $new_x = $this->_imgInfoOrig['width']; |
||
| 395 | $new_y = $this->_imgInfoOrig['height']; |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | |||
| 400 | |||
| 401 | if(is_null($this->_imgOrig)) |
||
| 402 | { |
||
| 403 | $this->loadImage($filePath); |
||
| 404 | } |
||
| 405 | if(sizeof($this->_errors) == 0) |
||
| 406 | { |
||
| 407 | return $this->_resize($new_x, $new_y); |
||
| 408 | } |
||
| 409 | } |
||
| 410 | |||
| 411 | return false; |
||
| 412 | |||
| 413 | } // End resize |
||
| 414 | /** |
||
| 415 | * resize the image and return the thumbnail image details array("width"=>, "height"=>, "name") |
||
| 416 | * |
||
| 417 | * @param string $fileName |
||
| 418 | * @param int $new_x the thumbnail width |
||
| 419 | * @param int $new_y the thumbnail height |
||
| 420 | * @param string $mode can be save, view and both |
||
| 421 | * @return unknown |
||
| 422 | */ |
||
| 423 | function _resize( $new_x, $new_y) |
||
| 464 | /** |
||
| 465 | * Get the extension of a file name |
||
| 466 | * |
||
| 467 | * @param string $file |
||
| 468 | * @return string |
||
| 469 | * @copyright this function originally come from Andy's php |
||
| 470 | */ |
||
| 471 | function _getExtension($file) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Validate whether image reading/writing routines are valid. |
||
| 482 | * |
||
| 483 | * @param string $filename |
||
| 484 | * @param string $extension |
||
| 485 | * @param string $function |
||
| 486 | * @param bool $write |
||
| 487 | * @return bool |
||
| 488 | * @access private |
||
| 489 | * @copyright this function originally come from Andy's php |
||
| 490 | */ |
||
| 491 | function _isSupported($filename, $extension, $function, $write = false) |
||
| 517 | /** |
||
| 518 | * flip image horizotally or vertically |
||
| 519 | * |
||
| 520 | * @param string $direction |
||
| 521 | * @return boolean |
||
| 522 | */ |
||
| 523 | function flip($direction="horizontal") |
||
| 524 | { |
||
| 525 | $this->_createFinalImageHandler($this->_imgInfoOrig['width'], $this->_imgInfoOrig['height']); |
||
| 526 | if($direction != "vertical") |
||
| 527 | { |
||
| 528 | $dst_x = 0; |
||
| 529 | $dst_y = 0; |
||
| 530 | $src_x = $this->_imgInfoOrig['width'] -1; |
||
| 531 | $src_y = 0; |
||
| 532 | $dst_w = $this->_imgInfoOrig['width']; |
||
| 533 | $dst_h = $this->_imgInfoOrig['height']; |
||
| 534 | $src_w = 0 - $this->_imgInfoOrig['width']; |
||
| 535 | $src_h = $this->_imgInfoOrig['height']; |
||
| 536 | |||
| 537 | }else |
||
| 538 | { |
||
| 539 | $dst_x = 0; |
||
| 540 | $dst_y = 0; |
||
| 541 | $src_x = 0; |
||
| 542 | $src_y = $this->_imgInfoOrig['height'] - 1; |
||
| 543 | $dst_w = $this->_imgInfoOrig['width']; |
||
| 544 | $dst_h = $this->_imgInfoOrig['height']; |
||
| 545 | $src_w = $this->_imgInfoOrig['width']; |
||
| 546 | $src_h = 0 - $this->_imgInfoOrig['height']; |
||
| 547 | } |
||
| 548 | if(function_exists('ImageCopyResampled')){ |
||
| 549 | imagecopyresampled($this->_imgFinal, $this->_imgOrig, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
||
| 550 | } else { |
||
| 551 | imagecopyresized($this->_imgFinal, $this->_imgOrig, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
||
| 552 | } |
||
| 553 | $this->_imgInfoFinal['width'] = $dst_w; |
||
| 554 | $this->_imgInfoFinal['height'] = $dst_h; |
||
| 555 | $this->_imgInfoFinal['name'] = basename($this->imageFile); |
||
| 556 | $this->_imgInfoFinal['path'] = $this->imageFile; |
||
| 557 | if($this->_imgFinal) |
||
| 558 | { |
||
| 559 | return true; |
||
| 560 | }else |
||
| 561 | { |
||
| 562 | $this->_debug('Unable to resize the image on the fly.'); |
||
| 563 | return false; |
||
| 564 | |||
| 565 | } |
||
| 566 | } |
||
| 567 | /** |
||
| 568 | * flip vertically |
||
| 569 | * |
||
| 570 | * @return boolean |
||
| 571 | */ |
||
| 572 | function flipVertical() |
||
| 576 | /** |
||
| 577 | * flip horizontal |
||
| 578 | * |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | function flipHorizontal() |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * get the GD version information |
||
| 589 | * |
||
| 590 | * @param bool $versionOnly |
||
| 591 | * @return array |
||
| 592 | * @access private |
||
| 593 | * @copyright this function originally come from Andy's php |
||
| 594 | */ |
||
| 595 | function getGDInfo($versionOnly = false) |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Destroy the resources used by the images. |
||
| 672 | * |
||
| 673 | * @param bool $original |
||
| 674 | * @return void |
||
| 675 | * @access public |
||
| 676 | * @copyright this function originally come from Andy's php |
||
| 677 | */ |
||
| 678 | function DestroyImages($original = true) |
||
| 690 | |||
| 691 | function getImageInfo($imagePath) |
||
| 695 | /** |
||
| 696 | * get image information, e.g. width, height, type |
||
| 697 | * @access public |
||
| 698 | * @return array |
||
| 699 | */ |
||
| 700 | function _getImageInfo($imagePath) |
||
| 739 | function rotate($angle, $bgColor=0) |
||
| 740 | { |
||
| 741 | $angle = intval($angle) -360; |
||
| 742 | while($angle <0) |
||
| 743 | { |
||
| 744 | $angle += 360; |
||
| 745 | } |
||
| 746 | |||
| 747 | |||
| 748 | if($this->_imgFinal = imagerotate($this->_imgOrig, $angle, 0)) |
||
|
|
|||
| 749 | { |
||
| 750 | return true; |
||
| 751 | }else |
||
| 752 | { |
||
| 753 | return false; |
||
| 754 | } |
||
| 755 | |||
| 756 | |||
| 757 | } |
||
| 758 | /** |
||
| 759 | * get the original image info |
||
| 760 | * |
||
| 761 | * @return array |
||
| 762 | */ |
||
| 763 | function getOriginalImageInfo() |
||
| 767 | /** |
||
| 768 | * return the final image info |
||
| 769 | * |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | function getFinalImageInfo() |
||
| 787 | |||
| 788 | /** |
||
| 789 | * create final image handler |
||
| 790 | * |
||
| 791 | * @access private |
||
| 792 | * @param $dst_w width |
||
| 793 | * @param $dst_h height |
||
| 794 | * @return boolean |
||
| 795 | * @copyright original from noname at nivelzero dot ro |
||
| 796 | */ |
||
| 797 | function _createFinalImageHandler($dst_w, $dst_h) |
||
| 813 | } |
||
| 814 | |||
| 815 | ?> |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: