Merge remote branch 'bluehawk/docs' into docs_test
[kohana-image.git] / classes / kohana / image / gd.php
blob46bd07ba8a73c77d216c6577163830cf52c47e7f
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * Support for image manipulation using [GD](http://php.net/GD).
5 * @package Kohana/Image
6 * @category Drivers
7 * @author Kohana Team
8 * @copyright (c) 2008-2009 Kohana Team
9 * @license http://kohanaphp.com/license.html
11 class Kohana_Image_GD extends Image {
13 // Is GD bundled or separate?
14 protected static $_bundled;
16 /**
17 * Checks if GD is enabled and bundled. Bundled GD is required for some
18 * methods to work. Exceptions will be thrown from those methods when GD is
19 * not bundled.
21 * @return boolean
23 public static function check()
25 if ( ! function_exists('gd_info'))
27 throw new Kohana_Exception('GD is either not installed or not enabled, check your configuration');
30 if (defined('GD_BUNDLED'))
32 // Get the version via a constant, available in PHP 5.
33 Image_GD::$_bundled = GD_BUNDLED;
35 else
37 // Get the version information
38 $info = gd_info();
40 // Extract the bundled status
41 Image_GD::$_bundled = (bool) preg_match('/\bbundled\b/i', $info['GD Version']);
44 if (defined('GD_VERSION'))
46 // Get the version via a constant, available in PHP 5.2.4+
47 $version = GD_VERSION;
49 else
51 // Get the version information
52 $info = gd_info();
54 // Extract the version number
55 preg_match('/\d+\.\d+(?:\.\d+)?/', $info['GD Version'], $matches);
57 // Get the major version
58 $version = $matches[0];
61 if ( ! version_compare($version, '2.0.1', '>='))
63 throw new Kohana_Exception('Image_GD requires GD version :required or greater, you have :version',
64 array('required' => '2.0.1', ':version' => $version));
67 return Image_GD::$_checked = TRUE;
70 // Temporary image resource
71 protected $_image;
73 // Function name to open Image
74 protected $_create_function;
76 /**
77 * Runs [Image_GD::check] and loads the image.
79 * @return void
80 * @throws Kohana_Exception
82 public function __construct($file)
84 if ( ! Image_GD::$_checked)
86 // Run the install check
87 Image_GD::check();
90 parent::__construct($file);
92 // Set the image creation function name
93 switch ($this->type)
95 case IMAGETYPE_JPEG:
96 $create = 'imagecreatefromjpeg';
97 break;
98 case IMAGETYPE_GIF:
99 $create = 'imagecreatefromgif';
100 break;
101 case IMAGETYPE_PNG:
102 $create = 'imagecreatefrompng';
103 break;
106 if ( ! isset($create) OR ! function_exists($create))
108 throw new Kohana_Exception('Installed GD does not support :type images',
109 array(':type' => image_type_to_extension($this->type, FALSE)));
112 // Save function for future use
113 $this->_create_function = $create;
115 // Save filename for lazy loading
116 $this->_image = $this->file;
120 * Destroys the loaded image to free up resources.
122 * @return void
124 public function __destruct()
126 if (is_resource($this->_image))
128 // Free all resources
129 imagedestroy($this->_image);
134 * Loads an image into GD.
136 * @return void
138 protected function _load_image()
140 if ( ! is_resource($this->_image))
142 // Gets create function
143 $create = $this->_create_function;
145 // Open the temporary image
146 $this->_image = $create($this->file);
148 // Preserve transparency when saving
149 imagesavealpha($this->_image, TRUE);
153 protected function _do_resize($width, $height)
155 // Presize width and height
156 $pre_width = $this->width;
157 $pre_height = $this->height;
159 // Loads image if not yet loaded
160 $this->_load_image();
162 // Test if we can do a resize without resampling to speed up the final resize
163 if ($width > ($this->width / 2) AND $height > ($this->height / 2))
165 // The maximum reduction is 10% greater than the final size
166 $reduction_width = round($width * 1.1);
167 $reduction_height = round($height * 1.1);
169 while ($pre_width / 2 > $reduction_width AND $pre_height / 2 > $reduction_height)
171 // Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
172 $pre_width /= 2;
173 $pre_height /= 2;
176 // Create the temporary image to copy to
177 $image = $this->_create($pre_width, $pre_height);
179 if (imagecopyresized($image, $this->_image, 0, 0, 0, 0, $pre_width, $pre_height, $this->width, $this->height))
181 // Swap the new image for the old one
182 imagedestroy($this->_image);
183 $this->_image = $image;
187 // Create the temporary image to copy to
188 $image = $this->_create($width, $height);
190 // Execute the resize
191 if (imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height))
193 // Swap the new image for the old one
194 imagedestroy($this->_image);
195 $this->_image = $image;
197 // Reset the width and height
198 $this->width = imagesx($image);
199 $this->height = imagesy($image);
203 protected function _do_crop($width, $height, $offset_x, $offset_y)
205 // Create the temporary image to copy to
206 $image = $this->_create($width, $height);
208 // Loads image if not yet loaded
209 $this->_load_image();
211 // Execute the crop
212 if (imagecopyresampled($image, $this->_image, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height))
214 // Swap the new image for the old one
215 imagedestroy($this->_image);
216 $this->_image = $image;
218 // Reset the width and height
219 $this->width = imagesx($image);
220 $this->height = imagesy($image);
224 protected function _do_rotate($degrees)
226 if ( ! Image_GD::$_bundled)
228 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
229 array(':function' => 'imagerotate'));
232 // Loads image if not yet loaded
233 $this->_load_image();
235 // Transparent black will be used as the background for the uncovered region
236 $transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127);
238 // Rotate, setting the transparent color
239 $image = imagerotate($this->_image, 360 - $degrees, $transparent, 1);
241 // Save the alpha of the rotated image
242 imagesavealpha($image, TRUE);
244 // Get the width and height of the rotated image
245 $width = imagesx($image);
246 $height = imagesy($image);
248 if (imagecopymerge($this->_image, $image, 0, 0, 0, 0, $width, $height, 100))
250 // Swap the new image for the old one
251 imagedestroy($this->_image);
252 $this->_image = $image;
254 // Reset the width and height
255 $this->width = $width;
256 $this->height = $height;
260 protected function _do_flip($direction)
262 // Create the flipped image
263 $flipped = $this->_create($this->width, $this->height);
265 // Loads image if not yet loaded
266 $this->_load_image();
268 if ($direction === Image::HORIZONTAL)
270 for ($x = 0; $x < $this->width; $x++)
272 // Flip each row from top to bottom
273 imagecopy($flipped, $this->_image, $x, 0, $this->width - $x - 1, 0, 1, $this->height);
276 else
278 for ($y = 0; $y < $this->height; $y++)
280 // Flip each column from left to right
281 imagecopy($flipped, $this->_image, 0, $y, 0, $this->height - $y - 1, $this->width, 1);
285 // Swap the new image for the old one
286 imagedestroy($this->_image);
287 $this->_image = $flipped;
289 // Reset the width and height
290 $this->width = imagesx($flipped);
291 $this->height = imagesy($flipped);
294 protected function _do_sharpen($amount)
296 if ( ! Image_GD::$_bundled)
298 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
299 array(':function' => 'imageconvolution'));
302 // Loads image if not yet loaded
303 $this->_load_image();
305 // Amount should be in the range of 18-10
306 $amount = round(abs(-18 + ($amount * 0.08)), 2);
308 // Gaussian blur matrix
309 $matrix = array
311 array(-1, -1, -1),
312 array(-1, $amount, -1),
313 array(-1, -1, -1),
316 // Perform the sharpen
317 if (imageconvolution($this->_image, $matrix, $amount - 8, 0))
319 // Reset the width and height
320 $this->width = imagesx($this->_image);
321 $this->height = imagesy($this->_image);
325 protected function _do_reflection($height, $opacity, $fade_in)
327 if ( ! Image_GD::$_bundled)
329 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
330 array(':function' => 'imagefilter'));
333 // Loads image if not yet loaded
334 $this->_load_image();
336 // Convert an opacity range of 0-100 to 127-0
337 $opacity = round(abs(($opacity * 127 / 100) - 127));
339 if ($opacity < 127)
341 // Calculate the opacity stepping
342 $stepping = (127 - $opacity) / $height;
344 else
346 // Avoid a "divide by zero" error
347 $stepping = 127 / $height;
350 // Create the reflection image
351 $reflection = $this->_create($this->width, $this->height + $height);
353 // Copy the image to the reflection
354 imagecopy($reflection, $this->_image, 0, 0, 0, 0, $this->width, $this->height);
356 for ($offset = 0; $height >= $offset; $offset++)
358 // Read the next line down
359 $src_y = $this->height - $offset - 1;
361 // Place the line at the bottom of the reflection
362 $dst_y = $this->height + $offset;
364 if ($fade_in === TRUE)
366 // Start with the most transparent line first
367 $dst_opacity = round($opacity + ($stepping * ($height - $offset)));
369 else
371 // Start with the most opaque line first
372 $dst_opacity = round($opacity + ($stepping * $offset));
375 // Create a single line of the image
376 $line = $this->_create($this->width, 1);
378 // Copy a single line from the current image into the line
379 imagecopy($line, $this->_image, 0, 0, 0, $src_y, $this->width, 1);
381 // Colorize the line to add the correct alpha level
382 imagefilter($line, IMG_FILTER_COLORIZE, 0, 0, 0, $dst_opacity);
384 // Copy a the line into the reflection
385 imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width, 1);
388 // Swap the new image for the old one
389 imagedestroy($this->_image);
390 $this->_image = $reflection;
392 // Reset the width and height
393 $this->width = imagesx($reflection);
394 $this->height = imagesy($reflection);
397 protected function _do_watermark(Image $watermark, $offset_x, $offset_y, $opacity)
399 if ( ! Image_GD::$_bundled)
401 throw new Kohana_Exception('This method requires :function, which is only available in the bundled version of GD',
402 array(':function' => 'imagelayereffect'));
405 // Loads image if not yet loaded
406 $this->_load_image();
408 // Create the watermark image resource
409 $overlay = imagecreatefromstring($watermark->render());
411 // Get the width and height of the watermark
412 $width = imagesx($overlay);
413 $height = imagesy($overlay);
415 if ($opacity < 100)
417 // Convert an opacity range of 0-100 to 127-0
418 $opacity = round(abs(($opacity * 127 / 100) - 127));
420 // Allocate transparent white
421 $color = imagecolorallocatealpha($overlay, 255, 255, 255, $opacity);
423 // The transparent image will overlay the watermark
424 imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
426 // Fill the background with transparent white
427 imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
430 // Alpha blending must be enabled on the background!
431 imagealphablending($this->_image, TRUE);
433 if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
435 // Destroy the overlay image
436 imagedestroy($overlay);
440 protected function _do_background($r, $g, $b, $opacity)
442 // Loads image if not yet loaded
443 $this->_load_image();
445 // Convert an opacity range of 0-100 to 127-0
446 $opacity = round(abs(($opacity * 127 / 100) - 127));
448 // Create a new background
449 $background = $this->_create($this->width, $this->height);
451 // Allocate the color
452 $color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
454 // Fill the image with white
455 imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color);
457 // Alpha blending must be enabled on the background!
458 imagealphablending($background, TRUE);
460 // Copy the image onto a white background to remove all transparency
461 if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height))
463 // Swap the new image for the old one
464 imagedestroy($this->_image);
465 $this->_image = $background;
469 protected function _do_save($file, $quality)
471 // Loads image if not yet loaded
472 $this->_load_image();
474 // Get the extension of the file
475 $extension = pathinfo($file, PATHINFO_EXTENSION);
477 // Get the save function and IMAGETYPE
478 list($save, $type) = $this->_save_function($extension, $quality);
480 // Save the image to a file
481 $status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file);
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 TRUE;
493 protected function _do_render($type, $quality)
495 // Loads image if not yet loaded
496 $this->_load_image();
498 // Get the save function and IMAGETYPE
499 list($save, $type) = $this->_save_function($type, $quality);
501 // Capture the output
502 ob_start();
504 // Render the image
505 $status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL);
507 if ($status === TRUE AND $type !== $this->type)
509 // Reset the image type and mime type
510 $this->type = $type;
511 $this->mime = image_type_to_mime_type($type);
514 return ob_get_clean();
518 * Get the GD saving function and image type for this extension.
519 * Also normalizes the quality setting
521 * @param string image type: png, jpg, etc
522 * @param integer image quality
523 * @return array save function, IMAGETYPE_* constant
524 * @throws Kohana_Exception
526 protected function _save_function($extension, & $quality)
528 switch (strtolower($extension))
530 case 'jpg':
531 case 'jpeg':
532 // Save a JPG file
533 $save = 'imagejpeg';
534 $type = IMAGETYPE_JPEG;
535 break;
536 case 'gif':
537 // Save a GIF file
538 $save = 'imagegif';
539 $type = IMAGETYPE_GIF;
541 // GIFs do not a quality setting
542 $quality = NULL;
543 break;
544 case 'png':
545 // Save a PNG file
546 $save = 'imagepng';
547 $type = IMAGETYPE_PNG;
549 // Use a compression level of 9 (does not affect quality!)
550 $quality = 9;
551 break;
552 default:
553 throw new Kohana_Exception('Installed GD does not support :type images',
554 array(':type' => $extension));
555 break;
558 return array($save, $type);
562 * Create an empty image with the given width and height.
564 * @param integer image width
565 * @param integer image height
566 * @return resource
568 protected function _create($width, $height)
570 // Create an empty image
571 $image = imagecreatetruecolor($width, $height);
573 // Do not apply alpha blending
574 imagealphablending($image, FALSE);
576 // Save alpha levels
577 imagesavealpha($image, TRUE);
579 return $image;
582 } // End Image_GD