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 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 $area; |
||
343 | private $offset; |
||
344 | |||
345 | |||
346 | |||
347 | /** |
||
348 | * Calculate target dimension for image when using fill-to-fit resize strategy. |
||
349 | */ |
||
350 | private $fillWidth; |
||
351 | private $fillHeight; |
||
352 | |||
353 | |||
354 | |||
355 | /** |
||
356 | * Allow remote file download, default is to disallow remote file download. |
||
357 | */ |
||
358 | private $allowRemote = false; |
||
359 | |||
360 | |||
361 | |||
362 | /** |
||
363 | * Path to cache for remote download. |
||
364 | */ |
||
365 | private $remoteCache; |
||
366 | |||
367 | |||
368 | |||
369 | /** |
||
370 | * Pattern to recognize a remote file. |
||
371 | */ |
||
372 | //private $remotePattern = '#^[http|https]://#'; |
||
373 | private $remotePattern = '#^https?://#'; |
||
374 | |||
375 | |||
376 | |||
377 | /** |
||
378 | * Use the cache if true, set to false to ignore the cached file. |
||
379 | */ |
||
380 | private $useCache = true; |
||
381 | |||
382 | |||
383 | |||
384 | /* |
||
385 | * Set whitelist for valid hostnames from where remote source can be |
||
386 | * downloaded. |
||
387 | */ |
||
388 | private $remoteHostWhitelist = null; |
||
389 | |||
390 | |||
391 | |||
392 | /* |
||
393 | * Do verbose logging to file by setting this to a filename. |
||
394 | */ |
||
395 | private $verboseFileName = null; |
||
396 | |||
397 | |||
398 | |||
399 | /* |
||
400 | * Output to ascii can take som options as an array. |
||
401 | */ |
||
402 | private $asciiOptions = array(); |
||
403 | |||
404 | |||
405 | |||
406 | /* |
||
407 | * Image copy strategy, defaults to RESAMPLE. |
||
408 | */ |
||
409 | const RESIZE = 1; |
||
410 | const RESAMPLE = 2; |
||
411 | private $copyStrategy = null; |
||
412 | |||
413 | |||
414 | |||
415 | /* |
||
416 | * Class for image resizer. |
||
417 | */ |
||
418 | private $imageResizer = null; |
||
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 | 9 | public function __construct($imageSrc = null, $imageFolder = null, $saveFolder = null, $saveName = null) |
|
448 | { |
||
449 | 9 | $this->setSource($imageSrc, $imageFolder); |
|
450 | 9 | $this->setTarget($saveFolder, $saveName); |
|
451 | 9 | $this->imageResizer = new CImageResizer(array($this, 'log')); |
|
452 | 9 | } |
|
453 | |||
454 | |||
455 | |||
456 | |||
457 | /** |
||
458 | * Set verbose mode. |
||
459 | * |
||
460 | * @param boolean $mode true or false to enable and disable verbose mode, |
||
461 | * default is true. |
||
462 | * |
||
463 | * @return $this |
||
464 | */ |
||
465 | public function setVerbose($mode = true) |
||
466 | { |
||
467 | $this->verbose = $mode; |
||
468 | return $this; |
||
469 | } |
||
470 | |||
471 | |||
472 | |||
473 | /** |
||
474 | * Set save folder, base folder for saving cache files. |
||
475 | * |
||
476 | * @todo clean up how $this->saveFolder is used in other methods. |
||
477 | * |
||
478 | * @param string $path where to store cached files. |
||
479 | * |
||
480 | * @return $this |
||
481 | */ |
||
482 | 2 | public function setSaveFolder($path) |
|
483 | { |
||
484 | 2 | $this->saveFolder = $path; |
|
485 | 2 | return $this; |
|
486 | } |
||
487 | |||
488 | |||
489 | |||
490 | /** |
||
491 | * Use cache or not. |
||
492 | * |
||
493 | * @param boolean $use true or false to use cache. |
||
494 | * |
||
495 | * @return $this |
||
496 | */ |
||
497 | public function useCache($use = true) |
||
498 | { |
||
499 | $this->useCache = $use; |
||
500 | return $this; |
||
501 | } |
||
502 | |||
503 | |||
504 | |||
505 | /** |
||
506 | * Create and save a dummy image. Use dimensions as stated in |
||
507 | * $this->newWidth, or $width or default to 100 (same for height. |
||
508 | * |
||
509 | * @param integer $width use specified width for image dimension. |
||
510 | * @param integer $height use specified width for image dimension. |
||
511 | * |
||
512 | * @return $this |
||
513 | */ |
||
514 | 2 | public function createDummyImage($width = null, $height = null) |
|
515 | { |
||
516 | 2 | $this->newWidth = $this->newWidth ?: $width ?: 100; |
|
517 | 2 | $this->newHeight = $this->newHeight ?: $height ?: 100; |
|
518 | |||
519 | 2 | $this->image = $this->CreateImageKeepTransparency($this->newWidth, $this->newHeight); |
|
520 | |||
521 | 2 | return $this; |
|
522 | } |
||
523 | |||
524 | |||
525 | |||
526 | /** |
||
527 | * Allow or disallow remote image download. |
||
528 | * |
||
529 | * @param boolean $allow true or false to enable and disable. |
||
530 | * @param string $cache path to cache dir. |
||
531 | * @param string $pattern to use to detect if its a remote file. |
||
532 | * |
||
533 | * @return $this |
||
534 | */ |
||
535 | 2 | public function setRemoteDownload($allow, $cache, $pattern = null) |
|
536 | { |
||
537 | 2 | $this->allowRemote = $allow; |
|
538 | 2 | $this->remoteCache = $cache; |
|
539 | 2 | $this->remotePattern = is_null($pattern) ? $this->remotePattern : $pattern; |
|
540 | |||
541 | 2 | $this->log( |
|
542 | "Set remote download to: " |
||
543 | 2 | . ($this->allowRemote ? "true" : "false") |
|
544 | 2 | . " using pattern " |
|
545 | 2 | . $this->remotePattern |
|
546 | 2 | ); |
|
547 | |||
548 | 2 | return $this; |
|
549 | } |
||
550 | |||
551 | |||
552 | |||
553 | /** |
||
554 | * Check if the image resource is a remote file or not. |
||
555 | * |
||
556 | * @param string $src check if src is remote. |
||
557 | * |
||
558 | * @return boolean true if $src is a remote file, else false. |
||
559 | */ |
||
560 | 2 | public function isRemoteSource($src) |
|
561 | { |
||
562 | 2 | $remote = preg_match($this->remotePattern, $src); |
|
563 | 2 | $this->log("Detected remote image: " . ($remote ? "true" : "false")); |
|
564 | 2 | return !!$remote; |
|
565 | } |
||
566 | |||
567 | |||
568 | |||
569 | /** |
||
570 | * Set whitelist for valid hostnames from where remote source can be |
||
571 | * downloaded. |
||
572 | * |
||
573 | * @param array $whitelist with regexp hostnames to allow download from. |
||
574 | * |
||
575 | * @return $this |
||
576 | */ |
||
577 | 2 | public function setRemoteHostWhitelist($whitelist = null) |
|
578 | { |
||
579 | 2 | $this->remoteHostWhitelist = $whitelist; |
|
580 | 2 | $this->log( |
|
581 | "Setting remote host whitelist to: " |
||
582 | 2 | . (is_null($whitelist) ? "null" : print_r($whitelist, 1)) |
|
583 | 2 | ); |
|
584 | 2 | return $this; |
|
585 | } |
||
586 | |||
587 | |||
588 | |||
589 | /** |
||
590 | * Check if the hostname for the remote image, is on a whitelist, |
||
591 | * if the whitelist is defined. |
||
592 | * |
||
593 | * @param string $src the remote source. |
||
594 | * |
||
595 | * @return boolean true if hostname on $src is in the whitelist, else false. |
||
596 | */ |
||
597 | 3 | public function isRemoteSourceOnWhitelist($src) |
|
598 | { |
||
599 | 3 | if (is_null($this->remoteHostWhitelist)) { |
|
600 | 1 | $this->log("Remote host on whitelist not configured - allowing."); |
|
601 | 1 | return true; |
|
602 | } |
||
603 | |||
604 | 2 | $whitelist = new CWhitelist(); |
|
605 | 2 | $hostname = parse_url($src, PHP_URL_HOST); |
|
606 | 2 | $allow = $whitelist->check($hostname, $this->remoteHostWhitelist); |
|
|
|||
607 | |||
608 | 2 | $this->log( |
|
609 | "Remote host is on whitelist: " |
||
610 | 2 | . ($allow ? "true" : "false") |
|
611 | 2 | ); |
|
612 | 2 | return $allow; |
|
613 | } |
||
614 | |||
615 | |||
616 | |||
617 | /** |
||
618 | * Check if file extension is valid as a file extension. |
||
619 | * |
||
620 | * @param string $extension of image file. |
||
621 | * |
||
622 | * @return $this |
||
623 | */ |
||
624 | private function checkFileExtension($extension) |
||
625 | { |
||
626 | $valid = array('jpg', 'jpeg', 'png', 'gif'); |
||
627 | |||
628 | in_array(strtolower($extension), $valid) |
||
629 | or $this->raiseError('Not a valid file extension.'); |
||
630 | |||
631 | return $this; |
||
632 | } |
||
633 | |||
634 | |||
635 | |||
636 | /** |
||
637 | * Normalize the file extension. |
||
638 | * |
||
639 | * @param string $extension of image file or skip to use internal. |
||
640 | * |
||
641 | * @return string $extension as a normalized file extension. |
||
642 | */ |
||
643 | 2 | private function normalizeFileExtension($extension = null) |
|
644 | { |
||
645 | 2 | $extension = strtolower($extension ? $extension : $this->extension); |
|
646 | |||
647 | 2 | if ($extension == 'jpeg') { |
|
648 | $extension = 'jpg'; |
||
649 | } |
||
650 | |||
651 | 2 | return $extension; |
|
652 | } |
||
653 | |||
654 | |||
655 | |||
656 | /** |
||
657 | * Download a remote image and return path to its local copy. |
||
658 | * |
||
659 | * @param string $src remote path to image. |
||
660 | * |
||
661 | * @return string as path to downloaded remote source. |
||
662 | */ |
||
663 | public function downloadRemoteSource($src) |
||
664 | { |
||
665 | if (!$this->isRemoteSourceOnWhitelist($src)) { |
||
666 | throw new Exception("Hostname is not on whitelist for remote sources."); |
||
667 | } |
||
668 | |||
669 | $remote = new CRemoteImage(); |
||
670 | |||
671 | if (!is_writable($this->remoteCache)) { |
||
672 | $this->log("The remote cache is not writable."); |
||
673 | } |
||
674 | |||
675 | $remote->setCache($this->remoteCache); |
||
676 | $remote->useCache($this->useCache); |
||
677 | $src = $remote->download($src); |
||
678 | |||
679 | $this->log("Remote HTTP status: " . $remote->getStatus()); |
||
680 | $this->log("Remote item is in local cache: $src"); |
||
681 | $this->log("Remote details on cache:" . print_r($remote->getDetails(), true)); |
||
682 | |||
683 | return $src; |
||
684 | } |
||
685 | |||
686 | |||
687 | |||
688 | /** |
||
689 | * Set source file to use as image source. |
||
690 | * |
||
691 | * @param string $src of image. |
||
692 | * @param string $dir as optional base directory where images are. |
||
693 | * |
||
694 | * @return $this |
||
695 | */ |
||
696 | 9 | public function setSource($src, $dir = null) |
|
720 | |||
721 | |||
722 | |||
723 | /** |
||
724 | * Set target file. |
||
725 | * |
||
726 | * @param string $src of target image. |
||
727 | * @param string $dir as optional base directory where images are stored. |
||
728 | * Uses $this->saveFolder if null. |
||
729 | * |
||
730 | * @return $this |
||
731 | */ |
||
732 | 9 | public function setTarget($src = null, $dir = null) |
|
751 | |||
752 | |||
753 | |||
754 | /** |
||
755 | * Get filename of target file. |
||
756 | * |
||
757 | * @return Boolean|String as filename of target or false if not set. |
||
758 | */ |
||
759 | 2 | public function getTarget() |
|
763 | |||
764 | |||
765 | |||
766 | /** |
||
767 | * Set options to use when processing image. |
||
768 | * |
||
769 | * @param array $args used when processing image. |
||
770 | * |
||
771 | * @return $this |
||
772 | */ |
||
773 | public function setOptions($args) |
||
875 | |||
876 | |||
877 | |||
878 | /** |
||
879 | * Map filter name to PHP filter and id. |
||
880 | * |
||
881 | * @param string $name the name of the filter. |
||
882 | * |
||
883 | * @return array with filter settings |
||
884 | * @throws Exception |
||
885 | */ |
||
886 | private function mapFilter($name) |
||
909 | |||
910 | |||
911 | |||
912 | /** |
||
913 | * Load image details from original image file. |
||
914 | * |
||
915 | * @param string $file the file to load or null to use $this->pathToImage. |
||
916 | * |
||
917 | * @return $this |
||
918 | * @throws Exception |
||
919 | */ |
||
920 | public function loadImageDetails($file = null) |
||
944 | |||
945 | |||
946 | |||
947 | /** |
||
948 | * Init new width and height and do some sanity checks on constraints, |
||
949 | * before any processing can be done. |
||
950 | * |
||
951 | * @return $this |
||
952 | * @throws Exception |
||
953 | */ |
||
954 | public function initDimensions() |
||
963 | |||
964 | |||
965 | |||
966 | /** |
||
967 | * Calculate new width and height of image, based on settings. |
||
968 | * |
||
969 | * @return $this |
||
970 | */ |
||
971 | public function calculateNewWidthAndHeight() |
||
1127 | |||
1128 | |||
1129 | |||
1130 | /** |
||
1131 | * Re-calculate image dimensions when original image dimension has changed. |
||
1132 | * |
||
1133 | * @return $this |
||
1134 | */ |
||
1135 | public function reCalculateDimensions() |
||
1148 | |||
1149 | |||
1150 | |||
1151 | /** |
||
1152 | * Set extension for filename to save as. |
||
1153 | * |
||
1154 | * @param string $saveas extension to save image as |
||
1155 | * |
||
1156 | * @return $this |
||
1157 | */ |
||
1158 | public function setSaveAsExtension($saveAs = null) |
||
1171 | |||
1172 | |||
1173 | |||
1174 | /** |
||
1175 | * Set JPEG quality to use when saving image |
||
1176 | * |
||
1177 | * @param int $quality as the quality to set. |
||
1178 | * |
||
1179 | * @return $this |
||
1180 | */ |
||
1181 | View Code Duplication | public function setJpegQuality($quality = null) |
|
1198 | |||
1199 | |||
1200 | |||
1201 | /** |
||
1202 | * Set PNG compressen algorithm to use when saving image |
||
1203 | * |
||
1204 | * @param int $compress as the algorithm to use. |
||
1205 | * |
||
1206 | * @return $this |
||
1207 | */ |
||
1208 | View Code Duplication | public function setPngCompression($compress = null) |
|
1225 | |||
1226 | |||
1227 | |||
1228 | /** |
||
1229 | * Use original image if possible, check options which affects image processing. |
||
1230 | * |
||
1231 | * @param boolean $useOrig default is to use original if possible, else set to false. |
||
1232 | * |
||
1233 | * @return $this |
||
1234 | */ |
||
1235 | public function useOriginalIfPossible($useOrig = true) |
||
1265 | |||
1266 | |||
1267 | |||
1268 | /** |
||
1269 | * Generate filename to save file in cache. |
||
1270 | * |
||
1271 | * @param string $base as optional basepath for storing file. |
||
1272 | * @param boolean $useSubdir use or skip the subdir part when creating the |
||
1273 | * filename. |
||
1274 | * @param string $prefix to add as part of filename |
||
1275 | * |
||
1276 | * @return $this |
||
1277 | */ |
||
1278 | 2 | public function generateFilename($base = null, $useSubdir = true, $prefix = null) |
|
1362 | |||
1363 | |||
1364 | |||
1365 | /** |
||
1366 | * Use cached version of image, if possible. |
||
1367 | * |
||
1368 | * @param boolean $useCache is default true, set to false to avoid using cached object. |
||
1369 | * |
||
1370 | * @return $this |
||
1371 | */ |
||
1372 | public function useCacheIfPossible($useCache = true) |
||
1397 | |||
1398 | |||
1399 | |||
1400 | /** |
||
1401 | * Load image from disk. |
||
1402 | * |
||
1403 | * @param string $src of image. |
||
1404 | * @param string $dir as base directory where images are. |
||
1405 | * |
||
1406 | * @return $this |
||
1407 | * |
||
1408 | */ |
||
1409 | public function load($src = null, $dir = null) |
||
1447 | |||
1448 | |||
1449 | |||
1450 | /** |
||
1451 | * Get the type of PNG image. |
||
1452 | * |
||
1453 | * @param string $filename to use instead of default. |
||
1454 | * |
||
1455 | * @return int as the type of the png-image |
||
1456 | * |
||
1457 | */ |
||
1458 | public function getPngType($filename = null) |
||
1471 | |||
1472 | |||
1473 | |||
1474 | /** |
||
1475 | * Get the type of PNG image as a verbose string. |
||
1476 | * |
||
1477 | * @param integer $type to use, default is to check the type. |
||
1478 | * @param string $filename to use instead of default. |
||
1479 | * |
||
1480 | * @return int as the type of the png-image |
||
1481 | * |
||
1482 | */ |
||
1483 | private function getPngTypeAsString($pngType = null, $filename = null) |
||
1523 | |||
1524 | |||
1525 | |||
1526 | |||
1527 | /** |
||
1528 | * Calculate number of colors in an image. |
||
1529 | * |
||
1530 | * @param resource $im the image. |
||
1531 | * |
||
1532 | * @return int |
||
1533 | */ |
||
1534 | private function colorsTotal($im) |
||
1552 | |||
1553 | |||
1554 | |||
1555 | /** |
||
1556 | * Preprocess image before rezising it. |
||
1557 | * |
||
1558 | * @return $this |
||
1559 | */ |
||
1560 | public function preResize() |
||
1592 | |||
1593 | |||
1594 | |||
1595 | /** |
||
1596 | * Resize or resample the image while resizing. |
||
1597 | * |
||
1598 | * @param int $strategy as CImage::RESIZE or CImage::RESAMPLE |
||
1599 | * |
||
1600 | * @return $this |
||
1601 | */ |
||
1602 | public function setCopyResizeStrategy($strategy) |
||
1607 | |||
1608 | |||
1609 | |||
1610 | /** |
||
1611 | * Resize and or crop the image. |
||
1612 | * |
||
1613 | * @return void |
||
1614 | */ |
||
1615 | public function imageCopyResampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) |
||
1625 | |||
1626 | |||
1627 | |||
1628 | /** |
||
1629 | * Resize and or crop the image. |
||
1630 | * |
||
1631 | * @return $this |
||
1632 | */ |
||
1633 | public function resize() |
||
1823 | |||
1824 | |||
1825 | |||
1826 | /** |
||
1827 | * Postprocess image after rezising image. |
||
1828 | * |
||
1829 | * @return $this |
||
1830 | */ |
||
1831 | public function postResize() |
||
1904 | |||
1905 | |||
1906 | |||
1907 | /** |
||
1908 | * Rotate image using angle. |
||
1909 | * |
||
1910 | * @param float $angle to rotate image. |
||
1911 | * @param int $anglebgColor to fill image with if needed. |
||
1912 | * |
||
1913 | * @return $this |
||
1914 | */ |
||
1915 | public function rotate($angle, $bgColor) |
||
1929 | |||
1930 | |||
1931 | |||
1932 | /** |
||
1933 | * Rotate image using information in EXIF. |
||
1934 | * |
||
1935 | * @return $this |
||
1936 | */ |
||
1937 | public function rotateExif() |
||
1972 | |||
1973 | |||
1974 | |||
1975 | /** |
||
1976 | * Convert true color image to palette image, keeping alpha. |
||
1977 | * http://stackoverflow.com/questions/5752514/how-to-convert-png-to-8-bit-png-using-php-gd-library |
||
1978 | * |
||
1979 | * @return void |
||
1980 | */ |
||
1981 | public function trueColorToPalette() |
||
1998 | |||
1999 | |||
2000 | |||
2001 | /** |
||
2002 | * Sharpen image using image convolution. |
||
2003 | * |
||
2004 | * @return $this |
||
2005 | */ |
||
2006 | public function sharpenImage() |
||
2011 | |||
2012 | |||
2013 | |||
2014 | /** |
||
2015 | * Emboss image using image convolution. |
||
2016 | * |
||
2017 | * @return $this |
||
2018 | */ |
||
2019 | public function embossImage() |
||
2024 | |||
2025 | |||
2026 | |||
2027 | /** |
||
2028 | * Blur image using image convolution. |
||
2029 | * |
||
2030 | * @return $this |
||
2031 | */ |
||
2032 | public function blurImage() |
||
2037 | |||
2038 | |||
2039 | |||
2040 | /** |
||
2041 | * Create convolve expression and return arguments for image convolution. |
||
2042 | * |
||
2043 | * @param string $expression constant string which evaluates to a list of |
||
2044 | * 11 numbers separated by komma or such a list. |
||
2045 | * |
||
2046 | * @return array as $matrix (3x3), $divisor and $offset |
||
2047 | */ |
||
2048 | public function createConvolveArguments($expression) |
||
2082 | |||
2083 | |||
2084 | |||
2085 | /** |
||
2086 | * Add custom expressions (or overwrite existing) for image convolution. |
||
2087 | * |
||
2088 | * @param array $options Key value array with strings to be converted |
||
2089 | * to convolution expressions. |
||
2090 | * |
||
2091 | * @return $this |
||
2092 | */ |
||
2093 | public function addConvolveExpressions($options) |
||
2098 | |||
2099 | |||
2100 | |||
2101 | /** |
||
2102 | * Image convolution. |
||
2103 | * |
||
2104 | * @param string $options A string with 11 float separated by comma. |
||
2105 | * |
||
2106 | * @return $this |
||
2107 | */ |
||
2108 | public function imageConvolution($options = null) |
||
2125 | |||
2126 | |||
2127 | |||
2128 | /** |
||
2129 | * Set default background color between 000000-FFFFFF or if using |
||
2130 | * alpha 00000000-FFFFFF7F. |
||
2131 | * |
||
2132 | * @param string $color as hex value. |
||
2133 | * |
||
2134 | * @return $this |
||
2135 | */ |
||
2136 | public function setDefaultBackgroundColor($color) |
||
2178 | |||
2179 | |||
2180 | |||
2181 | /** |
||
2182 | * Get the background color. |
||
2183 | * |
||
2184 | * @param resource $img the image to work with or null if using $this->image. |
||
2185 | * |
||
2186 | * @return color value or null if no background color is set. |
||
2187 | */ |
||
2188 | 2 | private function getBackgroundColor($img = null) |
|
2211 | |||
2212 | |||
2213 | |||
2214 | /** |
||
2215 | * Create a image and keep transparency for png and gifs. |
||
2216 | * |
||
2217 | * @param int $width of the new image. |
||
2218 | * @param int $height of the new image. |
||
2219 | * |
||
2220 | * @return image resource. |
||
2221 | */ |
||
2222 | 2 | private function createImageKeepTransparency($width, $height) |
|
2251 | |||
2252 | |||
2253 | |||
2254 | /** |
||
2255 | * Set optimizing and post-processing options. |
||
2256 | * |
||
2257 | * @param array $options with config for postprocessing with external tools. |
||
2258 | * |
||
2259 | * @return $this |
||
2260 | */ |
||
2261 | public function setPostProcessingOptions($options) |
||
2283 | |||
2284 | |||
2285 | |||
2286 | /** |
||
2287 | * Find out the type (file extension) for the image to be saved. |
||
2288 | * |
||
2289 | * @return string as image extension. |
||
2290 | */ |
||
2291 | 2 | protected function getTargetImageExtension() |
|
2300 | |||
2301 | |||
2302 | |||
2303 | /** |
||
2304 | * Save image. |
||
2305 | * |
||
2306 | * @param string $src as target filename. |
||
2307 | * @param string $base as base directory where to store images. |
||
2308 | * @param boolean $overwrite or not, default to always overwrite file. |
||
2309 | * |
||
2310 | * @return $this or false if no folder is set. |
||
2311 | */ |
||
2312 | 2 | public function save($src = null, $base = null, $overwrite = true) |
|
2404 | |||
2405 | |||
2406 | |||
2407 | /** |
||
2408 | * Convert image from one colorpsace/color profile to sRGB without |
||
2409 | * color profile. |
||
2410 | * |
||
2411 | * @param string $src of image. |
||
2412 | * @param string $dir as base directory where images are. |
||
2413 | * @param string $cache as base directory where to store images. |
||
2414 | * @param string $iccFile filename of colorprofile. |
||
2415 | * @param boolean $useCache or not, default to always use cache. |
||
2416 | * |
||
2417 | * @return string | boolean false if no conversion else the converted |
||
2418 | * filename. |
||
2419 | */ |
||
2420 | 2 | public function convert2sRGBColorSpace($src, $dir, $cache, $iccFile, $useCache = true) |
|
2472 | |||
2473 | |||
2474 | |||
2475 | /** |
||
2476 | * Create a hard link, as an alias, to the cached file. |
||
2477 | * |
||
2478 | * @param string $alias where to store the link, |
||
2479 | * filename without extension. |
||
2480 | * |
||
2481 | * @return $this |
||
2482 | */ |
||
2483 | public function linkToCacheFile($alias) |
||
2504 | |||
2505 | |||
2506 | |||
2507 | /** |
||
2508 | * Add HTTP header for outputting together with image. |
||
2509 | * |
||
2510 | * @param string $type the header type such as "Cache-Control" |
||
2511 | * @param string $value the value to use |
||
2512 | * |
||
2513 | * @return void |
||
2514 | */ |
||
2515 | public function addHTTPHeader($type, $value) |
||
2519 | |||
2520 | |||
2521 | |||
2522 | /** |
||
2523 | * Output image to browser using caching. |
||
2524 | * |
||
2525 | * @param string $file to read and output, default is to |
||
2526 | * use $this->cacheFileName |
||
2527 | * @param string $format set to json to output file as json |
||
2528 | * object with details |
||
2529 | * |
||
2530 | * @return void |
||
2531 | */ |
||
2532 | public function output($file = null, $format = null) |
||
2605 | |||
2606 | |||
2607 | |||
2608 | /** |
||
2609 | * Create a JSON object from the image details. |
||
2610 | * |
||
2611 | * @param string $file the file to output. |
||
2612 | * |
||
2613 | * @return string json-encoded representation of the image. |
||
2614 | */ |
||
2615 | public function json($file = null) |
||
2660 | |||
2661 | |||
2662 | |||
2663 | /** |
||
2664 | * Set options for creating ascii version of image. |
||
2665 | * |
||
2666 | * @param array $options empty to use default or set options to change. |
||
2667 | * |
||
2668 | * @return void. |
||
2669 | */ |
||
2670 | public function setAsciiOptions($options = array()) |
||
2674 | |||
2675 | |||
2676 | |||
2677 | /** |
||
2678 | * Create an ASCII version from the image details. |
||
2679 | * |
||
2680 | * @param string $file the file to output. |
||
2681 | * |
||
2682 | * @return string ASCII representation of the image. |
||
2683 | */ |
||
2684 | public function ascii($file = null) |
||
2692 | |||
2693 | |||
2694 | |||
2695 | /** |
||
2696 | * Log an event if verbose mode. |
||
2697 | * |
||
2698 | * @param string $message to log. |
||
2699 | * |
||
2700 | * @return this |
||
2701 | */ |
||
2702 | 9 | public function log($message) |
|
2710 | |||
2711 | |||
2712 | |||
2713 | /** |
||
2714 | * Do verbose output to a file. |
||
2715 | * |
||
2716 | * @param string $fileName where to write the verbose output. |
||
2717 | * |
||
2718 | * @return void |
||
2719 | */ |
||
2720 | public function setVerboseToFile($fileName) |
||
2725 | |||
2726 | |||
2727 | |||
2728 | /** |
||
2729 | * Do verbose output and print out the log and the actual images. |
||
2730 | * |
||
2731 | * @return void |
||
2732 | */ |
||
2733 | private function verboseOutput() |
||
2765 | |||
2766 | |||
2767 | |||
2768 | /** |
||
2769 | * Raise error, enables to implement a selection of error methods. |
||
2770 | * |
||
2771 | * @param string $message the error message to display. |
||
2772 | * |
||
2773 | * @return void |
||
2774 | * @throws Exception |
||
2775 | */ |
||
2776 | private function raiseError($message) |
||
2780 | } |
||
2781 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.