Fixing whitespace from homm commits
[kohana-image.git] / classes / kohana / image / gd.php
blobe190654be610e8b110e15139c5dcf743500bb9b0
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * Image manipulation class using {@link http://php.net/gd GD}.
5 * @package Image
6 * @author Kohana Team
7 * @copyright (c) 2008-2009 Kohana Team
8 * @license http://kohanaphp.com/license.html
9 */
10 class Kohana_Image_GD extends Image {
12 // Is GD bundled or separate?
13 protected static $_bundled;
15 public static function check()
17 if ( ! function_exists('gd_info'))
19 throw new Kohana_Exception('GD is either not installed or not enabled, check your configuration');
22 if (defined('GD_BUNDLED'))
24 // Get the version via a constant, available in PHP 5.
25 Image_GD::$_bundled = GD_BUNDLED;
27 else
29 // Get the version information
30 $info = gd_info();
32 // Extract the bundled status
33 Image_GD::$_bundled = (bool) preg_match('/\bbundled\b/i', $info['GD Version']);
36 if (defined('GD_VERSION'))
38 // Get the version via a constant, available in PHP 5.2.4+
39 $version = GD_VERSION;
41 else
43 // Get the version information
44 $info = gd_info();
46 // Extract the version number
47 preg_match('/\d+\.\d+(?:\.\d+)?/', $info['GD Version'], $matches);
49 // Get the major version
50 $version = $matches[0];
53 if ( ! version_compare($version, '2.0.1', '>='))
55 throw new Kohana_Exception('Image_GD requires GD version :required or greater, you have :version',
56 array('required' => '2.0.1', ':version' => $version));
59 return Image_GD::$_checked = TRUE;
62 // Temporary image resource
63 protected $_image;
65 // Function name to open Image
66 protected $_create_function;
68 public function __construct($file)
70 if ( ! Image_GD::$_checked)
72 // Run the install check
73 Image_GD::check();
76 parent::__construct($file);
78 // Set the image creation function name
79 switch ($this->type)
81 case IMAGETYPE_JPEG:
82 $create = 'imagecreatefromjpeg';
83 break;
84 case IMAGETYPE_GIF:
85 $create = 'imagecreatefromgif';
86 break;
87 case IMAGETYPE_PNG:
88 $create = 'imagecreatefrompng';
89 break;
92 if ( ! isset($create) OR ! function_exists($create))
94 throw new Kohana_Exception('Installed GD does not support :type images',
95 array(':type' => image_type_to_extension($this->type, FALSE)));
98 // Save function for future use
99 $this->_create_function = $create;
101 // Save filename for lazy loading
102 $this->_image = $this->file;
105 public function __destruct()
107 if (is_resource($this->_image))
109 // Free all resources
110 imagedestroy($this->_image);
114 protected function _load_image()
116 if ( ! is_resource($this->_image))
118 // Gets create function
119 $create = $this->_create_function;
121 // Open the temporary image
122 $this->_image = $create($this->file);
124 // Preserve transparency when saving
125 imagesavealpha($this->_image, TRUE);
129 protected function _do_resize($width, $height)
131 // Presize width and height
132 $pre_width = $this->width;
133 $pre_height = $this->height;
135 // Loads image if not yet loaded
136 $this->_load_image();
138 // Test if we can do a resize without resampling to speed up the final resize
139 if ($width > ($this->width / 2) AND $height > ($this->height / 2))
141 // The maximum reduction is 10% greater than the final size
142 $reduction_width = round($width * 1.1);
143 $reduction_height = round($height * 1.1);
145 while ($pre_width / 2 > $reduction_width AND $pre_height / 2 > $reduction_height)
147 // Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
148 $pre_width /= 2;
149 $pre_height /= 2;
152 // Create the temporary image to copy to
153 $image = $this->_create($pre_width, $pre_height);
155 if (imagecopyresized($image, $this->_image, 0, 0, 0, 0, $pre_width, $pre_height, $this->width, $this->height))
157 // Swap the new image for the old one
158 imagedestroy($this->_image);
159 $this->_image = $image;
163 // Create the temporary image to copy to
164 $image = $this->_create($width, $height);
166 // Execute the resize
167 if (imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height))
169 // Swap the new image for the old one
170 imagedestroy($this->_image);
171 $this->_image = $image;
173 // Reset the width and height
174 $this->width = imagesx($image);
175 $this->height = imagesy($image);
179 protected function _do_crop($width, $height, $offset_x, $offset_y)
181 // Create the temporary image to copy to
182 $image = $this->_create($width, $height);
184 // Loads image if not yet loaded
185 $this->_load_image();
187 // Execute the crop
188 if (imagecopyresampled($image, $this->_image, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height))
190 // Swap the new image for the old one
191 imagedestroy($this->_image);
192 $this->_image = $image;
194 // Reset the width and height
195 $this->width = imagesx($image);
196 $this->height = imagesy($image);
200 protected function _do_rotate($degrees)
202 if ( ! Image_GD::$_bundled)
204 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
205 array(':function' => 'imagerotate'));
208 // Loads image if not yet loaded
209 $this->_load_image();
211 // Transparent black will be used as the background for the uncovered region
212 $transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127);
214 // Rotate, setting the transparent color
215 $image = imagerotate($this->_image, 360 - $degrees, $transparent, 1);
217 // Save the alpha of the rotated image
218 imagesavealpha($image, TRUE);
220 // Get the width and height of the rotated image
221 $width = imagesx($image);
222 $height = imagesy($image);
224 if (imagecopymerge($this->_image, $image, 0, 0, 0, 0, $width, $height, 100))
226 // Swap the new image for the old one
227 imagedestroy($this->_image);
228 $this->_image = $image;
230 // Reset the width and height
231 $this->width = $width;
232 $this->height = $height;
236 protected function _do_flip($direction)
238 // Create the flipped image
239 $flipped = $this->_create($this->width, $this->height);
241 // Loads image if not yet loaded
242 $this->_load_image();
244 if ($direction === Image::HORIZONTAL)
246 for ($x = 0; $x < $this->width; $x++)
248 // Flip each row from top to bottom
249 imagecopy($flipped, $this->_image, $x, 0, $this->width - $x - 1, 0, 1, $this->height);
252 else
254 for ($y = 0; $y < $this->height; $y++)
256 // Flip each column from left to right
257 imagecopy($flipped, $this->_image, 0, $y, 0, $this->height - $y - 1, $this->width, 1);
261 // Swap the new image for the old one
262 imagedestroy($this->_image);
263 $this->_image = $flipped;
265 // Reset the width and height
266 $this->width = imagesx($flipped);
267 $this->height = imagesy($flipped);
270 protected function _do_sharpen($amount)
272 if ( ! Image_GD::$_bundled)
274 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
275 array(':function' => 'imageconvolution'));
278 // Loads image if not yet loaded
279 $this->_load_image();
281 // Amount should be in the range of 18-10
282 $amount = round(abs(-18 + ($amount * 0.08)), 2);
284 // Gaussian blur matrix
285 $matrix = array
287 array(-1, -1, -1),
288 array(-1, $amount, -1),
289 array(-1, -1, -1),
292 // Perform the sharpen
293 if (imageconvolution($this->_image, $matrix, $amount - 8, 0))
295 // Reset the width and height
296 $this->width = imagesx($this->_image);
297 $this->height = imagesy($this->_image);
301 protected function _do_reflection($height, $opacity, $fade_in)
303 if ( ! Image_GD::$_bundled)
305 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
306 array(':function' => 'imagefilter'));
309 // Loads image if not yet loaded
310 $this->_load_image();
312 // Convert an opacity range of 0-100 to 127-0
313 $opacity = round(abs(($opacity * 127 / 100) - 127));
315 if ($opacity < 127)
317 // Calculate the opacity stepping
318 $stepping = (127 - $opacity) / $height;
320 else
322 // Avoid a "divide by zero" error
323 $stepping = 127 / $height;
326 // Create the reflection image
327 $reflection = $this->_create($this->width, $this->height + $height);
329 // Copy the image to the reflection
330 imagecopy($reflection, $this->_image, 0, 0, 0, 0, $this->width, $this->height);
332 for ($offset = 0; $height >= $offset; $offset++)
334 // Read the next line down
335 $src_y = $this->height - $offset - 1;
337 // Place the line at the bottom of the reflection
338 $dst_y = $this->height + $offset;
340 if ($fade_in === TRUE)
342 // Start with the most transparent line first
343 $dst_opacity = round($opacity + ($stepping * ($height - $offset)));
345 else
347 // Start with the most opaque line first
348 $dst_opacity = round($opacity + ($stepping * $offset));
351 // Create a single line of the image
352 $line = $this->_create($this->width, 1);
354 // Copy a single line from the current image into the line
355 imagecopy($line, $this->_image, 0, 0, 0, $src_y, $this->width, 1);
357 // Colorize the line to add the correct alpha level
358 imagefilter($line, IMG_FILTER_COLORIZE, 0, 0, 0, $dst_opacity);
360 // Copy a the line into the reflection
361 imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width, 1);
364 // Swap the new image for the old one
365 imagedestroy($this->_image);
366 $this->_image = $reflection;
368 // Reset the width and height
369 $this->width = imagesx($reflection);
370 $this->height = imagesy($reflection);
373 protected function _do_watermark(Image $watermark, $offset_x, $offset_y, $opacity)
375 if ( ! Image_GD::$_bundled)
377 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
378 array(':function' => 'imagelayereffect'));
381 // Loads image if not yet loaded
382 $this->_load_image();
384 // Create the watermark image resource
385 $overlay = imagecreatefromstring($watermark->render());
387 // Get the width and height of the watermark
388 $width = imagesx($overlay);
389 $height = imagesy($overlay);
391 if ($opacity < 100)
393 // Convert an opacity range of 0-100 to 127-0
394 $opacity = round(abs(($opacity * 127 / 100) - 127));
396 // Allocate transparent white
397 $color = imagecolorallocatealpha($overlay, 255, 255, 255, $opacity);
399 // The transparent image will overlay the watermark
400 imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
402 // Fill the background with transparent white
403 imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
406 // Alpha blending must be enabled on the background!
407 imagealphablending($this->_image, TRUE);
409 if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
411 // Destroy the overlay image
412 imagedestroy($overlay);
416 protected function _do_background($r, $g, $b, $opacity)
418 // Loads image if not yet loaded
419 $this->_load_image();
421 // Convert an opacity range of 0-100 to 127-0
422 $opacity = round(abs(($opacity * 127 / 100) - 127));
424 // Create a new background
425 $background = $this->_create($this->width, $this->height);
427 // Allocate the color
428 $color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
430 // Fill the image with white
431 imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color);
433 // Alpha blending must be enabled on the background!
434 imagealphablending($background, TRUE);
436 // Copy the image onto a white background to remove all transparency
437 if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height))
439 // Swap the new image for the old one
440 imagedestroy($this->_image);
441 $this->_image = $background;
445 protected function _do_save($file, $quality)
447 // Loads image if not yet loaded
448 $this->_load_image();
450 // Get the extension of the file
451 $extension = pathinfo($file, PATHINFO_EXTENSION);
453 // Get the save function and IMAGETYPE
454 list($save, $type) = $this->_save_function($extension, $quality);
456 // Save the image to a file
457 $status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file);
459 if ($status === TRUE AND $type !== $this->type)
461 // Reset the image type and mime type
462 $this->type = $type;
463 $this->mime = image_type_to_mime_type($type);
466 return TRUE;
469 protected function _do_render($type, $quality)
471 // Loads image if not yet loaded
472 $this->_load_image();
474 // Get the save function and IMAGETYPE
475 list($save, $type) = $this->_save_function($type, $quality);
477 // Capture the output
478 ob_start();
480 // Render the image
481 $status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL);
483 if ($status === TRUE AND $type !== $this->type)
485 // Reset the image type and mime type
486 $this->type = $type;
487 $this->mime = image_type_to_mime_type($type);
490 return ob_get_clean();
494 * Get the GD saving function and image type for this extension.
495 * Also normalizes the quality setting
497 * @throws Kohana_Exception
498 * @param string image type: png, jpg, etc
499 * @param integer image quality
500 * @return array save function, IMAGETYPE_* constant
502 protected function _save_function($extension, & $quality)
504 switch (strtolower($extension))
506 case 'jpg':
507 case 'jpeg':
508 // Save a JPG file
509 $save = 'imagejpeg';
510 $type = IMAGETYPE_JPEG;
511 break;
512 case 'gif':
513 // Save a GIF file
514 $save = 'imagegif';
515 $type = IMAGETYPE_GIF;
517 // GIFs do not a quality setting
518 $quality = NULL;
519 break;
520 case 'png':
521 // Save a PNG file
522 $save = 'imagepng';
523 $type = IMAGETYPE_PNG;
525 // Use a compression level of 9 (does not affect quality!)
526 $quality = 9;
527 break;
528 default:
529 throw new Kohana_Exception('Installed GD does not support :type images',
530 array(':type' => $extension));
531 break;
534 return array($save, $type);
538 * Create an empty image with the given width and height.
540 * @param integer image width
541 * @param integer image height
542 * @return resource
544 protected function _create($width, $height)
546 // Create an empty image
547 $image = imagecreatetruecolor($width, $height);
549 // Do not apply alpha blending
550 imagealphablending($image, FALSE);
552 // Save alpha levels
553 imagesavealpha($image, TRUE);
555 return $image;
558 } // End Image_GD