Complex classes like SimpleImage 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 SimpleImage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class SimpleImage { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int Default output image quality |
||
| 25 | * |
||
| 26 | */ |
||
| 27 | public $quality = 80; |
||
| 28 | |||
| 29 | protected $image, $filename, $original_info, $width, $height, $imagestring; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create instance and load an image, or create an image from scratch |
||
| 33 | * |
||
| 34 | * @param null|string $filename Path to image file (may be omitted to create image from scratch) |
||
| 35 | * @param int $width Image width (is used for creating image from scratch) |
||
|
|
|||
| 36 | * @param int|null $height If omitted - assumed equal to $width (is used for creating image from scratch) |
||
| 37 | * @param null|string $color Hex color string, array(red, green, blue) or array(red, green, blue, alpha). |
||
| 38 | * Where red, green, blue - integers 0-255, alpha - integer 0-127<br> |
||
| 39 | * (is used for creating image from scratch) |
||
| 40 | * |
||
| 41 | * @return SimpleImage |
||
| 42 | * @throws Exception |
||
| 43 | * |
||
| 44 | */ |
||
| 45 | function __construct($filename = null, $width = null, $height = null, $color = null) { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Destroy image resource |
||
| 56 | * |
||
| 57 | */ |
||
| 58 | function __destruct() { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Adaptive resize |
||
| 66 | * |
||
| 67 | * This function has been deprecated and will be removed in an upcoming release. Please |
||
| 68 | * update your code to use the `thumbnail()` method instead. The arguments for both |
||
| 69 | * methods are exactly the same. |
||
| 70 | * |
||
| 71 | * @param int $width |
||
| 72 | * @param int|null $height If omitted - assumed equal to $width |
||
| 73 | * |
||
| 74 | * @return SimpleImage |
||
| 75 | * |
||
| 76 | */ |
||
| 77 | function adaptive_resize($width, $height = null) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Rotates and/or flips an image automatically so the orientation will be correct (based on exif 'Orientation') |
||
| 85 | * |
||
| 86 | * @return SimpleImage |
||
| 87 | * |
||
| 88 | */ |
||
| 89 | function auto_orient() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Best fit (proportionally resize to fit in specified width/height) |
||
| 133 | * |
||
| 134 | * Shrink the image proportionally to fit inside a $width x $height box |
||
| 135 | * |
||
| 136 | * @param int $max_width |
||
| 137 | * @param int $max_height |
||
| 138 | * |
||
| 139 | * @return SimpleImage |
||
| 140 | * |
||
| 141 | */ |
||
| 142 | function best_fit($max_width, $max_height) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Blur |
||
| 173 | * |
||
| 174 | * @param string $type selective|gaussian |
||
| 175 | * @param int $passes Number of times to apply the filter |
||
| 176 | * |
||
| 177 | * @return SimpleImage |
||
| 178 | * |
||
| 179 | */ |
||
| 180 | function blur($type = 'selective', $passes = 1) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Brightness |
||
| 197 | * |
||
| 198 | * @param int $level Darkest = -255, lightest = 255 |
||
| 199 | * |
||
| 200 | * @return SimpleImage |
||
| 201 | * |
||
| 202 | */ |
||
| 203 | function brightness($level) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Contrast |
||
| 210 | * |
||
| 211 | * @param int $level Min = -100, max = 100 |
||
| 212 | * |
||
| 213 | * @return SimpleImage |
||
| 214 | * |
||
| 215 | * |
||
| 216 | */ |
||
| 217 | function contrast($level) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Colorize |
||
| 224 | * |
||
| 225 | * @param string $color Hex color string, array(red, green, blue) or array(red, green, blue, alpha). |
||
| 226 | * Where red, green, blue - integers 0-255, alpha - integer 0-127 |
||
| 227 | * @param float|int $opacity 0-1 |
||
| 228 | * |
||
| 229 | * @return SimpleImage |
||
| 230 | * |
||
| 231 | */ |
||
| 232 | function colorize($color, $opacity) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Create an image from scratch |
||
| 241 | * |
||
| 242 | * @param int $width Image width |
||
| 243 | * @param int|null $height If omitted - assumed equal to $width |
||
| 244 | * @param null|string $color Hex color string, array(red, green, blue) or array(red, green, blue, alpha). |
||
| 245 | * Where red, green, blue - integers 0-255, alpha - integer 0-127 |
||
| 246 | * |
||
| 247 | * @return SimpleImage |
||
| 248 | * |
||
| 249 | */ |
||
| 250 | function create($width, $height = null, $color = null) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Crop an image |
||
| 275 | * |
||
| 276 | * @param int $x1 Left |
||
| 277 | * @param int $y1 Top |
||
| 278 | * @param int $x2 Right |
||
| 279 | * @param int $y2 Bottom |
||
| 280 | * |
||
| 281 | * @return SimpleImage |
||
| 282 | * |
||
| 283 | */ |
||
| 284 | function crop($x1, $y1, $x2, $y2) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Desaturate (grayscale) |
||
| 313 | * |
||
| 314 | * @return SimpleImage |
||
| 315 | * |
||
| 316 | */ |
||
| 317 | function desaturate() { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Edge Detect |
||
| 324 | * |
||
| 325 | * @return SimpleImage |
||
| 326 | * |
||
| 327 | */ |
||
| 328 | function edges() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Emboss |
||
| 335 | * |
||
| 336 | * @return SimpleImage |
||
| 337 | * |
||
| 338 | */ |
||
| 339 | function emboss() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Fill image with color |
||
| 346 | * |
||
| 347 | * @param string $color Hex color string, array(red, green, blue) or array(red, green, blue, alpha). |
||
| 348 | * Where red, green, blue - integers 0-255, alpha - integer 0-127 |
||
| 349 | * |
||
| 350 | * @return SimpleImage |
||
| 351 | * |
||
| 352 | */ |
||
| 353 | function fill($color = '#000000') { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Fit to height (proportionally resize to specified height) |
||
| 367 | * |
||
| 368 | * @param int $height |
||
| 369 | * |
||
| 370 | * @return SimpleImage |
||
| 371 | * |
||
| 372 | */ |
||
| 373 | function fit_to_height($height) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Fit to width (proportionally resize to specified width) |
||
| 384 | * |
||
| 385 | * @param int $width |
||
| 386 | * |
||
| 387 | * @return SimpleImage |
||
| 388 | * |
||
| 389 | */ |
||
| 390 | function fit_to_width($width) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Flip an image horizontally or vertically |
||
| 401 | * |
||
| 402 | * @param string $direction x|y |
||
| 403 | * |
||
| 404 | * @return SimpleImage |
||
| 405 | * |
||
| 406 | */ |
||
| 407 | function flip($direction) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the current height |
||
| 434 | * |
||
| 435 | * @return int |
||
| 436 | * |
||
| 437 | */ |
||
| 438 | function get_height() { |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get the current orientation |
||
| 444 | * |
||
| 445 | * @return string portrait|landscape|square |
||
| 446 | * |
||
| 447 | */ |
||
| 448 | function get_orientation() { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Get info about the original image |
||
| 464 | * |
||
| 465 | * @return array <pre> array( |
||
| 466 | * width => 320, |
||
| 467 | * height => 200, |
||
| 468 | * orientation => ['portrait', 'landscape', 'square'], |
||
| 469 | * exif => array(...), |
||
| 470 | * mime => ['image/jpeg', 'image/gif', 'image/png'], |
||
| 471 | * format => ['jpeg', 'gif', 'png'] |
||
| 472 | * )</pre> |
||
| 473 | * |
||
| 474 | */ |
||
| 475 | function get_original_info() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the current width |
||
| 481 | * |
||
| 482 | * @return int |
||
| 483 | * |
||
| 484 | */ |
||
| 485 | function get_width() { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Invert |
||
| 491 | * |
||
| 492 | * @return SimpleImage |
||
| 493 | * |
||
| 494 | */ |
||
| 495 | function invert() { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Load an image |
||
| 502 | * |
||
| 503 | * @param string $filename Path to image file |
||
| 504 | * |
||
| 505 | * @return SimpleImage |
||
| 506 | * @throws Exception |
||
| 507 | * |
||
| 508 | */ |
||
| 509 | function load($filename) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Load a base64 string as image |
||
| 521 | * |
||
| 522 | * @param string $filename base64 string |
||
| 523 | * |
||
| 524 | * @return SimpleImage |
||
| 525 | * |
||
| 526 | */ |
||
| 527 | function load_base64($base64string) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Mean Remove |
||
| 539 | * |
||
| 540 | * @return SimpleImage |
||
| 541 | * |
||
| 542 | */ |
||
| 543 | function mean_remove() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Changes the opacity level of the image |
||
| 550 | * |
||
| 551 | * @param float|int $opacity 0-1 |
||
| 552 | * |
||
| 553 | * @throws Exception |
||
| 554 | * |
||
| 555 | */ |
||
| 556 | function opacity($opacity) { |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Outputs image without saving |
||
| 580 | * |
||
| 581 | * @param null|string $format If omitted or null - format of original file will be used, may be gif|jpg|png |
||
| 582 | * @param int|null $quality Output image quality in percents 0-100 |
||
| 583 | * |
||
| 584 | * @throws Exception |
||
| 585 | * |
||
| 586 | */ |
||
| 587 | function output($format = null, $quality = null) { |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Outputs image as data base64 to use as img src |
||
| 636 | * |
||
| 637 | * @param null|string $format If omitted or null - format of original file will be used, may be gif|jpg|png |
||
| 638 | * @param int|null $quality Output image quality in percents 0-100 |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | * @throws Exception |
||
| 642 | * |
||
| 643 | */ |
||
| 644 | function output_base64($format = null, $quality = null) { |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Overlay |
||
| 695 | * |
||
| 696 | * Overlay an image on top of another, works with 24-bit PNG alpha-transparency |
||
| 697 | * |
||
| 698 | * @param string $overlay An image filename or a SimpleImage object |
||
| 699 | * @param string $position center|top|left|bottom|right|top left|top right|bottom left|bottom right |
||
| 700 | * @param float|int $opacity Overlay opacity 0-1 |
||
| 701 | * @param int $x_offset Horizontal offset in pixels |
||
| 702 | * @param int $y_offset Vertical offset in pixels |
||
| 703 | * |
||
| 704 | * @return SimpleImage |
||
| 705 | * |
||
| 706 | */ |
||
| 707 | function overlay($overlay, $position = 'center', $opacity = 1, $x_offset = 0, $y_offset = 0) { |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Pixelate |
||
| 767 | * |
||
| 768 | * @param int $block_size Size in pixels of each resulting block |
||
| 769 | * |
||
| 770 | * @return SimpleImage |
||
| 771 | * |
||
| 772 | */ |
||
| 773 | function pixelate($block_size = 10) { |
||
| 777 | |||
| 778 | /** |
||
| 779 | * Resize an image to the specified dimensions |
||
| 780 | * |
||
| 781 | * @param int $width |
||
| 782 | * @param int $height |
||
| 783 | * |
||
| 784 | * @return SimpleImage |
||
| 785 | * |
||
| 786 | */ |
||
| 787 | function resize($width, $height) { |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Rotate an image |
||
| 821 | * |
||
| 822 | * @param int $angle 0-360 |
||
| 823 | * @param string $bg_color Hex color string, array(red, green, blue) or array(red, green, blue, alpha). |
||
| 824 | * Where red, green, blue - integers 0-255, alpha - integer 0-127 |
||
| 825 | * |
||
| 826 | * @return SimpleImage |
||
| 827 | * |
||
| 828 | */ |
||
| 829 | function rotate($angle, $bg_color = '#000000') { |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Save an image |
||
| 849 | * |
||
| 850 | * The resulting format will be determined by the file extension. |
||
| 851 | * |
||
| 852 | * @param null|string $filename If omitted - original file will be overwritten |
||
| 853 | * @param null|int $quality Output image quality in percents 0-100 |
||
| 854 | * |
||
| 855 | * @return SimpleImage |
||
| 856 | * @throws Exception |
||
| 857 | * |
||
| 858 | */ |
||
| 859 | function save($filename = null, $quality = null) { |
||
| 890 | |||
| 891 | /** |
||
| 892 | * Sepia |
||
| 893 | * |
||
| 894 | * @return SimpleImage |
||
| 895 | * |
||
| 896 | */ |
||
| 897 | function sepia() { |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Sketch |
||
| 905 | * |
||
| 906 | * @return SimpleImage |
||
| 907 | * |
||
| 908 | */ |
||
| 909 | function sketch() { |
||
| 913 | |||
| 914 | /** |
||
| 915 | * Smooth |
||
| 916 | * |
||
| 917 | * @param int $level Min = -10, max = 10 |
||
| 918 | * |
||
| 919 | * @return SimpleImage |
||
| 920 | * |
||
| 921 | */ |
||
| 922 | function smooth($level) { |
||
| 926 | |||
| 927 | /** |
||
| 928 | * Add text to an image |
||
| 929 | * |
||
| 930 | * @param string $text |
||
| 931 | * @param string $font_file |
||
| 932 | * @param float|int $font_size |
||
| 933 | * @param string $color |
||
| 934 | * @param string $position |
||
| 935 | * @param int $x_offset |
||
| 936 | * @param int $y_offset |
||
| 937 | * |
||
| 938 | * @return SimpleImage |
||
| 939 | * @throws Exception |
||
| 940 | * |
||
| 941 | */ |
||
| 942 | function text($text, $font_file, $font_size = 12, $color = '#000000', $position = 'center', $x_offset = 0, $y_offset = 0) { |
||
| 1006 | |||
| 1007 | /** |
||
| 1008 | * Thumbnail |
||
| 1009 | * |
||
| 1010 | * This function attempts to get the image to as close to the provided dimensions as possible, and then crops the |
||
| 1011 | * remaining overflow (from the center) to get the image to be the size specified. Useful for generating thumbnails. |
||
| 1012 | * |
||
| 1013 | * @param int $width |
||
| 1014 | * @param int|null $height If omitted - assumed equal to $width |
||
| 1015 | * |
||
| 1016 | * @return SimpleImage |
||
| 1017 | * |
||
| 1018 | */ |
||
| 1019 | function thumbnail($width, $height = null) { |
||
| 1041 | |||
| 1042 | /** |
||
| 1043 | * Returns the file extension of the specified file |
||
| 1044 | * |
||
| 1045 | * @param string $filename |
||
| 1046 | * |
||
| 1047 | * @return string |
||
| 1048 | * |
||
| 1049 | */ |
||
| 1050 | protected function file_ext($filename) { |
||
| 1059 | |||
| 1060 | /** |
||
| 1061 | * Get meta data of image or base64 string |
||
| 1062 | * |
||
| 1063 | * @param string|null $imagestring If omitted treat as a normal image |
||
| 1064 | * |
||
| 1065 | * @return SimpleImage |
||
| 1066 | * @throws Exception |
||
| 1067 | * |
||
| 1068 | */ |
||
| 1069 | protected function get_meta_data() { |
||
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Same as PHP's imagecopymerge() function, except preserves alpha-transparency in 24-bit PNGs |
||
| 1114 | * |
||
| 1115 | * @param $dst_im |
||
| 1116 | * @param $src_im |
||
| 1117 | * @param $dst_x |
||
| 1118 | * @param $dst_y |
||
| 1119 | * @param $src_x |
||
| 1120 | * @param $src_y |
||
| 1121 | * @param $src_w |
||
| 1122 | * @param $src_h |
||
| 1123 | * @param $pct |
||
| 1124 | * |
||
| 1125 | * @link http://www.php.net/manual/en/function.imagecopymerge.php#88456 |
||
| 1126 | * |
||
| 1127 | */ |
||
| 1128 | protected function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct) { |
||
| 1178 | |||
| 1179 | /** |
||
| 1180 | * Ensures $value is always within $min and $max range. |
||
| 1181 | * |
||
| 1182 | * If lower, $min is returned. If higher, $max is returned. |
||
| 1183 | * |
||
| 1184 | * @param int|float $value |
||
| 1185 | * @param int|float $min |
||
| 1186 | * @param int|float $max |
||
| 1187 | * |
||
| 1188 | * @return int|float |
||
| 1189 | * |
||
| 1190 | */ |
||
| 1191 | protected function keep_within($value, $min, $max) { |
||
| 1204 | |||
| 1205 | /** |
||
| 1206 | * Converts a hex color value to its RGB equivalent |
||
| 1207 | * |
||
| 1208 | * @param string $color Hex color string, array(red, green, blue) or array(red, green, blue, alpha). |
||
| 1209 | * Where red, green, blue - integers 0-255, alpha - integer 0-127 |
||
| 1210 | * |
||
| 1211 | * @return array|bool |
||
| 1212 | * |
||
| 1213 | */ |
||
| 1214 | protected function normalize_color($color) { |
||
| 1263 | |||
| 1264 | } |
||
| 1265 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.