Complex classes like CImage 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 CImage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class CImage |
||
|
|
|||
| 10 | { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Constants type of PNG image |
||
| 14 | */ |
||
| 15 | const PNG_GREYSCALE = 0; |
||
| 16 | const PNG_RGB = 2; |
||
| 17 | const PNG_RGB_PALETTE = 3; |
||
| 18 | const PNG_GREYSCALE_ALPHA = 4; |
||
| 19 | const PNG_RGB_ALPHA = 6; |
||
| 20 | |||
| 21 | |||
| 22 | |||
| 23 | /** |
||
| 24 | * Constant for default image quality when not set |
||
| 25 | */ |
||
| 26 | const JPEG_QUALITY_DEFAULT = 60; |
||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Quality level for JPEG images. |
||
| 32 | */ |
||
| 33 | private $quality; |
||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | /** |
||
| 38 | * Is the quality level set from external use (true) or is it default (false)? |
||
| 39 | */ |
||
| 40 | private $useQuality = false; |
||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Constant for default image quality when not set |
||
| 46 | */ |
||
| 47 | const PNG_COMPRESSION_DEFAULT = -1; |
||
| 48 | |||
| 49 | |||
| 50 | |||
| 51 | /** |
||
| 52 | * Compression level for PNG images. |
||
| 53 | */ |
||
| 54 | private $compress; |
||
| 55 | |||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Is the compress level set from external use (true) or is it default (false)? |
||
| 60 | */ |
||
| 61 | private $useCompress = false; |
||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Add HTTP headers for outputing image. |
||
| 68 | */ |
||
| 69 | private $HTTPHeader = array(); |
||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | /** |
||
| 74 | * Default background color, red, green, blue, alpha. |
||
| 75 | * |
||
| 76 | * @todo remake when upgrading to PHP 5.5 |
||
| 77 | */ |
||
| 78 | /* |
||
| 79 | const BACKGROUND_COLOR = array( |
||
| 80 | 'red' => 0, |
||
| 81 | 'green' => 0, |
||
| 82 | 'blue' => 0, |
||
| 83 | 'alpha' => null, |
||
| 84 | );*/ |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * Default background color to use. |
||
| 90 | * |
||
| 91 | * @todo remake when upgrading to PHP 5.5 |
||
| 92 | */ |
||
| 93 | //private $bgColorDefault = self::BACKGROUND_COLOR; |
||
| 94 | private $bgColorDefault = array( |
||
| 95 | 'red' => 0, |
||
| 96 | 'green' => 0, |
||
| 97 | 'blue' => 0, |
||
| 98 | 'alpha' => null, |
||
| 99 | ); |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Background color to use, specified as part of options. |
||
| 104 | */ |
||
| 105 | private $bgColor; |
||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | /** |
||
| 110 | * Where to save the target file. |
||
| 111 | */ |
||
| 112 | private $saveFolder; |
||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | /** |
||
| 117 | * The working image object. |
||
| 118 | */ |
||
| 119 | private $image; |
||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Image filename, may include subdirectory, relative from $imageFolder |
||
| 125 | */ |
||
| 126 | private $imageSrc; |
||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * Actual path to the image, $imageFolder . '/' . $imageSrc |
||
| 132 | */ |
||
| 133 | private $pathToImage; |
||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * File type for source image, as provided by getimagesize() |
||
| 139 | */ |
||
| 140 | private $fileType; |
||
| 141 | |||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * File extension to use when saving image. |
||
| 146 | */ |
||
| 147 | private $extension; |
||
| 148 | |||
| 149 | |||
| 150 | |||
| 151 | /** |
||
| 152 | * Output format, supports null (image) or json. |
||
| 153 | */ |
||
| 154 | private $outputFormat = null; |
||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * Verbose mode to print out a trace and display the created image |
||
| 160 | */ |
||
| 161 | private $verbose = false; |
||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | /** |
||
| 166 | * Keep a log/trace on what happens |
||
| 167 | */ |
||
| 168 | private $log = array(); |
||
| 169 | |||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * Handle image as palette image |
||
| 174 | */ |
||
| 175 | private $palette; |
||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Target filename, with path, to save resulting image in. |
||
| 181 | */ |
||
| 182 | private $cacheFileName; |
||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * Set a format to save image as, or null to use original format. |
||
| 188 | */ |
||
| 189 | private $saveAs; |
||
| 190 | |||
| 191 | |||
| 192 | /** |
||
| 193 | * Path to command for filter optimize, for example optipng or null. |
||
| 194 | */ |
||
| 195 | private $pngFilter; |
||
| 196 | private $pngFilterCmd; |
||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Path to command for deflate optimize, for example pngout or null. |
||
| 202 | */ |
||
| 203 | private $pngDeflate; |
||
| 204 | private $pngDeflateCmd; |
||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Path to command to optimize jpeg images, for example jpegtran or null. |
||
| 210 | */ |
||
| 211 | private $jpegOptimize; |
||
| 212 | private $jpegOptimizeCmd; |
||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | /** |
||
| 217 | * Image dimensions, calculated from loaded image. |
||
| 218 | */ |
||
| 219 | private $width; // Calculated from source image |
||
| 220 | private $height; // Calculated from source image |
||
| 221 | |||
| 222 | |||
| 223 | /** |
||
| 224 | * New image dimensions, incoming as argument or calculated. |
||
| 225 | */ |
||
| 226 | private $newWidth; |
||
| 227 | private $newWidthOrig; // Save original value |
||
| 228 | private $newHeight; |
||
| 229 | private $newHeightOrig; // Save original value |
||
| 230 | |||
| 231 | |||
| 232 | /** |
||
| 233 | * Change target height & width when different dpr, dpr 2 means double image dimensions. |
||
| 234 | */ |
||
| 235 | private $dpr = 1; |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Always upscale images, even if they are smaller than target image. |
||
| 240 | */ |
||
| 241 | const UPSCALE_DEFAULT = true; |
||
| 242 | private $upscale = self::UPSCALE_DEFAULT; |
||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * Array with details on how to crop, incoming as argument and calculated. |
||
| 248 | */ |
||
| 249 | public $crop; |
||
| 250 | public $cropOrig; // Save original value |
||
| 251 | |||
| 252 | |||
| 253 | /** |
||
| 254 | * String with details on how to do image convolution. String |
||
| 255 | * should map a key in the $convolvs array or be a string of |
||
| 256 | * 11 float values separated by comma. The first nine builds |
||
| 257 | * up the matrix, then divisor and last offset. |
||
| 258 | */ |
||
| 259 | private $convolve; |
||
| 260 | |||
| 261 | |||
| 262 | /** |
||
| 263 | * Custom convolution expressions, matrix 3x3, divisor and offset. |
||
| 264 | */ |
||
| 265 | private $convolves = array( |
||
| 266 | 'lighten' => '0,0,0, 0,12,0, 0,0,0, 9, 0', |
||
| 267 | 'darken' => '0,0,0, 0,6,0, 0,0,0, 9, 0', |
||
| 268 | 'sharpen' => '-1,-1,-1, -1,16,-1, -1,-1,-1, 8, 0', |
||
| 269 | 'sharpen-alt' => '0,-1,0, -1,5,-1, 0,-1,0, 1, 0', |
||
| 270 | 'emboss' => '1,1,-1, 1,3,-1, 1,-1,-1, 3, 0', |
||
| 271 | 'emboss-alt' => '-2,-1,0, -1,1,1, 0,1,2, 1, 0', |
||
| 272 | 'blur' => '1,1,1, 1,15,1, 1,1,1, 23, 0', |
||
| 273 | 'gblur' => '1,2,1, 2,4,2, 1,2,1, 16, 0', |
||
| 274 | 'edge' => '-1,-1,-1, -1,8,-1, -1,-1,-1, 9, 0', |
||
| 275 | 'edge-alt' => '0,1,0, 1,-4,1, 0,1,0, 1, 0', |
||
| 276 | 'draw' => '0,-1,0, -1,5,-1, 0,-1,0, 0, 0', |
||
| 277 | 'mean' => '1,1,1, 1,1,1, 1,1,1, 9, 0', |
||
| 278 | 'motion' => '1,0,0, 0,1,0, 0,0,1, 3, 0', |
||
| 279 | ); |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Resize strategy to fill extra area with background color. |
||
| 284 | * True or false. |
||
| 285 | */ |
||
| 286 | private $fillToFit; |
||
| 287 | |||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * To store value for option scale. |
||
| 292 | */ |
||
| 293 | private $scale; |
||
| 294 | |||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * To store value for option. |
||
| 299 | */ |
||
| 300 | private $rotateBefore; |
||
| 301 | |||
| 302 | |||
| 303 | |||
| 304 | /** |
||
| 305 | * To store value for option. |
||
| 306 | */ |
||
| 307 | private $rotateAfter; |
||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * To store value for option. |
||
| 313 | */ |
||
| 314 | private $autoRotate; |
||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * To store value for option. |
||
| 320 | */ |
||
| 321 | private $sharpen; |
||
| 322 | |||
| 323 | |||
| 324 | |||
| 325 | /** |
||
| 326 | * To store value for option. |
||
| 327 | */ |
||
| 328 | private $emboss; |
||
| 329 | |||
| 330 | |||
| 331 | |||
| 332 | /** |
||
| 333 | * To store value for option. |
||
| 334 | */ |
||
| 335 | private $blur; |
||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * Used with option area to set which parts of the image to use. |
||
| 341 | */ |
||
| 342 | private $offset; |
||
| 343 | |||
| 344 | |||
| 345 | |||
| 346 | /** |
||
| 347 | * Calculate target dimension for image when using fill-to-fit resize strategy. |
||
| 348 | */ |
||
| 349 | private $fillWidth; |
||
| 350 | private $fillHeight; |
||
| 351 | |||
| 352 | |||
| 353 | |||
| 354 | /** |
||
| 355 | * Allow remote file download, default is to disallow remote file download. |
||
| 356 | */ |
||
| 357 | private $allowRemote = false; |
||
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Path to cache for remote download. |
||
| 363 | */ |
||
| 364 | private $remoteCache; |
||
| 365 | |||
| 366 | |||
| 367 | |||
| 368 | /** |
||
| 369 | * Pattern to recognize a remote file. |
||
| 370 | */ |
||
| 371 | //private $remotePattern = '#^[http|https]://#'; |
||
| 372 | private $remotePattern = '#^https?://#'; |
||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | /** |
||
| 377 | * Use the cache if true, set to false to ignore the cached file. |
||
| 378 | */ |
||
| 379 | private $useCache = true; |
||
| 380 | |||
| 381 | |||
| 382 | /** |
||
| 383 | * Disable the fasttrackCacke to start with, inject an object to enable it. |
||
| 384 | */ |
||
| 385 | private $fastTrackCache = null; |
||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | /* |
||
| 390 | * Set whitelist for valid hostnames from where remote source can be |
||
| 391 | * downloaded. |
||
| 392 | */ |
||
| 393 | private $remoteHostWhitelist = null; |
||
| 394 | |||
| 395 | |||
| 396 | |||
| 397 | /* |
||
| 398 | * Do verbose logging to file by setting this to a filename. |
||
| 399 | */ |
||
| 400 | private $verboseFileName = null; |
||
| 401 | |||
| 402 | |||
| 403 | |||
| 404 | /* |
||
| 405 | * Output to ascii can take som options as an array. |
||
| 406 | */ |
||
| 407 | private $asciiOptions = array(); |
||
| 408 | |||
| 409 | |||
| 410 | |||
| 411 | /* |
||
| 412 | * Image copy strategy, defaults to RESAMPLE. |
||
| 413 | */ |
||
| 414 | const RESIZE = 1; |
||
| 415 | const RESAMPLE = 2; |
||
| 416 | private $copyStrategy = NULL; |
||
| 417 | |||
| 418 | |||
| 419 | |||
| 420 | /** |
||
| 421 | * Properties, the class is mutable and the method setOptions() |
||
| 422 | * decides (partly) what properties are created. |
||
| 423 | * |
||
| 424 | * @todo Clean up these and check if and how they are used |
||
| 425 | */ |
||
| 426 | |||
| 427 | public $keepRatio; |
||
| 428 | public $cropToFit; |
||
| 429 | private $cropWidth; |
||
| 430 | private $cropHeight; |
||
| 431 | public $crop_x; |
||
| 432 | public $crop_y; |
||
| 433 | public $filters; |
||
| 434 | private $attr; // Calculated from source image |
||
| 435 | |||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | /** |
||
| 440 | * Constructor, can take arguments to init the object. |
||
| 441 | * |
||
| 442 | * @param string $imageSrc filename which may contain subdirectory. |
||
| 443 | * @param string $imageFolder path to root folder for images. |
||
| 444 | * @param string $saveFolder path to folder where to save the new file or null to skip saving. |
||
| 445 | * @param string $saveName name of target file when saveing. |
||
| 446 | */ |
||
| 447 | public function __construct($imageSrc = null, $imageFolder = null, $saveFolder = null, $saveName = null) |
||
| 452 | |||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * Inject object and use it, must be available as member. |
||
| 457 | * |
||
| 458 | * @param string $property to set as object. |
||
| 459 | * @param object $object to set to property. |
||
| 460 | * |
||
| 461 | * @return $this |
||
| 462 | */ |
||
| 463 | public function injectDependency($property, $object) |
||
| 471 | |||
| 472 | |||
| 473 | |||
| 474 | /** |
||
| 475 | * Set verbose mode. |
||
| 476 | * |
||
| 477 | * @param boolean $mode true or false to enable and disable verbose mode, |
||
| 478 | * default is true. |
||
| 479 | * |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function setVerbose($mode = true) |
||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | /** |
||
| 491 | * Set save folder, base folder for saving cache files. |
||
| 492 | * |
||
| 493 | * @todo clean up how $this->saveFolder is used in other methods. |
||
| 494 | * |
||
| 495 | * @param string $path where to store cached files. |
||
| 496 | * |
||
| 497 | * @return $this |
||
| 498 | */ |
||
| 499 | public function setSaveFolder($path) |
||
| 504 | |||
| 505 | |||
| 506 | |||
| 507 | /** |
||
| 508 | * Use cache or not. |
||
| 509 | * |
||
| 510 | * @param boolean $use true or false to use cache. |
||
| 511 | * |
||
| 512 | * @return $this |
||
| 513 | */ |
||
| 514 | public function useCache($use = true) |
||
| 519 | |||
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * Create and save a dummy image. Use dimensions as stated in |
||
| 524 | * $this->newWidth, or $width or default to 100 (same for height. |
||
| 525 | * |
||
| 526 | * @param integer $width use specified width for image dimension. |
||
| 527 | * @param integer $height use specified width for image dimension. |
||
| 528 | * |
||
| 529 | * @return $this |
||
| 530 | */ |
||
| 531 | public function createDummyImage($width = null, $height = null) |
||
| 540 | |||
| 541 | |||
| 542 | |||
| 543 | /** |
||
| 544 | * Allow or disallow remote image download. |
||
| 545 | * |
||
| 546 | * @param boolean $allow true or false to enable and disable. |
||
| 547 | * @param string $cache path to cache dir. |
||
| 548 | * @param string $pattern to use to detect if its a remote file. |
||
| 549 | * |
||
| 550 | * @return $this |
||
| 551 | */ |
||
| 552 | public function setRemoteDownload($allow, $cache, $pattern = null) |
||
| 567 | |||
| 568 | |||
| 569 | |||
| 570 | /** |
||
| 571 | * Check if the image resource is a remote file or not. |
||
| 572 | * |
||
| 573 | * @param string $src check if src is remote. |
||
| 574 | * |
||
| 575 | * @return boolean true if $src is a remote file, else false. |
||
| 576 | */ |
||
| 577 | public function isRemoteSource($src) |
||
| 583 | |||
| 584 | |||
| 585 | |||
| 586 | /** |
||
| 587 | * Set whitelist for valid hostnames from where remote source can be |
||
| 588 | * downloaded. |
||
| 589 | * |
||
| 590 | * @param array $whitelist with regexp hostnames to allow download from. |
||
| 591 | * |
||
| 592 | * @return $this |
||
| 593 | */ |
||
| 594 | public function setRemoteHostWhitelist($whitelist = null) |
||
| 603 | |||
| 604 | |||
| 605 | |||
| 606 | /** |
||
| 607 | * Check if the hostname for the remote image, is on a whitelist, |
||
| 608 | * if the whitelist is defined. |
||
| 609 | * |
||
| 610 | * @param string $src the remote source. |
||
| 611 | * |
||
| 612 | * @return boolean true if hostname on $src is in the whitelist, else false. |
||
| 613 | */ |
||
| 614 | public function isRemoteSourceOnWhitelist($src) |
||
| 631 | |||
| 632 | |||
| 633 | |||
| 634 | /** |
||
| 635 | * Check if file extension is valid as a file extension. |
||
| 636 | * |
||
| 637 | * @param string $extension of image file. |
||
| 638 | * |
||
| 639 | * @return $this |
||
| 640 | */ |
||
| 641 | private function checkFileExtension($extension) |
||
| 650 | |||
| 651 | |||
| 652 | |||
| 653 | /** |
||
| 654 | * Normalize the file extension. |
||
| 655 | * |
||
| 656 | * @param string $extension of image file or skip to use internal. |
||
| 657 | * |
||
| 658 | * @return string $extension as a normalized file extension. |
||
| 659 | */ |
||
| 660 | private function normalizeFileExtension($extension = null) |
||
| 670 | |||
| 671 | |||
| 672 | |||
| 673 | /** |
||
| 674 | * Download a remote image and return path to its local copy. |
||
| 675 | * |
||
| 676 | * @param string $src remote path to image. |
||
| 677 | * |
||
| 678 | * @return string as path to downloaded remote source. |
||
| 679 | */ |
||
| 680 | public function downloadRemoteSource($src) |
||
| 702 | |||
| 703 | |||
| 704 | |||
| 705 | /** |
||
| 706 | * Set source file to use as image source. |
||
| 707 | * |
||
| 708 | * @param string $src of image. |
||
| 709 | * @param string $dir as optional base directory where images are. |
||
| 710 | * |
||
| 711 | * @return $this |
||
| 712 | */ |
||
| 713 | public function setSource($src, $dir = null) |
||
| 737 | |||
| 738 | |||
| 739 | |||
| 740 | /** |
||
| 741 | * Set target file. |
||
| 742 | * |
||
| 743 | * @param string $src of target image. |
||
| 744 | * @param string $dir as optional base directory where images are stored. |
||
| 745 | * Uses $this->saveFolder if null. |
||
| 746 | * |
||
| 747 | * @return $this |
||
| 748 | */ |
||
| 749 | public function setTarget($src = null, $dir = null) |
||
| 768 | |||
| 769 | |||
| 770 | |||
| 771 | /** |
||
| 772 | * Get filename of target file. |
||
| 773 | * |
||
| 774 | * @return Boolean|String as filename of target or false if not set. |
||
| 775 | */ |
||
| 776 | public function getTarget() |
||
| 780 | |||
| 781 | |||
| 782 | |||
| 783 | /** |
||
| 784 | * Set options to use when processing image. |
||
| 785 | * |
||
| 786 | * @param array $args used when processing image. |
||
| 787 | * |
||
| 788 | * @return $this |
||
| 789 | */ |
||
| 790 | public function setOptions($args) |
||
| 791 | { |
||
| 792 | $this->log("Set new options for processing image."); |
||
| 793 | |||
| 794 | $defaults = array( |
||
| 795 | // Options for calculate dimensions |
||
| 796 | 'newWidth' => null, |
||
| 797 | 'newHeight' => null, |
||
| 798 | 'aspectRatio' => null, |
||
| 799 | 'keepRatio' => true, |
||
| 800 | 'cropToFit' => false, |
||
| 801 | 'fillToFit' => null, |
||
| 802 | 'crop' => null, //array('width'=>null, 'height'=>null, 'start_x'=>0, 'start_y'=>0), |
||
| 803 | 'area' => null, //'0,0,0,0', |
||
| 804 | 'upscale' => self::UPSCALE_DEFAULT, |
||
| 805 | |||
| 806 | // Options for caching or using original |
||
| 807 | 'useCache' => true, |
||
| 808 | 'useOriginal' => true, |
||
| 809 | |||
| 810 | // Pre-processing, before resizing is done |
||
| 811 | 'scale' => null, |
||
| 812 | 'rotateBefore' => null, |
||
| 813 | 'autoRotate' => false, |
||
| 814 | |||
| 815 | // General options |
||
| 816 | 'bgColor' => null, |
||
| 817 | |||
| 818 | // Post-processing, after resizing is done |
||
| 819 | 'palette' => null, |
||
| 820 | 'filters' => null, |
||
| 821 | 'sharpen' => null, |
||
| 822 | 'emboss' => null, |
||
| 823 | 'blur' => null, |
||
| 824 | 'convolve' => null, |
||
| 825 | 'rotateAfter' => null, |
||
| 826 | |||
| 827 | // Output format |
||
| 828 | 'outputFormat' => null, |
||
| 829 | 'dpr' => 1, |
||
| 830 | ); |
||
| 831 | |||
| 832 | // Convert crop settings from string to array |
||
| 833 | if (isset($args['crop']) && !is_array($args['crop'])) { |
||
| 834 | $pices = explode(',', $args['crop']); |
||
| 835 | $args['crop'] = array( |
||
| 836 | 'width' => $pices[0], |
||
| 837 | 'height' => $pices[1], |
||
| 838 | 'start_x' => $pices[2], |
||
| 839 | 'start_y' => $pices[3], |
||
| 840 | ); |
||
| 841 | } |
||
| 842 | |||
| 843 | // Convert area settings from string to array |
||
| 844 | if (isset($args['area']) && !is_array($args['area'])) { |
||
| 845 | $pices = explode(',', $args['area']); |
||
| 846 | $args['area'] = array( |
||
| 847 | 'top' => $pices[0], |
||
| 848 | 'right' => $pices[1], |
||
| 849 | 'bottom' => $pices[2], |
||
| 850 | 'left' => $pices[3], |
||
| 851 | ); |
||
| 852 | } |
||
| 853 | |||
| 854 | // Convert filter settings from array of string to array of array |
||
| 855 | if (isset($args['filters']) && is_array($args['filters'])) { |
||
| 856 | foreach ($args['filters'] as $key => $filterStr) { |
||
| 857 | $parts = explode(',', $filterStr); |
||
| 858 | $filter = $this->mapFilter($parts[0]); |
||
| 859 | $filter['str'] = $filterStr; |
||
| 860 | for ($i=1; $i<=$filter['argc']; $i++) { |
||
| 861 | if (isset($parts[$i])) { |
||
| 862 | $filter["arg{$i}"] = $parts[$i]; |
||
| 863 | } else { |
||
| 864 | throw new Exception( |
||
| 865 | 'Missing arg to filter, review how many arguments are needed at |
||
| 866 | http://php.net/manual/en/function.imagefilter.php' |
||
| 867 | ); |
||
| 868 | } |
||
| 869 | } |
||
| 870 | $args['filters'][$key] = $filter; |
||
| 871 | } |
||
| 872 | } |
||
| 873 | |||
| 874 | // Merge default arguments with incoming and set properties. |
||
| 875 | //$args = array_merge_recursive($defaults, $args); |
||
| 876 | $args = array_merge($defaults, $args); |
||
| 877 | foreach ($defaults as $key => $val) { |
||
| 878 | $this->{$key} = $args[$key]; |
||
| 879 | } |
||
| 880 | |||
| 881 | if ($this->bgColor) { |
||
| 882 | $this->setDefaultBackgroundColor($this->bgColor); |
||
| 883 | } |
||
| 884 | |||
| 885 | // Save original values to enable re-calculating |
||
| 886 | $this->newWidthOrig = $this->newWidth; |
||
| 887 | $this->newHeightOrig = $this->newHeight; |
||
| 888 | $this->cropOrig = $this->crop; |
||
| 889 | |||
| 890 | return $this; |
||
| 891 | } |
||
| 892 | |||
| 893 | |||
| 894 | |||
| 895 | /** |
||
| 896 | * Map filter name to PHP filter and id. |
||
| 897 | * |
||
| 898 | * @param string $name the name of the filter. |
||
| 899 | * |
||
| 900 | * @return array with filter settings |
||
| 901 | * @throws Exception |
||
| 902 | */ |
||
| 903 | private function mapFilter($name) |
||
| 926 | |||
| 927 | |||
| 928 | |||
| 929 | /** |
||
| 930 | * Load image details from original image file. |
||
| 931 | * |
||
| 932 | * @param string $file the file to load or null to use $this->pathToImage. |
||
| 933 | * |
||
| 934 | * @return $this |
||
| 935 | * @throws Exception |
||
| 936 | */ |
||
| 937 | public function loadImageDetails($file = null) |
||
| 959 | |||
| 960 | |||
| 961 | |||
| 962 | /** |
||
| 963 | * Init new width and height and do some sanity checks on constraints, before any |
||
| 964 | * processing can be done. |
||
| 965 | * |
||
| 966 | * @return $this |
||
| 967 | * @throws Exception |
||
| 968 | */ |
||
| 969 | public function initDimensions() |
||
| 1034 | |||
| 1035 | |||
| 1036 | |||
| 1037 | /** |
||
| 1038 | * Calculate new width and height of image, based on settings. |
||
| 1039 | * |
||
| 1040 | * @return $this |
||
| 1041 | */ |
||
| 1042 | public function calculateNewWidthAndHeight() |
||
| 1185 | |||
| 1186 | |||
| 1187 | |||
| 1188 | /** |
||
| 1189 | * Re-calculate image dimensions when original image dimension has changed. |
||
| 1190 | * |
||
| 1191 | * @return $this |
||
| 1192 | */ |
||
| 1193 | public function reCalculateDimensions() |
||
| 1206 | |||
| 1207 | |||
| 1208 | |||
| 1209 | /** |
||
| 1210 | * Set extension for filename to save as. |
||
| 1211 | * |
||
| 1212 | * @param string $saveas extension to save image as |
||
| 1213 | * |
||
| 1214 | * @return $this |
||
| 1215 | */ |
||
| 1216 | public function setSaveAsExtension($saveAs = null) |
||
| 1229 | |||
| 1230 | |||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Set JPEG quality to use when saving image |
||
| 1234 | * |
||
| 1235 | * @param int $quality as the quality to set. |
||
| 1236 | * |
||
| 1237 | * @return $this |
||
| 1238 | */ |
||
| 1239 | public function setJpegQuality($quality = null) |
||
| 1256 | |||
| 1257 | |||
| 1258 | |||
| 1259 | /** |
||
| 1260 | * Set PNG compressen algorithm to use when saving image |
||
| 1261 | * |
||
| 1262 | * @param int $compress as the algorithm to use. |
||
| 1263 | * |
||
| 1264 | * @return $this |
||
| 1265 | */ |
||
| 1266 | public function setPngCompression($compress = null) |
||
| 1283 | |||
| 1284 | |||
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Use original image if possible, check options which affects image processing. |
||
| 1288 | * |
||
| 1289 | * @param boolean $useOrig default is to use original if possible, else set to false. |
||
| 1290 | * |
||
| 1291 | * @return $this |
||
| 1292 | */ |
||
| 1293 | public function useOriginalIfPossible($useOrig = true) |
||
| 1294 | { |
||
| 1295 | if ($useOrig |
||
| 1296 | && ($this->newWidth == $this->width) |
||
| 1297 | && ($this->newHeight == $this->height) |
||
| 1298 | && !$this->area |
||
| 1299 | && !$this->crop |
||
| 1300 | && !$this->cropToFit |
||
| 1301 | && !$this->fillToFit |
||
| 1302 | && !$this->filters |
||
| 1303 | && !$this->sharpen |
||
| 1304 | && !$this->emboss |
||
| 1305 | && !$this->blur |
||
| 1306 | && !$this->convolve |
||
| 1307 | && !$this->palette |
||
| 1308 | && !$this->useQuality |
||
| 1309 | && !$this->useCompress |
||
| 1310 | && !$this->saveAs |
||
| 1311 | && !$this->rotateBefore |
||
| 1312 | && !$this->rotateAfter |
||
| 1313 | && !$this->autoRotate |
||
| 1314 | && !$this->bgColor |
||
| 1315 | && ($this->upscale === self::UPSCALE_DEFAULT) |
||
| 1316 | ) { |
||
| 1317 | $this->log("Using original image."); |
||
| 1318 | $this->output($this->pathToImage); |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | return $this; |
||
| 1322 | } |
||
| 1323 | |||
| 1324 | |||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * Generate filename to save file in cache. |
||
| 1328 | * |
||
| 1329 | * @param string $base as optional basepath for storing file. |
||
| 1330 | * @param boolean $useSubdir use or skip the subdir part when creating the |
||
| 1331 | * filename. |
||
| 1332 | * @param string $prefix to add as part of filename |
||
| 1333 | * |
||
| 1334 | * @return $this |
||
| 1335 | */ |
||
| 1336 | public function generateFilename($base = null, $useSubdir = true, $prefix = null) |
||
| 1337 | { |
||
| 1338 | $filename = basename($this->pathToImage); |
||
| 1339 | $cropToFit = $this->cropToFit ? '_cf' : null; |
||
| 1340 | $fillToFit = $this->fillToFit ? '_ff' : null; |
||
| 1341 | $crop_x = $this->crop_x ? "_x{$this->crop_x}" : null; |
||
| 1342 | $crop_y = $this->crop_y ? "_y{$this->crop_y}" : null; |
||
| 1343 | $scale = $this->scale ? "_s{$this->scale}" : null; |
||
| 1344 | $bgColor = $this->bgColor ? "_bgc{$this->bgColor}" : null; |
||
| 1345 | $quality = $this->quality ? "_q{$this->quality}" : null; |
||
| 1346 | $compress = $this->compress ? "_co{$this->compress}" : null; |
||
| 1347 | $rotateBefore = $this->rotateBefore ? "_rb{$this->rotateBefore}" : null; |
||
| 1348 | $rotateAfter = $this->rotateAfter ? "_ra{$this->rotateAfter}" : null; |
||
| 1349 | |||
| 1350 | $saveAs = $this->normalizeFileExtension(); |
||
| 1351 | $saveAs = $saveAs ? "_$saveAs" : null; |
||
| 1352 | |||
| 1353 | $copyStrat = null; |
||
| 1354 | if ($this->copyStrategy === self::RESIZE) { |
||
| 1355 | $copyStrat = "_rs"; |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | $width = $this->newWidth ? '_' . $this->newWidth : null; |
||
| 1359 | $height = $this->newHeight ? '_' . $this->newHeight : null; |
||
| 1360 | |||
| 1361 | $offset = isset($this->offset) |
||
| 1362 | ? '_o' . $this->offset['top'] . '-' . $this->offset['right'] . '-' . $this->offset['bottom'] . '-' . $this->offset['left'] |
||
| 1363 | : null; |
||
| 1364 | |||
| 1365 | $crop = $this->crop |
||
| 1366 | ? '_c' . $this->crop['width'] . '-' . $this->crop['height'] . '-' . $this->crop['start_x'] . '-' . $this->crop['start_y'] |
||
| 1367 | : null; |
||
| 1368 | |||
| 1369 | $filters = null; |
||
| 1370 | if (isset($this->filters)) { |
||
| 1371 | foreach ($this->filters as $filter) { |
||
| 1372 | if (is_array($filter)) { |
||
| 1373 | $filters .= "_f{$filter['id']}"; |
||
| 1374 | for ($i=1; $i<=$filter['argc']; $i++) { |
||
| 1375 | $filters .= "-".$filter["arg{$i}"]; |
||
| 1376 | } |
||
| 1377 | } |
||
| 1378 | } |
||
| 1379 | } |
||
| 1380 | |||
| 1381 | $sharpen = $this->sharpen ? 's' : null; |
||
| 1382 | $emboss = $this->emboss ? 'e' : null; |
||
| 1383 | $blur = $this->blur ? 'b' : null; |
||
| 1384 | $palette = $this->palette ? 'p' : null; |
||
| 1385 | |||
| 1386 | $autoRotate = $this->autoRotate ? 'ar' : null; |
||
| 1387 | |||
| 1388 | $optimize = $this->jpegOptimize ? 'o' : null; |
||
| 1389 | $optimize .= $this->pngFilter ? 'f' : null; |
||
| 1390 | $optimize .= $this->pngDeflate ? 'd' : null; |
||
| 1391 | |||
| 1392 | $convolve = null; |
||
| 1393 | if ($this->convolve) { |
||
| 1394 | $convolve = '_conv' . preg_replace('/[^a-zA-Z0-9]/', '', $this->convolve); |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | $upscale = null; |
||
| 1398 | if ($this->upscale !== self::UPSCALE_DEFAULT) { |
||
| 1399 | $upscale = '_nu'; |
||
| 1400 | } |
||
| 1401 | |||
| 1402 | $subdir = null; |
||
| 1403 | if ($useSubdir === true) { |
||
| 1404 | $subdir = str_replace('/', '-', dirname($this->imageSrc)); |
||
| 1405 | $subdir = ($subdir == '.') ? '_.' : $subdir; |
||
| 1406 | $subdir .= '_'; |
||
| 1407 | } |
||
| 1408 | |||
| 1409 | $file = $prefix . $subdir . $filename . $width . $height |
||
| 1410 | . $offset . $crop . $cropToFit . $fillToFit |
||
| 1411 | . $crop_x . $crop_y . $upscale |
||
| 1412 | . $quality . $filters . $sharpen . $emboss . $blur . $palette |
||
| 1413 | . $optimize . $compress |
||
| 1414 | . $scale . $rotateBefore . $rotateAfter . $autoRotate . $bgColor |
||
| 1415 | . $convolve . $copyStrat . $saveAs; |
||
| 1416 | |||
| 1417 | return $this->setTarget($file, $base); |
||
| 1418 | } |
||
| 1419 | |||
| 1420 | |||
| 1421 | |||
| 1422 | /** |
||
| 1423 | * Use cached version of image, if possible. |
||
| 1424 | * |
||
| 1425 | * @param boolean $useCache is default true, set to false to avoid using cached object. |
||
| 1426 | * |
||
| 1427 | * @return $this |
||
| 1428 | */ |
||
| 1429 | public function useCacheIfPossible($useCache = true) |
||
| 1454 | |||
| 1455 | |||
| 1456 | |||
| 1457 | /** |
||
| 1458 | * Load image from disk. Try to load image without verbose error message, |
||
| 1459 | * if fail, load again and display error messages. |
||
| 1460 | * |
||
| 1461 | * @param string $src of image. |
||
| 1462 | * @param string $dir as base directory where images are. |
||
| 1463 | * |
||
| 1464 | * @return $this |
||
| 1465 | * |
||
| 1466 | */ |
||
| 1467 | public function load($src = null, $dir = null) |
||
| 1505 | |||
| 1506 | |||
| 1507 | |||
| 1508 | /** |
||
| 1509 | * Get the type of PNG image. |
||
| 1510 | * |
||
| 1511 | * @param string $filename to use instead of default. |
||
| 1512 | * |
||
| 1513 | * @return int as the type of the png-image |
||
| 1514 | * |
||
| 1515 | */ |
||
| 1516 | public function getPngType($filename = null) |
||
| 1529 | |||
| 1530 | |||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * Get the type of PNG image as a verbose string. |
||
| 1534 | * |
||
| 1535 | * @param integer $type to use, default is to check the type. |
||
| 1536 | * @param string $filename to use instead of default. |
||
| 1537 | * |
||
| 1538 | * @return int as the type of the png-image |
||
| 1539 | * |
||
| 1540 | */ |
||
| 1541 | private function getPngTypeAsString($pngType = null, $filename = null) |
||
| 1581 | |||
| 1582 | |||
| 1583 | |||
| 1584 | |||
| 1585 | /** |
||
| 1586 | * Calculate number of colors in an image. |
||
| 1587 | * |
||
| 1588 | * @param resource $im the image. |
||
| 1589 | * |
||
| 1590 | * @return int |
||
| 1591 | */ |
||
| 1592 | private function colorsTotal($im) |
||
| 1610 | |||
| 1611 | |||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * Preprocess image before rezising it. |
||
| 1615 | * |
||
| 1616 | * @return $this |
||
| 1617 | */ |
||
| 1618 | public function preResize() |
||
| 1650 | |||
| 1651 | |||
| 1652 | |||
| 1653 | /** |
||
| 1654 | * Resize or resample the image while resizing. |
||
| 1655 | * |
||
| 1656 | * @param int $strategy as CImage::RESIZE or CImage::RESAMPLE |
||
| 1657 | * |
||
| 1658 | * @return $this |
||
| 1659 | */ |
||
| 1660 | public function setCopyResizeStrategy($strategy) |
||
| 1665 | |||
| 1666 | |||
| 1667 | |||
| 1668 | /** |
||
| 1669 | * Resize and or crop the image. |
||
| 1670 | * |
||
| 1671 | * @return void |
||
| 1672 | */ |
||
| 1673 | public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) |
||
| 1683 | |||
| 1684 | |||
| 1685 | |||
| 1686 | /** |
||
| 1687 | * Resize and or crop the image. |
||
| 1688 | * |
||
| 1689 | * @return $this |
||
| 1690 | */ |
||
| 1691 | public function resize() |
||
| 1692 | { |
||
| 1693 | |||
| 1694 | $this->log("### Starting to Resize()"); |
||
| 1695 | $this->log("Upscale = '$this->upscale'"); |
||
| 1696 | |||
| 1697 | // Only use a specified area of the image, $this->offset is defining the area to use |
||
| 1698 | if (isset($this->offset)) { |
||
| 1699 | |||
| 1700 | $this->log("Offset for area to use, cropping it width={$this->offset['width']}, height={$this->offset['height']}, start_x={$this->offset['left']}, start_y={$this->offset['top']}"); |
||
| 1701 | $img = $this->CreateImageKeepTransparency($this->offset['width'], $this->offset['height']); |
||
| 1702 | imagecopy($img, $this->image, 0, 0, $this->offset['left'], $this->offset['top'], $this->offset['width'], $this->offset['height']); |
||
| 1703 | $this->image = $img; |
||
| 1704 | $this->width = $this->offset['width']; |
||
| 1705 | $this->height = $this->offset['height']; |
||
| 1706 | } |
||
| 1707 | |||
| 1708 | if ($this->crop) { |
||
| 1709 | |||
| 1710 | // Do as crop, take only part of image |
||
| 1711 | $this->log("Cropping area width={$this->crop['width']}, height={$this->crop['height']}, start_x={$this->crop['start_x']}, start_y={$this->crop['start_y']}"); |
||
| 1712 | $img = $this->CreateImageKeepTransparency($this->crop['width'], $this->crop['height']); |
||
| 1713 | imagecopy($img, $this->image, 0, 0, $this->crop['start_x'], $this->crop['start_y'], $this->crop['width'], $this->crop['height']); |
||
| 1714 | $this->image = $img; |
||
| 1715 | $this->width = $this->crop['width']; |
||
| 1716 | $this->height = $this->crop['height']; |
||
| 1717 | } |
||
| 1718 | |||
| 1719 | if (!$this->upscale) { |
||
| 1720 | // Consider rewriting the no-upscale code to fit within this if-statement, |
||
| 1721 | // likely to be more readable code. |
||
| 1722 | // The code is more or leass equal in below crop-to-fit, fill-to-fit and stretch |
||
| 1723 | } |
||
| 1724 | |||
| 1725 | if ($this->cropToFit) { |
||
| 1726 | |||
| 1727 | // Resize by crop to fit |
||
| 1728 | $this->log("Resizing using strategy - Crop to fit"); |
||
| 1729 | |||
| 1730 | if (!$this->upscale |
||
| 1731 | && ($this->width < $this->newWidth || $this->height < $this->newHeight)) { |
||
| 1732 | $this->log("Resizing - smaller image, do not upscale."); |
||
| 1733 | |||
| 1734 | $posX = 0; |
||
| 1735 | $posY = 0; |
||
| 1736 | $cropX = 0; |
||
| 1737 | $cropY = 0; |
||
| 1738 | |||
| 1739 | if ($this->newWidth > $this->width) { |
||
| 1740 | $posX = round(($this->newWidth - $this->width) / 2); |
||
| 1741 | } |
||
| 1742 | if ($this->newWidth < $this->width) { |
||
| 1743 | $cropX = round(($this->width/2) - ($this->newWidth/2)); |
||
| 1744 | } |
||
| 1745 | |||
| 1746 | if ($this->newHeight > $this->height) { |
||
| 1747 | $posY = round(($this->newHeight - $this->height) / 2); |
||
| 1748 | } |
||
| 1749 | if ($this->newHeight < $this->height) { |
||
| 1750 | $cropY = round(($this->height/2) - ($this->newHeight/2)); |
||
| 1751 | } |
||
| 1752 | $this->log(" cwidth: $this->cropWidth"); |
||
| 1753 | $this->log(" cheight: $this->cropHeight"); |
||
| 1754 | $this->log(" nwidth: $this->newWidth"); |
||
| 1755 | $this->log(" nheight: $this->newHeight"); |
||
| 1756 | $this->log(" width: $this->width"); |
||
| 1757 | $this->log(" height: $this->height"); |
||
| 1758 | $this->log(" posX: $posX"); |
||
| 1759 | $this->log(" posY: $posY"); |
||
| 1760 | $this->log(" cropX: $cropX"); |
||
| 1761 | $this->log(" cropY: $cropY"); |
||
| 1762 | |||
| 1763 | $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
||
| 1764 | imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->width, $this->height); |
||
| 1765 | } else { |
||
| 1766 | $cropX = round(($this->cropWidth/2) - ($this->newWidth/2)); |
||
| 1767 | $cropY = round(($this->cropHeight/2) - ($this->newHeight/2)); |
||
| 1768 | $imgPreCrop = $this->CreateImageKeepTransparency($this->cropWidth, $this->cropHeight); |
||
| 1769 | $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
||
| 1770 | $this->imageCopyResampled($imgPreCrop, $this->image, 0, 0, 0, 0, $this->cropWidth, $this->cropHeight, $this->width, $this->height); |
||
| 1771 | imagecopy($imageResized, $imgPreCrop, 0, 0, $cropX, $cropY, $this->newWidth, $this->newHeight); |
||
| 1772 | } |
||
| 1773 | |||
| 1774 | $this->image = $imageResized; |
||
| 1775 | $this->width = $this->newWidth; |
||
| 1776 | $this->height = $this->newHeight; |
||
| 1777 | |||
| 1778 | } elseif ($this->fillToFit) { |
||
| 1779 | |||
| 1780 | // Resize by fill to fit |
||
| 1781 | $this->log("Resizing using strategy - Fill to fit"); |
||
| 1782 | |||
| 1783 | $posX = 0; |
||
| 1784 | $posY = 0; |
||
| 1785 | |||
| 1786 | $ratioOrig = $this->width / $this->height; |
||
| 1787 | $ratioNew = $this->newWidth / $this->newHeight; |
||
| 1788 | |||
| 1789 | // Check ratio for landscape or portrait |
||
| 1790 | if ($ratioOrig < $ratioNew) { |
||
| 1791 | $posX = round(($this->newWidth - $this->fillWidth) / 2); |
||
| 1792 | } else { |
||
| 1793 | $posY = round(($this->newHeight - $this->fillHeight) / 2); |
||
| 1794 | } |
||
| 1795 | |||
| 1796 | if (!$this->upscale |
||
| 1797 | && ($this->width < $this->newWidth && $this->height < $this->newHeight) |
||
| 1798 | ) { |
||
| 1799 | |||
| 1800 | $this->log("Resizing - smaller image, do not upscale."); |
||
| 1801 | $posX = round(($this->newWidth - $this->width) / 2); |
||
| 1802 | $posY = round(($this->newHeight - $this->height) / 2); |
||
| 1803 | $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
||
| 1804 | imagecopy($imageResized, $this->image, $posX, $posY, 0, 0, $this->width, $this->height); |
||
| 1805 | |||
| 1806 | } else { |
||
| 1807 | $imgPreFill = $this->CreateImageKeepTransparency($this->fillWidth, $this->fillHeight); |
||
| 1808 | $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
||
| 1809 | $this->imageCopyResampled($imgPreFill, $this->image, 0, 0, 0, 0, $this->fillWidth, $this->fillHeight, $this->width, $this->height); |
||
| 1810 | imagecopy($imageResized, $imgPreFill, $posX, $posY, 0, 0, $this->fillWidth, $this->fillHeight); |
||
| 1811 | } |
||
| 1812 | |||
| 1813 | $this->image = $imageResized; |
||
| 1814 | $this->width = $this->newWidth; |
||
| 1815 | $this->height = $this->newHeight; |
||
| 1816 | |||
| 1817 | } elseif (!($this->newWidth == $this->width && $this->newHeight == $this->height)) { |
||
| 1818 | |||
| 1819 | // Resize it |
||
| 1820 | $this->log("Resizing, new height and/or width"); |
||
| 1821 | |||
| 1822 | if (!$this->upscale |
||
| 1823 | && ($this->width < $this->newWidth || $this->height < $this->newHeight) |
||
| 1824 | ) { |
||
| 1825 | $this->log("Resizing - smaller image, do not upscale."); |
||
| 1826 | |||
| 1827 | if (!$this->keepRatio) { |
||
| 1828 | $this->log("Resizing - stretch to fit selected."); |
||
| 1829 | |||
| 1830 | $posX = 0; |
||
| 1831 | $posY = 0; |
||
| 1832 | $cropX = 0; |
||
| 1833 | $cropY = 0; |
||
| 1834 | |||
| 1835 | if ($this->newWidth > $this->width && $this->newHeight > $this->height) { |
||
| 1836 | $posX = round(($this->newWidth - $this->width) / 2); |
||
| 1837 | $posY = round(($this->newHeight - $this->height) / 2); |
||
| 1838 | } elseif ($this->newWidth > $this->width) { |
||
| 1839 | $posX = round(($this->newWidth - $this->width) / 2); |
||
| 1840 | $cropY = round(($this->height - $this->newHeight) / 2); |
||
| 1841 | } elseif ($this->newHeight > $this->height) { |
||
| 1842 | $posY = round(($this->newHeight - $this->height) / 2); |
||
| 1843 | $cropX = round(($this->width - $this->newWidth) / 2); |
||
| 1844 | } |
||
| 1845 | |||
| 1846 | $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
||
| 1847 | imagecopy($imageResized, $this->image, $posX, $posY, $cropX, $cropY, $this->width, $this->height); |
||
| 1848 | $this->image = $imageResized; |
||
| 1849 | $this->width = $this->newWidth; |
||
| 1850 | $this->height = $this->newHeight; |
||
| 1851 | } |
||
| 1852 | } else { |
||
| 1853 | $imageResized = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
||
| 1854 | $this->imageCopyResampled($imageResized, $this->image, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $this->width, $this->height); |
||
| 1855 | $this->image = $imageResized; |
||
| 1856 | $this->width = $this->newWidth; |
||
| 1857 | $this->height = $this->newHeight; |
||
| 1858 | } |
||
| 1859 | } |
||
| 1860 | |||
| 1861 | return $this; |
||
| 1862 | } |
||
| 1863 | |||
| 1864 | |||
| 1865 | |||
| 1866 | /** |
||
| 1867 | * Postprocess image after rezising image. |
||
| 1868 | * |
||
| 1869 | * @return $this |
||
| 1870 | */ |
||
| 1871 | public function postResize() |
||
| 1944 | |||
| 1945 | |||
| 1946 | |||
| 1947 | /** |
||
| 1948 | * Rotate image using angle. |
||
| 1949 | * |
||
| 1950 | * @param float $angle to rotate image. |
||
| 1951 | * @param int $anglebgColor to fill image with if needed. |
||
| 1952 | * |
||
| 1953 | * @return $this |
||
| 1954 | */ |
||
| 1955 | public function rotate($angle, $bgColor) |
||
| 1969 | |||
| 1970 | |||
| 1971 | |||
| 1972 | /** |
||
| 1973 | * Rotate image using information in EXIF. |
||
| 1974 | * |
||
| 1975 | * @return $this |
||
| 1976 | */ |
||
| 1977 | public function rotateExif() |
||
| 2012 | |||
| 2013 | |||
| 2014 | |||
| 2015 | /** |
||
| 2016 | * Convert true color image to palette image, keeping alpha. |
||
| 2017 | * http://stackoverflow.com/questions/5752514/how-to-convert-png-to-8-bit-png-using-php-gd-library |
||
| 2018 | * |
||
| 2019 | * @return void |
||
| 2020 | */ |
||
| 2021 | public function trueColorToPalette() |
||
| 2038 | |||
| 2039 | |||
| 2040 | |||
| 2041 | /** |
||
| 2042 | * Sharpen image using image convolution. |
||
| 2043 | * |
||
| 2044 | * @return $this |
||
| 2045 | */ |
||
| 2046 | public function sharpenImage() |
||
| 2051 | |||
| 2052 | |||
| 2053 | |||
| 2054 | /** |
||
| 2055 | * Emboss image using image convolution. |
||
| 2056 | * |
||
| 2057 | * @return $this |
||
| 2058 | */ |
||
| 2059 | public function embossImage() |
||
| 2064 | |||
| 2065 | |||
| 2066 | |||
| 2067 | /** |
||
| 2068 | * Blur image using image convolution. |
||
| 2069 | * |
||
| 2070 | * @return $this |
||
| 2071 | */ |
||
| 2072 | public function blurImage() |
||
| 2077 | |||
| 2078 | |||
| 2079 | |||
| 2080 | /** |
||
| 2081 | * Create convolve expression and return arguments for image convolution. |
||
| 2082 | * |
||
| 2083 | * @param string $expression constant string which evaluates to a list of |
||
| 2084 | * 11 numbers separated by komma or such a list. |
||
| 2085 | * |
||
| 2086 | * @return array as $matrix (3x3), $divisor and $offset |
||
| 2087 | */ |
||
| 2088 | public function createConvolveArguments($expression) |
||
| 2122 | |||
| 2123 | |||
| 2124 | |||
| 2125 | /** |
||
| 2126 | * Add custom expressions (or overwrite existing) for image convolution. |
||
| 2127 | * |
||
| 2128 | * @param array $options Key value array with strings to be converted |
||
| 2129 | * to convolution expressions. |
||
| 2130 | * |
||
| 2131 | * @return $this |
||
| 2132 | */ |
||
| 2133 | public function addConvolveExpressions($options) |
||
| 2138 | |||
| 2139 | |||
| 2140 | |||
| 2141 | /** |
||
| 2142 | * Image convolution. |
||
| 2143 | * |
||
| 2144 | * @param string $options A string with 11 float separated by comma. |
||
| 2145 | * |
||
| 2146 | * @return $this |
||
| 2147 | */ |
||
| 2148 | public function imageConvolution($options = null) |
||
| 2165 | |||
| 2166 | |||
| 2167 | |||
| 2168 | /** |
||
| 2169 | * Set default background color between 000000-FFFFFF or if using |
||
| 2170 | * alpha 00000000-FFFFFF7F. |
||
| 2171 | * |
||
| 2172 | * @param string $color as hex value. |
||
| 2173 | * |
||
| 2174 | * @return $this |
||
| 2175 | */ |
||
| 2176 | public function setDefaultBackgroundColor($color) |
||
| 2218 | |||
| 2219 | |||
| 2220 | |||
| 2221 | /** |
||
| 2222 | * Get the background color. |
||
| 2223 | * |
||
| 2224 | * @param resource $img the image to work with or null if using $this->image. |
||
| 2225 | * |
||
| 2226 | * @return color value or null if no background color is set. |
||
| 2227 | */ |
||
| 2228 | private function getBackgroundColor($img = null) |
||
| 2251 | |||
| 2252 | |||
| 2253 | |||
| 2254 | /** |
||
| 2255 | * Create a image and keep transparency for png and gifs. |
||
| 2256 | * |
||
| 2257 | * @param int $width of the new image. |
||
| 2258 | * @param int $height of the new image. |
||
| 2259 | * |
||
| 2260 | * @return image resource. |
||
| 2261 | */ |
||
| 2262 | private function createImageKeepTransparency($width, $height) |
||
| 2291 | |||
| 2292 | |||
| 2293 | |||
| 2294 | /** |
||
| 2295 | * Set optimizing and post-processing options. |
||
| 2296 | * |
||
| 2297 | * @param array $options with config for postprocessing with external tools. |
||
| 2298 | * |
||
| 2299 | * @return $this |
||
| 2300 | */ |
||
| 2301 | public function setPostProcessingOptions($options) |
||
| 2323 | |||
| 2324 | |||
| 2325 | |||
| 2326 | /** |
||
| 2327 | * Find out the type (file extension) for the image to be saved. |
||
| 2328 | * |
||
| 2329 | * @return string as image extension. |
||
| 2330 | */ |
||
| 2331 | protected function getTargetImageExtension() |
||
| 2340 | |||
| 2341 | |||
| 2342 | |||
| 2343 | /** |
||
| 2344 | * Save image. |
||
| 2345 | * |
||
| 2346 | * @param string $src as target filename. |
||
| 2347 | * @param string $base as base directory where to store images. |
||
| 2348 | * @param boolean $overwrite or not, default to always overwrite file. |
||
| 2349 | * |
||
| 2350 | * @return $this or false if no folder is set. |
||
| 2351 | */ |
||
| 2352 | public function save($src = null, $base = null, $overwrite = true) |
||
| 2444 | |||
| 2445 | |||
| 2446 | |||
| 2447 | /** |
||
| 2448 | * Convert image from one colorpsace/color profile to sRGB without |
||
| 2449 | * color profile. |
||
| 2450 | * |
||
| 2451 | * @param string $src of image. |
||
| 2452 | * @param string $dir as base directory where images are. |
||
| 2453 | * @param string $cache as base directory where to store images. |
||
| 2454 | * @param string $iccFile filename of colorprofile. |
||
| 2455 | * @param boolean $useCache or not, default to always use cache. |
||
| 2456 | * |
||
| 2457 | * @return string | boolean false if no conversion else the converted |
||
| 2458 | * filename. |
||
| 2459 | */ |
||
| 2460 | public function convert2sRGBColorSpace($src, $dir, $cache, $iccFile, $useCache = true) |
||
| 2512 | |||
| 2513 | |||
| 2514 | |||
| 2515 | /** |
||
| 2516 | * Create a hard link, as an alias, to the cached file. |
||
| 2517 | * |
||
| 2518 | * @param string $alias where to store the link, |
||
| 2519 | * filename without extension. |
||
| 2520 | * |
||
| 2521 | * @return $this |
||
| 2522 | */ |
||
| 2523 | public function linkToCacheFile($alias) |
||
| 2544 | |||
| 2545 | |||
| 2546 | |||
| 2547 | /** |
||
| 2548 | * Add HTTP header for output together with image. |
||
| 2549 | * |
||
| 2550 | * @param string $type the header type such as "Cache-Control" |
||
| 2551 | * @param string $value the value to use |
||
| 2552 | * |
||
| 2553 | * @return void |
||
| 2554 | */ |
||
| 2555 | public function addHTTPHeader($type, $value) |
||
| 2559 | |||
| 2560 | |||
| 2561 | |||
| 2562 | /** |
||
| 2563 | * Output image to browser using caching. |
||
| 2564 | * |
||
| 2565 | * @param string $file to read and output, default is to |
||
| 2566 | * use $this->cacheFileName |
||
| 2567 | * @param string $format set to json to output file as json |
||
| 2568 | * object with details |
||
| 2569 | * |
||
| 2570 | * @return void |
||
| 2571 | */ |
||
| 2572 | public function output($file = null, $format = null) |
||
| 2665 | |||
| 2666 | |||
| 2667 | |||
| 2668 | /** |
||
| 2669 | * Create a JSON object from the image details. |
||
| 2670 | * |
||
| 2671 | * @param string $file the file to output. |
||
| 2672 | * |
||
| 2673 | * @return string json-encoded representation of the image. |
||
| 2674 | */ |
||
| 2675 | public function json($file = null) |
||
| 2720 | |||
| 2721 | |||
| 2722 | |||
| 2723 | /** |
||
| 2724 | * Set options for creating ascii version of image. |
||
| 2725 | * |
||
| 2726 | * @param array $options empty to use default or set options to change. |
||
| 2727 | * |
||
| 2728 | * @return void. |
||
| 2729 | */ |
||
| 2730 | public function setAsciiOptions($options = array()) |
||
| 2734 | |||
| 2735 | |||
| 2736 | |||
| 2737 | /** |
||
| 2738 | * Create an ASCII version from the image details. |
||
| 2739 | * |
||
| 2740 | * @param string $file the file to output. |
||
| 2741 | * |
||
| 2742 | * @return string ASCII representation of the image. |
||
| 2743 | */ |
||
| 2744 | public function ascii($file = null) |
||
| 2752 | |||
| 2753 | |||
| 2754 | |||
| 2755 | /** |
||
| 2756 | * Log an event if verbose mode. |
||
| 2757 | * |
||
| 2758 | * @param string $message to log. |
||
| 2759 | * |
||
| 2760 | * @return this |
||
| 2761 | */ |
||
| 2762 | public function log($message) |
||
| 2770 | |||
| 2771 | |||
| 2772 | |||
| 2773 | /** |
||
| 2774 | * Do verbose output to a file. |
||
| 2775 | * |
||
| 2776 | * @param string $fileName where to write the verbose output. |
||
| 2777 | * |
||
| 2778 | * @return void |
||
| 2779 | */ |
||
| 2780 | public function setVerboseToFile($fileName) |
||
| 2785 | |||
| 2786 | |||
| 2787 | |||
| 2788 | /** |
||
| 2789 | * Do verbose output and print out the log and the actual images. |
||
| 2790 | * |
||
| 2791 | * @return void |
||
| 2792 | */ |
||
| 2793 | private function verboseOutput() |
||
| 2825 | |||
| 2826 | |||
| 2827 | |||
| 2828 | /** |
||
| 2829 | * Raise error, enables to implement a selection of error methods. |
||
| 2830 | * |
||
| 2831 | * @param string $message the error message to display. |
||
| 2832 | * |
||
| 2833 | * @return void |
||
| 2834 | * @throws Exception |
||
| 2835 | */ |
||
| 2836 | private function raiseError($message) |
||
| 2840 | } |
||
| 2841 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.