Total Complexity | 153 |
Total Lines | 1258 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Image_Transform often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image_Transform, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
83 | class Image_Transform |
||
84 | { |
||
85 | /** |
||
86 | * Name of the image file |
||
87 | * @var string |
||
88 | */ |
||
89 | public $image = ''; |
||
90 | |||
91 | /** |
||
92 | * Type of the image file (eg. jpg, gif png ...) |
||
93 | * @var string |
||
94 | */ |
||
95 | public $type = ''; |
||
96 | |||
97 | /** |
||
98 | * Original image width in x direction |
||
99 | * @var int |
||
100 | */ |
||
101 | public $img_x = ''; |
||
102 | |||
103 | /** |
||
104 | * Original image width in y direction |
||
105 | * @var int |
||
106 | */ |
||
107 | public $img_y = ''; |
||
108 | |||
109 | /** |
||
110 | * New image width in x direction |
||
111 | * @var int |
||
112 | */ |
||
113 | public $new_x = ''; |
||
114 | |||
115 | /** |
||
116 | * New image width in y direction |
||
117 | * @var int |
||
118 | */ |
||
119 | public $new_y = ''; |
||
120 | |||
121 | /** |
||
122 | * Path to the library used |
||
123 | * e.g. /usr/local/ImageMagick/bin/ or |
||
124 | * /usr/local/netpbm/ |
||
125 | */ |
||
126 | public $lib_path = ''; |
||
127 | |||
128 | /** |
||
129 | * Flag to warn if image has been resized more than once before displaying |
||
130 | * or saving. |
||
131 | */ |
||
132 | public $resized = false; |
||
133 | |||
134 | /** |
||
135 | * @var array General options |
||
136 | * @access protected |
||
137 | */ |
||
138 | public $_options = array( |
||
139 | 'quality' => 75, |
||
140 | 'scaleMethod' => 'smooth', |
||
141 | 'canvasColor' => array(255, 255, 255), |
||
142 | 'pencilColor' => array(0, 0, 0), |
||
143 | 'textColor' => array(0, 0, 0) |
||
144 | ); |
||
145 | |||
146 | /** |
||
147 | * Flag for whether settings should be discarded on saving/display of image |
||
148 | * @var bool |
||
149 | * @see Image_Transform::keepSettingsOnSave |
||
150 | */ |
||
151 | public $keep_settings_on_save = false; |
||
152 | |||
153 | /** |
||
154 | * Supported image types |
||
155 | * @var array |
||
156 | * @access protected |
||
157 | */ |
||
158 | public $_supported_image_types = array(); |
||
159 | |||
160 | /** |
||
161 | * Initialization error tracking |
||
162 | * @var object |
||
163 | * @access private |
||
164 | **/ |
||
165 | public $_error = null; |
||
166 | |||
167 | /** |
||
168 | * associative array that tracks existence of programs |
||
169 | * (for drivers using shell interface and a tiny performance |
||
170 | * improvement if the clearstatcache() is used) |
||
171 | * @var array |
||
172 | * @access protected |
||
173 | */ |
||
174 | public $_programs = array(); |
||
175 | |||
176 | /** |
||
177 | * Default parameters used in the addText methods. |
||
178 | */ |
||
179 | public $default_text_params = array( |
||
180 | 'text' => 'Default text', |
||
181 | 'x' => 10, |
||
182 | 'y' => 20, |
||
183 | 'color' => 'red', |
||
184 | 'font' => 'Arial.ttf', |
||
185 | 'size' => 12, |
||
186 | 'angle' => 0, |
||
187 | 'resize_first' => false |
||
188 | ); |
||
189 | |||
190 | /** |
||
191 | * Creates a new Image_Transform object |
||
192 | * |
||
193 | * @param string $driver name of driver class to initialize. If no driver |
||
194 | * is specified the factory will attempt to use 'Imagick' first |
||
195 | * then 'GD' second, then 'Imlib' last |
||
196 | * |
||
197 | * @return object an Image_Transform object, or PEAR_Error on error |
||
198 | * |
||
199 | * @see PEAR::isError() |
||
200 | * @see Image_Transform::setOption() |
||
201 | */ |
||
202 | public function &factory($driver = '') |
||
203 | { |
||
204 | if ('' == $driver) { |
||
205 | $extensions = array( |
||
206 | 'imagick' => 'Imagick3', |
||
207 | 'gd' => 'GD', |
||
208 | 'imlib' => 'Imlib' |
||
209 | ); |
||
210 | if (version_compare(PHP_VERSION, '5.0.0', '<')) { |
||
211 | //Imagick2 driver for php < 5 |
||
212 | $extensions['imagick'] = 'Imagick2'; |
||
213 | } |
||
214 | |||
215 | foreach ($extensions as $ext => $ext_driver) { |
||
216 | if (PEAR::loadExtension($ext)) { |
||
217 | $driver = $ext_driver; |
||
218 | break; |
||
219 | } |
||
220 | } |
||
221 | if (!$driver) { |
||
222 | return PEAR::raiseError('No image library specified and none can be found.' . ' You must specify driver in factory() call.', IMAGE_TRANSFORM_ERROR_ARGUMENT); |
||
223 | } |
||
224 | } else { |
||
225 | switch (strtolower($driver)) { |
||
226 | case 'gd': |
||
227 | $driver = 'GD'; |
||
228 | break; |
||
229 | case 'imagick': |
||
230 | $driver = 'Imagick3'; |
||
231 | if (version_compare(PHP_VERSION, '5.0.0', '<')) { |
||
232 | $driver = 'Imagick2'; |
||
233 | } |
||
234 | break; |
||
235 | case 'imlib': |
||
236 | $driver = 'Imlib'; |
||
237 | break; |
||
238 | } |
||
239 | } |
||
240 | |||
241 | // $file = 'Image/Transform/Driver/' . $driver . '.php'; |
||
242 | $file = __DIR__ . '/Transform/Driver/' . $driver . '.php'; |
||
243 | if (!@fclose(@fopen($file, 'r', true))) { |
||
244 | return PEAR::raiseError('Driver failed to load file ' . $file, IMAGE_TRANSFORM_DRIVER_FILE_MISSING); |
||
245 | } |
||
246 | |||
247 | $classname = 'Image_Transform_Driver_' . $driver; |
||
248 | |||
249 | if (!class_exists($classname)) { |
||
250 | require_once $file; |
||
251 | |||
252 | if (!class_exists($classname)) { |
||
253 | return PEAR::raiseError('Image library ' . $driver . ' not supported... aborting.', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
254 | } |
||
255 | } |
||
256 | $obj = new $classname(); |
||
257 | |||
258 | // Check startup error |
||
259 | if ($error =& $obj->isError()) { |
||
260 | $obj =& $error; |
||
261 | } |
||
262 | |||
263 | return $obj; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Returns/sets an error when the instance couldn't initialize properly |
||
268 | * |
||
269 | * @param object $error PEAR_Error object when setting an error |
||
270 | * |
||
271 | * @return mixed FALSE or PEAR_Error object |
||
272 | * @access protected |
||
273 | */ |
||
274 | public function &isError($error = null) |
||
275 | { |
||
276 | if (!is_null($error)) { |
||
277 | $this->_error =& $error; |
||
278 | } |
||
279 | |||
280 | return $this->_error; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Resizes the image in the X and/or Y direction(s) |
||
285 | * |
||
286 | * If either is 0 it will keep the original size for that dimension |
||
287 | * |
||
288 | * @param mixed $new_x (0, number, percentage 10% or 0.1) |
||
289 | * @param mixed $new_y (0, number, percentage 10% or 0.1) |
||
290 | * @param array $options Options |
||
291 | * |
||
292 | * @return mixed TRUE or PEAR_Error object on error |
||
293 | * @access public |
||
294 | */ |
||
295 | public function resize($new_x = 0, $new_y = 0, $options = null) |
||
296 | { |
||
297 | // 0 means keep original size |
||
298 | $new_x = (0 == $new_x) ? $this->img_x : $this->_parse_size($new_x, $this->img_x); |
||
299 | $new_y = (0 == $new_y) ? $this->img_y : $this->_parse_size($new_y, $this->img_y); |
||
300 | |||
301 | // Now do the library specific resizing. |
||
302 | return $this->_resize($new_x, $new_y, $options); |
||
303 | } // End resize |
||
304 | |||
305 | /** |
||
306 | * Scales the image to the specified width |
||
307 | * |
||
308 | * This method preserves the aspect ratio |
||
309 | * |
||
310 | * @param int $new_x Size to scale X-dimension to |
||
311 | * |
||
312 | * @return mixed TRUE or PEAR_Error object on error |
||
313 | * @access public |
||
314 | */ |
||
315 | public function scaleByX($new_x) |
||
316 | { |
||
317 | if ($new_x <= 0) { |
||
318 | return PEAR::raiseError('New size must be strictly positive', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
||
319 | } |
||
320 | $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); |
||
321 | |||
322 | return $this->_resize(max(1, $new_x), max(1, $new_y)); |
||
323 | } // End scaleByX |
||
324 | |||
325 | /** |
||
326 | * Alias for resize() |
||
327 | * |
||
328 | * @see resize() |
||
329 | */ |
||
330 | public function scaleByXY($new_x = 0, $new_y = 0, $options = null) |
||
331 | { |
||
332 | return $this->resize($new_x, $new_y, $options); |
||
333 | } // End scaleByXY |
||
334 | |||
335 | /** |
||
336 | * Scales the image to the specified height. |
||
337 | * |
||
338 | * This method preserves the aspect ratio |
||
339 | * |
||
340 | * @param int $new_y Size to scale Y-dimension to |
||
341 | * |
||
342 | * @return mixed TRUE or PEAR_Error object on error |
||
343 | * @access public |
||
344 | */ |
||
345 | public function scaleByY($new_y) |
||
346 | { |
||
347 | if ($new_y <= 0) { |
||
348 | return PEAR::raiseError('New size must be strictly positive', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
||
349 | } |
||
350 | $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); |
||
351 | |||
352 | return $this->_resize(max(1, $new_x), max(1, $new_y)); |
||
353 | } // End scaleByY |
||
354 | |||
355 | /** |
||
356 | * Scales an image by a percentage, factor or a given length |
||
357 | * |
||
358 | * This method preserves the aspect ratio |
||
359 | * |
||
360 | * @param mixed $size (number, percentage 10% or 0.1) |
||
361 | * |
||
362 | * @return mixed TRUE or PEAR_Error object on error |
||
363 | * @access public |
||
364 | * @see scaleByPercentage, scaleByFactor, scaleByLength |
||
365 | */ |
||
366 | public function scale($size) |
||
367 | { |
||
368 | if ((strlen($size) > 1) && ('%' == substr($size, -1))) { |
||
369 | return $this->scaleByPercentage(substr($size, 0, -1)); |
||
370 | } elseif ($size < 1) { |
||
371 | return $this->scaleByFactor($size); |
||
372 | } else { |
||
373 | return $this->scaleByLength($size); |
||
374 | } |
||
375 | } // End scale |
||
376 | |||
377 | /** |
||
378 | * Scales an image to a percentage of its original size. For example, if |
||
379 | * my image was 640x480 and I called scaleByPercentage(10) then the image |
||
380 | * would be resized to 64x48 |
||
381 | * |
||
382 | * @param int $size Percentage of original size to scale to |
||
383 | * |
||
384 | * @return mixed TRUE or PEAR_Error object on error |
||
385 | * @access public |
||
386 | */ |
||
387 | public function scaleByPercentage($size) |
||
388 | { |
||
389 | return $this->scaleByFactor($size / 100); |
||
390 | } // End scaleByPercentage |
||
391 | |||
392 | /** |
||
393 | * Scales an image to a factor of its original size. For example, if |
||
394 | * my image was 640x480 and I called scaleByFactor(0.5) then the image |
||
395 | * would be resized to 320x240. |
||
396 | * |
||
397 | * @param float $size Factor of original size to scale to |
||
398 | * |
||
399 | * @return mixed TRUE or PEAR_Error object on error |
||
400 | * @access public |
||
401 | */ |
||
402 | public function scaleByFactor($size) |
||
403 | { |
||
404 | if ($size <= 0) { |
||
405 | return PEAR::raiseError('New size must be strictly positive', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
||
406 | } |
||
407 | $new_x = round($size * $this->img_x, 0); |
||
408 | $new_y = round($size * $this->img_y, 0); |
||
409 | |||
410 | return $this->_resize(max(1, $new_x), max(1, $new_y)); |
||
411 | } // End scaleByFactor |
||
412 | |||
413 | /** |
||
414 | * Scales an image so that the longest side has the specified dimension. |
||
415 | * |
||
416 | * This method preserves the aspect ratio |
||
417 | * |
||
418 | * @param int $size Max dimension in pixels |
||
419 | * |
||
420 | * @return mixed TRUE or PEAR_Error object on error |
||
421 | * @access public |
||
422 | */ |
||
423 | public function scaleMaxLength($size) |
||
424 | { |
||
425 | if ($size <= 0) { |
||
426 | return PEAR::raiseError('New size must be strictly positive', IMAGE_TRANSFORM_ERROR_OUTOFBOUND); |
||
427 | } |
||
428 | if ($this->img_x >= $this->img_y) { |
||
429 | $new_x = $size; |
||
430 | $new_y = round(($new_x / $this->img_x) * $this->img_y, 0); |
||
431 | } else { |
||
432 | $new_y = $size; |
||
433 | $new_x = round(($new_y / $this->img_y) * $this->img_x, 0); |
||
434 | } |
||
435 | |||
436 | return $this->_resize(max(1, $new_x), max(1, $new_y)); |
||
437 | } // End scaleMaxLength |
||
438 | |||
439 | /** |
||
440 | * Alias for scaleMaxLength |
||
441 | * |
||
442 | * @param int $size Max dimension in pixels |
||
443 | * |
||
444 | * @return mixed TRUE or PEAR_Error object on error |
||
445 | * @access public |
||
446 | * @see scaleMaxLength() |
||
447 | */ |
||
448 | public function scaleByLength($size) |
||
449 | { |
||
450 | return $this->scaleMaxLength($size); |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * Fits the image in the specified box size |
||
455 | * |
||
456 | * If the image is bigger than the box specified by $width and $height, |
||
457 | * it will be scaled down to fit inside of it. |
||
458 | * If the image is smaller, nothing is done. |
||
459 | * |
||
460 | * @param integer $width Width of the box in pixels |
||
461 | * @param integer $height Height of the box in pixels |
||
462 | * |
||
463 | * @return bool|PEAR_Error TRUE or PEAR_Error object on error |
||
464 | * @access public |
||
465 | */ |
||
466 | public function fit($width, $height) |
||
479 | } |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * This works as per fit, but creates the canvas of size $width x $height |
||
484 | * and positions the resized image on it, by default in the centre. |
||
485 | * |
||
486 | * @param unknown_type $width |
||
487 | * @param unknown_type $height |
||
488 | * @param unknown_type $posn |
||
489 | * |
||
490 | * @return unknown |
||
491 | */ |
||
492 | public function fitOnCanvas($width, $height, $posn = 'center') |
||
493 | { |
||
494 | return PEAR::raiseError('fitOnCanvas() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * Fits the image in the specified width |
||
499 | * |
||
500 | * If the image is wider than the width specified by $width, |
||
501 | * it will be scaled down to fit inside of it. |
||
502 | * If the image is smaller, nothing is done. |
||
503 | * |
||
504 | * @param integer $width Maximum width in pixels |
||
505 | * |
||
506 | * @return bool|PEAR_Error TRUE or PEAR_Error object on error |
||
507 | * @access public |
||
508 | */ |
||
509 | public function fitX($width) |
||
510 | { |
||
511 | return ($this->img_x <= $width) ? true : $this->scaleByX($width); |
||
512 | } |
||
513 | |||
514 | /** |
||
515 | * Fits the image in the specified height |
||
516 | * |
||
517 | * If the image is taller than the height specified by $height, |
||
518 | * it will be scaled down to fit inside of it. |
||
519 | * If the image is smaller, nothing is done. |
||
520 | * |
||
521 | * @param integer $height Maximum height in pixels |
||
522 | * |
||
523 | * @return bool|PEAR_Error TRUE or PEAR_Error object on error |
||
524 | * @access public |
||
525 | */ |
||
526 | public function fitY($height) |
||
527 | { |
||
528 | return ($this->img_y <= $height) ? true : $this->scaleByY($height); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Sets one options |
||
533 | * |
||
534 | * @param string $name Name of option |
||
535 | * @param mixed $value Value of option |
||
536 | * |
||
537 | * @return void |
||
538 | * @access public |
||
539 | * @see setOptions() |
||
540 | */ |
||
541 | public function setOption($name, $value) |
||
542 | { |
||
543 | $this->_options[$name] = $value; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * Sets multiple options at once |
||
548 | * |
||
549 | * Associative array of options: |
||
550 | * - quality (Integer: 0: poor - 100: best) |
||
551 | * - scaleMethod ('smooth', 'pixel') |
||
552 | * |
||
553 | * @param array $options Array of options |
||
554 | * |
||
555 | * @return void |
||
556 | * @access public |
||
557 | */ |
||
558 | public function setOptions($options) |
||
559 | { |
||
560 | $this->_options = array_merge($this->_options, $options); |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Sets the image type (in lowercase letters), the image height and width. |
||
565 | * |
||
566 | * @param string $image Image filename |
||
567 | * |
||
568 | * @return mixed TRUE or PEAR_error |
||
569 | * @access protected |
||
570 | * @see PHP_Compat::image_type_to_mime_type() |
||
571 | * @link http://php.net/getimagesize |
||
572 | */ |
||
573 | public function _get_image_details($image) |
||
574 | { |
||
575 | $data = @getimagesize($image); |
||
576 | // 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, |
||
577 | // 7 = TIFF (intel byte order), 8 = TIFF (motorola byte order), |
||
578 | // 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, |
||
579 | // 15 = WBMP, 16 = XBM |
||
580 | if (!is_array($data)) { |
||
581 | return PEAR::raiseError("Cannot fetch image or images details.", true); |
||
582 | } |
||
583 | |||
584 | switch ($data[2]) { |
||
585 | case IMAGETYPE_GIF: |
||
586 | $type = 'gif'; |
||
587 | break; |
||
588 | case IMAGETYPE_JPEG: |
||
589 | $type = 'jpeg'; |
||
590 | break; |
||
591 | case IMAGETYPE_PNG: |
||
592 | $type = 'png'; |
||
593 | break; |
||
594 | case IMAGETYPE_SWF: |
||
595 | $type = 'swf'; |
||
596 | break; |
||
597 | case IMAGETYPE_PSD: |
||
598 | $type = 'psd'; |
||
599 | break; |
||
600 | case IMAGETYPE_BMP: |
||
601 | $type = 'bmp'; |
||
602 | break; |
||
603 | case IMAGETYPE_TIFF_II: |
||
604 | case IMAGETYPE_TIFF_MM: |
||
605 | $type = 'tiff'; |
||
606 | break; |
||
607 | case IMAGETYPE_JPC: |
||
608 | $type = 'jpc'; |
||
609 | break; |
||
610 | case IMAGETYPE_JP2: |
||
611 | $type = 'jp2'; |
||
612 | break; |
||
613 | case IMAGETYPE_JPX: |
||
614 | $type = 'jpx'; |
||
615 | break; |
||
616 | case IMAGETYPE_JB2: |
||
617 | $type = 'jb2'; |
||
618 | break; |
||
619 | case IMAGETYPE_SWC: |
||
620 | $type = 'swc'; |
||
621 | break; |
||
622 | case IMAGETYPE_IFF: |
||
623 | $type = 'iff'; |
||
624 | break; |
||
625 | case IMAGETYPE_WBMP: |
||
626 | $type = 'wbmp'; |
||
627 | break; |
||
628 | case IMAGETYPE_XBM: |
||
629 | $type = 'xbm'; |
||
630 | break; |
||
631 | default: |
||
632 | return PEAR::raiseError("Cannot recognize image format", IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
633 | } |
||
634 | $this->img_x = $this->new_x = $data[0]; |
||
635 | $this->img_y = $this->new_y = $data[1]; |
||
636 | $this->type = $type; |
||
637 | |||
638 | return true; |
||
639 | } |
||
640 | |||
641 | /** |
||
642 | * Returns the matching IMAGETYPE_* constant for a given image type |
||
643 | * |
||
644 | * @param mixed $type String (GIF, JPG,...) |
||
645 | * |
||
646 | * @return mixed string or integer or input on error |
||
647 | * @access protected |
||
648 | * @see PHP_Compat::image_type_to_mime_type() |
||
649 | **/ |
||
650 | public function _convert_image_type($type) |
||
651 | { |
||
652 | switch (strtolower($type)) { |
||
653 | case 'gif': |
||
654 | return IMAGETYPE_GIF; |
||
655 | case 'jpeg': |
||
656 | case 'jpg': |
||
657 | return IMAGETYPE_JPEG; |
||
658 | case 'png': |
||
659 | return IMAGETYPE_PNG; |
||
660 | case 'swf': |
||
661 | return IMAGETYPE_SWF; |
||
662 | case 'psd': |
||
663 | return IMAGETYPE_PSD; |
||
664 | case 'bmp': |
||
665 | return IMAGETYPE_BMP; |
||
666 | case 'tiff': |
||
667 | return IMAGETYPE_TIFF_II; |
||
668 | //IMAGETYPE_TIFF_MM; |
||
669 | case 'jpc': |
||
670 | return IMAGETYPE_JPC; |
||
671 | case 'jp2': |
||
672 | return IMAGETYPE_JP2; |
||
673 | case 'jpx': |
||
674 | return IMAGETYPE_JPX; |
||
675 | case 'jb2': |
||
676 | return IMAGETYPE_JB2; |
||
677 | case 'swc': |
||
678 | return IMAGETYPE_SWC; |
||
679 | case 'iff': |
||
680 | return IMAGETYPE_IFF; |
||
681 | case 'wbmp': |
||
682 | return IMAGETYPE_WBMP; |
||
683 | case 'xbm': |
||
684 | return IMAGETYPE_XBM; |
||
685 | default: |
||
686 | return $type; |
||
687 | } |
||
688 | |||
689 | return (isset($types[$t = strtolower($type)])) ? $types[$t] : $type; |
||
690 | } |
||
691 | |||
692 | /** |
||
693 | * Parses input for number format and convert |
||
694 | * |
||
695 | * If either parameter is 0 it will be scaled proportionally |
||
696 | * |
||
697 | * @param mixed $new_size (0, number, percentage 10% or 0.1) |
||
698 | * @param int $old_size |
||
699 | * |
||
700 | * @return mixed Integer or PEAR_error |
||
701 | * @access protected |
||
702 | */ |
||
703 | public function _parse_size($new_size, $old_size) |
||
704 | { |
||
705 | if ('%' == substr($new_size, -1)) { |
||
706 | $new_size = substr($new_size, 0, -1); |
||
707 | $new_size = $new_size / 100; |
||
708 | } |
||
709 | if ($new_size > 1) { |
||
710 | return (int)$new_size; |
||
711 | } elseif (0 == $new_size) { |
||
712 | return (int)$old_size; |
||
713 | } else { |
||
714 | return (int)round($new_size * $old_size, 0); |
||
715 | } |
||
716 | } |
||
717 | |||
718 | /** |
||
719 | * Returns an angle between 0 and 360 from any angle value |
||
720 | * |
||
721 | * @param float $angle The angle to normalize |
||
722 | * |
||
723 | * @return float the angle |
||
724 | * @access protected |
||
725 | */ |
||
726 | public function _rotation_angle($angle) |
||
727 | { |
||
728 | $angle %= 360; |
||
729 | |||
730 | return ($angle < 0) ? $angle + 360 : $angle; |
||
731 | } |
||
732 | |||
733 | /** |
||
734 | * Returns the current value of $this->default_text_params. |
||
735 | * |
||
736 | * @return array $this->default_text_params The current text parameters |
||
737 | * @access protected |
||
738 | */ |
||
739 | public function _get_default_text_params() |
||
740 | { |
||
741 | return $this->default_text_params; |
||
742 | } |
||
743 | |||
744 | /** |
||
745 | * Sets the image width |
||
746 | * |
||
747 | * @param int $size dimension to set |
||
748 | * |
||
749 | * @return void |
||
750 | * @access protected |
||
751 | * @since 29/05/02 13:36:31 |
||
752 | */ |
||
753 | public function _set_img_x($size) |
||
754 | { |
||
755 | $this->img_x = $size; |
||
756 | } |
||
757 | |||
758 | /** |
||
759 | * Sets the image height |
||
760 | * |
||
761 | * @param int $size dimension to set |
||
762 | * |
||
763 | * @return void |
||
764 | * @access protected |
||
765 | * @since 29/05/02 13:36:31 |
||
766 | */ |
||
767 | public function _set_img_y($size) |
||
768 | { |
||
769 | $this->img_y = $size; |
||
770 | } |
||
771 | |||
772 | /** |
||
773 | * Sets the new image width |
||
774 | * |
||
775 | * @param int $size dimension to set |
||
776 | * |
||
777 | * @return void |
||
778 | * @access protected |
||
779 | * @since 29/05/02 13:36:31 |
||
780 | */ |
||
781 | public function _set_new_x($size) |
||
782 | { |
||
783 | $this->new_x = $size; |
||
784 | } |
||
785 | |||
786 | /** |
||
787 | * Sets the new image height |
||
788 | * |
||
789 | * @param int $size dimension to set |
||
790 | * |
||
791 | * @return void |
||
792 | * @since 29/05/02 13:36:31 |
||
793 | * @access protected |
||
794 | */ |
||
795 | public function _set_new_y($size) |
||
796 | { |
||
797 | $this->new_y = $size; |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * Returns the image handle so that one can further try |
||
802 | * to manipulate the image |
||
803 | * |
||
804 | * @return resource |
||
805 | * |
||
806 | * @access public |
||
807 | */ |
||
808 | public function getHandle() |
||
809 | { |
||
810 | return PEAR::raiseError('getHandle() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
811 | }//function getHandle() |
||
812 | |||
813 | /** |
||
814 | * Returns the type of the image being manipulated |
||
815 | * |
||
816 | * @return string the image type |
||
817 | * @access public |
||
818 | */ |
||
819 | public function getImageType() |
||
820 | { |
||
821 | return $this->type; |
||
822 | } |
||
823 | |||
824 | /** |
||
825 | * Returns the MIME type of the image being manipulated |
||
826 | * |
||
827 | * @param string $type Image type to get MIME type for |
||
828 | * |
||
829 | * @return string The MIME type if available, or an empty string |
||
830 | * @access public |
||
831 | * @see PHP_Compat::image_type_to_mime_type() |
||
832 | * @link http://php.net/image_type_to_mime_type |
||
833 | */ |
||
834 | public function getMimeType($type = null) |
||
835 | { |
||
836 | return image_type_to_mime_type($this->_convert_image_type(($type) ? $type : $this->type)); |
||
837 | } |
||
838 | |||
839 | /** |
||
840 | * Returns the new image width |
||
841 | * |
||
842 | * This function returns the width |
||
843 | * of the new image. |
||
844 | * |
||
845 | * @access public |
||
846 | * @return int The width of the new image. |
||
847 | */ |
||
848 | public function getNewImageWidth() |
||
849 | { |
||
850 | if (isset($this->new_x)) { |
||
851 | return (int)$this->new_x; |
||
852 | } |
||
853 | |||
854 | return false; |
||
855 | } |
||
856 | |||
857 | /** |
||
858 | * Return new image Y |
||
859 | * |
||
860 | * This function will retrieve the |
||
861 | * new image 'Y' and return it's value |
||
862 | * if it's set. |
||
863 | * |
||
864 | * @access public |
||
865 | * @return int The new height of the image. |
||
866 | */ |
||
867 | public function getNewImageHeight() |
||
868 | { |
||
869 | if (isset($this->new_y)) { |
||
870 | return (int)$this->new_y; |
||
871 | } |
||
872 | |||
873 | return false; |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * Returns the image width |
||
878 | * |
||
879 | * @return int the width of the image |
||
880 | * @access public |
||
881 | */ |
||
882 | public function getImageWidth() |
||
883 | { |
||
884 | return $this->img_x; |
||
885 | } |
||
886 | |||
887 | /** |
||
888 | * Returns the image height |
||
889 | * |
||
890 | * @return int the width of the image |
||
891 | * @access public |
||
892 | */ |
||
893 | public function getImageHeight() |
||
894 | { |
||
895 | return $this->img_y; |
||
896 | } |
||
897 | |||
898 | /** |
||
899 | * Returns the image size and extra format information |
||
900 | * |
||
901 | * @return array The width and height of the image |
||
902 | * @access public |
||
903 | * @see PHP::getimagesize() |
||
904 | */ |
||
905 | public function getImageSize() |
||
906 | { |
||
907 | return array( |
||
908 | $this->img_x, |
||
909 | $this->img_y, |
||
910 | $this->_convert_image_type($this->type), |
||
911 | 'height="' . $this->img_y . '" width="' . $this->img_x . '"', |
||
912 | 'mime' => $this->getMimeType() |
||
913 | ); |
||
914 | } |
||
915 | |||
916 | /** |
||
917 | * This looks at the current image type and attempts to determine which |
||
918 | * web-safe format will be most suited. It does not work brilliantly with |
||
919 | * *.png images, because it is very difficult to know whether they are |
||
920 | * 8-bit or greater. Guess I need to have fatter code here :-) |
||
921 | * |
||
922 | * @return string web-safe image type |
||
923 | * @access public |
||
924 | */ |
||
925 | public function getWebSafeFormat() |
||
926 | { |
||
927 | switch ($this->type) { |
||
928 | case 'gif': |
||
929 | case 'png': |
||
930 | return 'png'; |
||
931 | break; |
||
932 | default: |
||
933 | return 'jpeg'; |
||
934 | } // switch |
||
935 | } |
||
936 | |||
937 | /** |
||
938 | * Handles space in path and Windows/UNIX difference |
||
939 | * |
||
940 | * @param string $path Base dir |
||
941 | * @param string $command Command to execute |
||
942 | * @param string $args Arguments to pass to the command |
||
943 | * |
||
944 | * @return string A prepared string suitable for exec() |
||
945 | * @access protected |
||
946 | */ |
||
947 | public function _prepare_cmd($path, $command, $args = '') |
||
948 | { |
||
949 | if (!OS_WINDOWS |
||
950 | || !preg_match('/\s/', $path)) { |
||
951 | return $path . $command . ' ' . $args; |
||
952 | } |
||
953 | |||
954 | return 'start /D "' . $path . '" /B ' . $command . ' ' . $args; |
||
955 | } |
||
956 | |||
957 | /** |
||
958 | * Place holder for the real resize method |
||
959 | * used by extended methods to do the resizing |
||
960 | * |
||
961 | * @return PEAR_error |
||
962 | * @access protected |
||
963 | */ |
||
964 | public function _resize() |
||
967 | } |
||
968 | |||
969 | /** |
||
970 | * Normalizes the colors, gamma and other properties of an image |
||
971 | * (this should give a result equivalent to a Photoshop autolevels) |
||
972 | * |
||
973 | * @return PEAR_error |
||
974 | * @access public |
||
975 | */ |
||
976 | public function normalize() |
||
977 | { |
||
978 | return PEAR::raiseError('Normalize method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
979 | } |
||
980 | |||
981 | /** |
||
982 | * Loads an image file to work with |
||
983 | * |
||
984 | * Place holder for the real load method |
||
985 | * used by extended methods to do the resizing |
||
986 | * |
||
987 | * @param string $filename Full name of file |
||
988 | * |
||
989 | * @return PEAR_error |
||
990 | * @access public |
||
991 | */ |
||
992 | public function load($filename) |
||
993 | { |
||
994 | return PEAR::raiseError('load() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
995 | } |
||
996 | |||
997 | /** |
||
998 | * Outputs the image to standard output |
||
999 | * |
||
1000 | * Place holder for the real display method |
||
1001 | * used by extended methods to do the resizing |
||
1002 | * |
||
1003 | * @param string $type Format of image to save as |
||
1004 | * @param mixed $quality Format-dependent |
||
1005 | * |
||
1006 | * @return PEAR_error |
||
1007 | * @access public |
||
1008 | */ |
||
1009 | public function display($type, $quality = null) |
||
1010 | { |
||
1011 | return PEAR::raiseError('display() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1012 | } |
||
1013 | |||
1014 | /** |
||
1015 | * Returns if the driver supports a given image type |
||
1016 | * |
||
1017 | * @param string $type Image type (GIF, PNG, JPEG...) |
||
1018 | * @param string $mode 'r' for read, 'w' for write, 'rw' for both |
||
1019 | * |
||
1020 | * @return TRUE if type (and mode) is supported FALSE otherwise |
||
1021 | * @access public |
||
1022 | */ |
||
1023 | public function supportsType($type, $mode = 'rw') |
||
1024 | { |
||
1025 | return (false === strpos(@$this->_supported_image_types[strtolower($type)], $mode)) ? false : true; |
||
1026 | } |
||
1027 | |||
1028 | /** |
||
1029 | * Saves image to file |
||
1030 | * |
||
1031 | * Place holder for the real save method |
||
1032 | * used by extended methods to do the resizing |
||
1033 | * |
||
1034 | * @param string $filename Filename to save image to |
||
1035 | * @param string $type Format of image to save as |
||
1036 | * @param mixed $quality Format-dependent |
||
1037 | * |
||
1038 | * @return PEAR_error |
||
1039 | * @access public |
||
1040 | */ |
||
1041 | public function save($filename, $type, $quality = null) |
||
1042 | { |
||
1043 | return PEAR::raiseError('save() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1044 | } |
||
1045 | |||
1046 | /** |
||
1047 | * Releases resource |
||
1048 | * |
||
1049 | * Place holder for the real free method |
||
1050 | * used by extended methods to do the resizing |
||
1051 | * |
||
1052 | * @return PEAR_error |
||
1053 | * @access public |
||
1054 | */ |
||
1055 | public function free() |
||
1056 | { |
||
1057 | return PEAR::raiseError('free() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1058 | } |
||
1059 | |||
1060 | /** |
||
1061 | * Converts a color string into an array of RGB values |
||
1062 | * |
||
1063 | * @param string $colorhex A color following the #FFFFFF format |
||
1064 | * |
||
1065 | * @return array 3-element array with 0-255 values |
||
1066 | * @access public |
||
1067 | * |
||
1068 | * @see rgb2colorname |
||
1069 | * @see colorarray2colorhex |
||
1070 | */ |
||
1071 | public function colorhex2colorarray($colorhex) |
||
1072 | { |
||
1073 | $r = hexdec(substr($colorhex, 1, 2)); |
||
1074 | $g = hexdec(substr($colorhex, 3, 2)); |
||
1075 | $b = hexdec(substr($colorhex, 5, 2)); |
||
1076 | |||
1077 | return array($r, $g, $b, 'type' => 'RGB'); |
||
1078 | } |
||
1079 | |||
1080 | public function _send_display_headers($type) |
||
1081 | { |
||
1082 | // Find the filename of the original image: |
||
1083 | $filename = explode('.', basename($this->image)); |
||
1084 | $filename = $filename[0]; |
||
1085 | header('Content-type: ' . $this->getMimeType($type)); |
||
1086 | header('Content-Disposition: inline; filename=' . $filename . '.' . $type); |
||
1087 | } |
||
1088 | |||
1089 | /** |
||
1090 | * Converts an array of RGB value into a #FFFFFF format color. |
||
1091 | * |
||
1092 | * @param array $color 3-element array with 0-255 values |
||
1093 | * |
||
1094 | * @return mixed A color following the #FFFFFF format or FALSE |
||
1095 | * if the array couldn't be converted |
||
1096 | * @access public |
||
1097 | * |
||
1098 | * @see rgb2colorname |
||
1099 | * @see colorhex2colorarray |
||
1100 | */ |
||
1101 | public function colorarray2colorhex($color) |
||
1102 | { |
||
1103 | if (!is_array($color)) { |
||
1104 | return false; |
||
1105 | } |
||
1106 | $color = sprintf('#%02X%02X%02X', @$color[0], @$color[1], @$color[2]); |
||
1107 | |||
1108 | return (7 != strlen($color)) ? false : $color; |
||
1109 | } |
||
1110 | |||
1111 | /** |
||
1112 | * Returns the temp directory according to either the TMP, TMPDIR, or TEMP env |
||
1113 | * variables. If these are not set it will also check for the existence of |
||
1114 | * /tmp, %WINDIR%\temp |
||
1115 | * |
||
1116 | * @access public |
||
1117 | * @return string The system tmp directory |
||
1118 | */ |
||
1119 | public function getTempDir() |
||
1120 | { |
||
1121 | require_once 'System.php'; |
||
1122 | |||
1123 | return System::tmpdir(); |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * Returns a temporary filename using tempnam() and the above getTmpDir() function. |
||
1128 | * |
||
1129 | * @param string $dirname Optional directory name for the tmp file |
||
1130 | * |
||
1131 | * @return string Filename and path of the tmp file |
||
1132 | * @access public |
||
1133 | */ |
||
1134 | public function getTempFile($dirname = null) |
||
1135 | { |
||
1136 | if (is_null($dirname)) { |
||
1137 | require_once 'System.php'; |
||
1138 | $dirname = System::tmpdir(); |
||
1139 | } |
||
1140 | |||
1141 | return tempnam($dirname, 'temp.'); |
||
1142 | } |
||
1143 | |||
1144 | public function keepSettingsOnSave($bool) |
||
1145 | { |
||
1146 | $this->keep_settings_on_save = $bool; |
||
1147 | } |
||
1148 | |||
1149 | /** |
||
1150 | * Methods to add to the driver classes in the future |
||
1151 | * |
||
1152 | * @return void |
||
1153 | */ |
||
1154 | public function addText() |
||
1155 | { |
||
1156 | return PEAR::raiseError('addText() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1157 | } |
||
1158 | |||
1159 | public function addDropShadow() |
||
1160 | { |
||
1161 | return PEAR::raiseError('addDropShadow() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1162 | } |
||
1163 | |||
1164 | public function addBorder() |
||
1165 | { |
||
1166 | return PEAR::raiseError('addBorder() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1167 | } |
||
1168 | |||
1169 | /** |
||
1170 | * Crops an image |
||
1171 | * |
||
1172 | * @param int $width Cropped image width |
||
1173 | * @param int $height Cropped image height |
||
1174 | * @param int $x X-coordinate to crop at |
||
1175 | * @param int $y Y-coordinate to crop at |
||
1176 | * |
||
1177 | * @return mixed TRUE or a PEAR_Error object on error |
||
1178 | * @access public |
||
1179 | **/ |
||
1180 | public function crop($width, $height, $x = 0, $y = 0) |
||
1181 | { |
||
1182 | return PEAR::raiseError('crop() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1183 | } |
||
1184 | |||
1185 | public function canvasResize() |
||
1186 | { |
||
1187 | return PEAR::raiseError('canvasResize() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1188 | } |
||
1189 | |||
1190 | /** |
||
1191 | * Corrects the gamma of an image |
||
1192 | * |
||
1193 | * @param float $outputgamma Gamma correction factor |
||
1194 | * |
||
1195 | * @return mixed TRUE or a PEAR_error object on error |
||
1196 | * @access public |
||
1197 | **/ |
||
1198 | public function gamma($outputgamma = 1.0) |
||
1199 | { |
||
1200 | return PEAR::raiseError('gamma() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1201 | } |
||
1202 | |||
1203 | /** |
||
1204 | * Rotates the image clockwise |
||
1205 | * |
||
1206 | * @param float $angle Angle of rotation in degres |
||
1207 | * @param mixed $options Rotation options |
||
1208 | * |
||
1209 | * @return bool|PEAR_Error TRUE on success, PEAR_Error object on error |
||
1210 | * @access public |
||
1211 | */ |
||
1212 | public function rotate($angle, $options = null) |
||
1213 | { |
||
1214 | return PEAR::raiseError('rotate() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1215 | } |
||
1216 | |||
1217 | /** |
||
1218 | * Horizontal mirroring |
||
1219 | * |
||
1220 | * @return mixed TRUE or PEAR_Error object on error |
||
1221 | * @access public |
||
1222 | * @see flip() |
||
1223 | **/ |
||
1224 | public function mirror() |
||
1225 | { |
||
1226 | return PEAR::raiseError('mirror() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1227 | } |
||
1228 | |||
1229 | /** |
||
1230 | * Vertical mirroring |
||
1231 | * |
||
1232 | * @return TRUE or PEAR Error object on error |
||
1233 | * @access public |
||
1234 | * @see mirror() |
||
1235 | **/ |
||
1236 | public function flip() |
||
1237 | { |
||
1238 | return PEAR::raiseError('flip() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1239 | } |
||
1240 | |||
1241 | /** |
||
1242 | * Converts an image into greyscale colors |
||
1243 | * |
||
1244 | * @return mixed TRUE or a PEAR error object on error |
||
1245 | * @access public |
||
1246 | **/ |
||
1247 | public function greyscale() |
||
1248 | { |
||
1249 | return PEAR::raiseError('greyscale() method not supported by driver', IMAGE_TRANSFORM_ERROR_UNSUPPORTED); |
||
1250 | } |
||
1251 | |||
1252 | /** |
||
1253 | * Converts an image into greyscale colors |
||
1254 | * |
||
1255 | * @return mixed TRUE or a PEAR error object on error |
||
1256 | * @see greyscale() |
||
1257 | **/ |
||
1258 | public function grayscale() |
||
1259 | { |
||
1260 | return $this->greyscale(); |
||
1261 | } |
||
1262 | |||
1263 | /** |
||
1264 | * Returns a color option |
||
1265 | * |
||
1266 | * @param string $colorOf one of 'canvasColor', 'pencilColor', 'fontColor' |
||
1267 | * @param array $options configuration options |
||
1268 | * @param array $default default value to return if color not found |
||
1269 | * |
||
1270 | * @return array an RGB color array |
||
1271 | * @access protected |
||
1272 | */ |
||
1273 | public function _getColor($colorOf, $options = array(), $default = array(0, 0, 0)) |
||
1274 | { |
||
1275 | $opt = array_merge($this->_options, (array)$options); |
||
1276 | if (isset($opt[$colorOf])) { |
||
1277 | $color = $opt[$colorOf]; |
||
1278 | if (is_array($color)) { |
||
1279 | return $color; |
||
1280 | } |
||
1281 | if ('#' == $color{0}) { |
||
1282 | return $this->colorhex2colorarray($color); |
||
1283 | } |
||
1284 | static $colornames = array(); |
||
1285 | require_once 'Image/Transform/Driver/ColorsDefs.php'; |
||
1286 | |||
1287 | return (isset($colornames[$color])) ? $colornames[$color] : $default; |
||
1288 | } |
||
1289 | |||
1290 | return $default; |
||
1291 | } |
||
1292 | |||
1293 | /** |
||
1294 | * Returns an option |
||
1295 | * |
||
1296 | * @param string $name name of option |
||
1297 | * @param array $options local override option array |
||
1298 | * @param mixed $default default value to return if option is not found |
||
1299 | * |
||
1300 | * @return mixed the option |
||
1301 | * @access protected |
||
1302 | */ |
||
1303 | public function _getOption($name, $options = array(), $default = null) |
||
1304 | { |
||
1305 | $opt = array_merge($this->_options, (array)$options); |
||
1306 | |||
1307 | return (isset($opt[$name])) ? $opt[$name] : $default; |
||
1308 | } |
||
1309 | |||
1310 | /** |
||
1311 | * Checks if the rectangle passed intersects with the current image |
||
1312 | * |
||
1313 | * @param int $width Width of rectangle |
||
1314 | * @param int $height Height of rectangle |
||
1315 | * @param int $x X-coordinate |
||
1316 | * @param int $y Y-coordinate |
||
1317 | * |
||
1318 | * @return bool|PEAR_Error TRUE if intersects, FALSE if not, |
||
1319 | * and PEAR_Error on error |
||
1320 | * @access public |
||
1321 | */ |
||
1322 | public function intersects($width, $height, $x, $y) |
||
1341 | } |
||
1342 | } |
||
1343 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.