Use transparent gray instead of white or black to modify transparency, fixes #3727
[kohana-image.git] / classes / kohana / image / gd.php
blob38d0e2401a0fe5a1179161832e0aefc153d3a889
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 imagesavealpha($overlay, TRUE);
413 // Get the width and height of the watermark
414 $width = imagesx($overlay);
415 $height = imagesy($overlay);
417 if ($opacity < 100)
419 // Convert an opacity range of 0-100 to 127-0
420 $opacity = round(abs(($opacity * 127 / 100) - 127));
422 // Allocate transparent gray
423 $color = imagecolorallocatealpha($overlay, 127, 127, 127, $opacity);
425 // The transparent image will overlay the watermark
426 imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
428 // Fill the background with the transparent color
429 imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
432 // Alpha blending must be enabled on the background!
433 imagealphablending($this->_image, TRUE);
435 if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
437 // Destroy the overlay image
438 imagedestroy($overlay);
442 protected function _do_background($r, $g, $b, $opacity)
444 // Loads image if not yet loaded
445 $this->_load_image();
447 // Convert an opacity range of 0-100 to 127-0
448 $opacity = round(abs(($opacity * 127 / 100) - 127));
450 // Create a new background
451 $background = $this->_create($this->width, $this->height);
453 // Allocate the color
454 $color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
456 // Fill the image with white
457 imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color);
459 // Alpha blending must be enabled on the background!
460 imagealphablending($background, TRUE);
462 // Copy the image onto a white background to remove all transparency
463 if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height))
465 // Swap the new image for the old one
466 imagedestroy($this->_image);
467 $this->_image = $background;
471 protected function _do_save($file, $quality)
473 // Loads image if not yet loaded
474 $this->_load_image();
476 // Get the extension of the file
477 $extension = pathinfo($file, PATHINFO_EXTENSION);
479 // Get the save function and IMAGETYPE
480 list($save, $type) = $this->_save_function($extension, $quality);
482 // Save the image to a file
483 $status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file);
485 if ($status === TRUE AND $type !== $this->type)
487 // Reset the image type and mime type
488 $this->type = $type;
489 $this->mime = image_type_to_mime_type($type);
492 return TRUE;
495 protected function _do_render($type, $quality)
497 // Loads image if not yet loaded
498 $this->_load_image();
500 // Get the save function and IMAGETYPE
501 list($save, $type) = $this->_save_function($type, $quality);
503 // Capture the output
504 ob_start();
506 // Render the image
507 $status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL);
509 if ($status === TRUE AND $type !== $this->type)
511 // Reset the image type and mime type
512 $this->type = $type;
513 $this->mime = image_type_to_mime_type($type);
516 return ob_get_clean();
520 * Get the GD saving function and image type for this extension.
521 * Also normalizes the quality setting
523 * @param string image type: png, jpg, etc
524 * @param integer image quality
525 * @return array save function, IMAGETYPE_* constant
526 * @throws Kohana_Exception
528 protected function _save_function($extension, & $quality)
530 switch (strtolower($extension))
532 case 'jpg':
533 case 'jpeg':
534 // Save a JPG file
535 $save = 'imagejpeg';
536 $type = IMAGETYPE_JPEG;
537 break;
538 case 'gif':
539 // Save a GIF file
540 $save = 'imagegif';
541 $type = IMAGETYPE_GIF;
543 // GIFs do not a quality setting
544 $quality = NULL;
545 break;
546 case 'png':
547 // Save a PNG file
548 $save = 'imagepng';
549 $type = IMAGETYPE_PNG;
551 // Use a compression level of 9 (does not affect quality!)
552 $quality = 9;
553 break;
554 default:
555 throw new Kohana_Exception('Installed GD does not support :type images',
556 array(':type' => $extension));
557 break;
560 return array($save, $type);
564 * Create an empty image with the given width and height.
566 * @param integer image width
567 * @param integer image height
568 * @return resource
570 protected function _create($width, $height)
572 // Create an empty image
573 $image = imagecreatetruecolor($width, $height);
575 // Do not apply alpha blending
576 imagealphablending($image, FALSE);
578 // Save alpha levels
579 imagesavealpha($image, TRUE);
581 return $image;
584 } // End Image_GD