Image_GD::_save_function() needs to normalize the extension to allow capitalized...
[kohana-image.git] / classes / kohana / image / gd.php
blobc5fd5ed981f46d00fdac87aa31a18aa36b95492b
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 public static function check()
14 if ( ! function_exists('gd_info'))
16 throw new Kohana_Exception('GD is either not installed or not enabled, check your configuration');
19 if (defined('GD_BUNDLED'))
21 // Get the version via a constant, available in PHP 5.
22 $bundled = GD_BUNDLED;
24 else
26 // Get the version information
27 $bundled = current(gd_info());
29 // Extract the bundled status
30 $bundled = (bool) preg_match('/\bbundled\b/i', $bundled);
33 if ( ! $bundled)
35 throw new Kohana_Exception('Image_GD requires GD to be bundled with PHP');
38 if (defined('GD_VERSION'))
40 // Get the version via a constant, available in PHP 5.2.4+
41 $version = GD_VERSION;
43 else
45 // Get the version information
46 $version = current(gd_info());
48 // Extract the version number
49 preg_match('/\d+\.\d+(?:\.\d+)?/', $version, $matches);
51 // Get the major version
52 $version = $matches[0];
55 if ( ! version_compare($version, '2.0', '>='))
57 throw new Kohana_Exception('Image_GD requires GD version 2.0 or greater, you have :version',
58 array(':version' => $version));
61 return Image_GD::$_checked = TRUE;
64 // Temporary image resource
65 protected $_image;
67 public function __construct($file)
69 if ( ! Image_GD::$_checked)
71 // Run the install check
72 Image_GD::check();
75 parent::__construct($file);
77 // Set the image creation function name
78 switch ($this->type)
80 case IMAGETYPE_JPEG:
81 $create = 'imagecreatefromjpeg';
82 break;
83 case IMAGETYPE_GIF:
84 $create = 'imagecreatefromgif';
85 break;
86 case IMAGETYPE_PNG:
87 $create = 'imagecreatefrompng';
88 break;
91 if ( ! isset($create) OR ! function_exists($create))
93 throw new Kohana_Exception('Installed GD does not support :type images',
94 array(':type' => image_type_to_extension($this->type, FALSE)));
97 // Open the temporary image
98 $this->_image = $create($this->file);
100 // Preserve transparency when saving
101 imagesavealpha($this->_image, TRUE);
104 public function __destruct()
106 if (is_resource($this->_image))
108 // Free all resources
109 imagedestroy($this->_image);
113 protected function _do_resize($width, $height)
115 // Presize width and height
116 $pre_width = $this->width;
117 $pre_height = $this->height;
119 // Test if we can do a resize without resampling to speed up the final resize
120 if ($width > ($this->width / 2) AND $height > ($this->height / 2))
122 // The maximum reduction is 10% greater than the final size
123 $reduction_width = round($width * 1.1);
124 $reduction_height = round($height * 1.1);
126 while ($pre_width / 2 > $reduction_width AND $pre_height / 2 > $reduction_height)
128 // Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
129 $pre_width /= 2;
130 $pre_height /= 2;
133 // Create the temporary image to copy to
134 $image = $this->_create($pre_width, $pre_height);
136 if (imagecopyresized($image, $this->_image, 0, 0, 0, 0, $pre_width, $pre_height, $this->width, $this->height))
138 // Swap the new image for the old one
139 imagedestroy($this->_image);
140 $this->_image = $image;
144 // Create the temporary image to copy to
145 $image = $this->_create($width, $height);
147 // Execute the resize
148 if (imagecopyresampled($image, $this->_image, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height))
150 // Swap the new image for the old one
151 imagedestroy($this->_image);
152 $this->_image = $image;
154 // Reset the width and height
155 $this->width = imagesx($image);
156 $this->height = imagesy($image);
160 protected function _do_crop($width, $height, $offset_x, $offset_y)
162 // Create the temporary image to copy to
163 $image = $this->_create($width, $height);
165 // Execute the crop
166 if (imagecopyresampled($image, $this->_image, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height))
168 // Swap the new image for the old one
169 imagedestroy($this->_image);
170 $this->_image = $image;
172 // Reset the width and height
173 $this->width = imagesx($image);
174 $this->height = imagesy($image);
178 protected function _do_rotate($degrees)
180 // Transparent black will be used as the background for the uncovered region
181 $transparent = imagecolorallocatealpha($this->_image, 0, 0, 0, 127);
183 // Rotate, setting the transparent color
184 $image = imagerotate($this->_image, 360 - $degrees, $transparent, 1);
186 // Save the alpha of the rotated image
187 imagesavealpha($image, TRUE);
189 // Get the width and height of the rotated image
190 $width = imagesx($image);
191 $height = imagesy($image);
193 if (imagecopymerge($this->_image, $image, 0, 0, 0, 0, $width, $height, 100))
195 // Swap the new image for the old one
196 imagedestroy($this->_image);
197 $this->_image = $image;
199 // Reset the width and height
200 $this->width = $width;
201 $this->height = $height;
205 protected function _do_flip($direction)
207 // Create the flipped image
208 $flipped = $this->_create($this->width, $this->height);
210 if ($direction === Image::HORIZONTAL)
212 for ($x = 0; $x < $this->width; $x++)
214 // Flip each row from top to bottom
215 imagecopy($flipped, $this->_image, $x, 0, $this->width - $x - 1, 0, 1, $this->height);
218 else
220 for ($y = 0; $y < $this->height; $y++)
222 // Flip each column from left to right
223 imagecopy($flipped, $this->_image, 0, $y, 0, $this->height - $y - 1, $this->width, 1);
227 // Swap the new image for the old one
228 imagedestroy($this->_image);
229 $this->_image = $flipped;
231 // Reset the width and height
232 $this->width = imagesx($flipped);
233 $this->height = imagesy($flipped);
236 protected function _do_sharpen($amount)
238 // Amount should be in the range of 18-10
239 $amount = round(abs(-18 + ($amount * 0.08)), 2);
241 // Gaussian blur matrix
242 $matrix = array
244 array(-1, -1, -1),
245 array(-1, $amount, -1),
246 array(-1, -1, -1),
249 // Perform the sharpen
250 if (imageconvolution($this->_image, $matrix, $amount - 8, 0))
252 // Reset the width and height
253 $this->width = imagesx($this->_image);
254 $this->height = imagesy($this->_image);
258 protected function _do_reflection($height, $opacity, $fade_in)
260 // Convert an opacity range of 0-100 to 127-0
261 $opacity = round(abs(($opacity * 127 / 100) - 127));
263 if ($opacity < 127)
265 // Calculate the opacity stepping
266 $stepping = (127 - $opacity) / $height;
268 else
270 // Avoid a "divide by zero" error
271 $stepping = 127 / $height;
274 // Create the reflection image
275 $reflection = $this->_create($this->width, $this->height + $height);
277 // Copy the image to the reflection
278 imagecopy($reflection, $this->_image, 0, 0, 0, 0, $this->width, $this->height);
280 for ($offset = 0; $height >= $offset; $offset++)
282 // Read the next line down
283 $src_y = $this->height - $offset - 1;
285 // Place the line at the bottom of the reflection
286 $dst_y = $this->height + $offset;
288 if ($fade_in === TRUE)
290 // Start with the most transparent line first
291 $dst_opacity = round($opacity + ($stepping * ($height - $offset)));
293 else
295 // Start with the most opaque line first
296 $dst_opacity = round($opacity + ($stepping * $offset));
299 // Create a single line of the image
300 $line = $this->_create($this->width, 1);
302 // Copy a single line from the current image into the line
303 imagecopy($line, $this->_image, 0, 0, 0, $src_y, $this->width, 1);
305 // Colorize the line to add the correct alpha level
306 imagefilter($line, IMG_FILTER_COLORIZE, 0, 0, 0, $dst_opacity);
308 // Copy a the line into the reflection
309 imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width, 1);
312 // Swap the new image for the old one
313 imagedestroy($this->_image);
314 $this->_image = $reflection;
316 // Reset the width and height
317 $this->width = imagesx($reflection);
318 $this->height = imagesy($reflection);
321 protected function _do_watermark(Image $watermark, $offset_x, $offset_y, $opacity)
323 // Create the watermark image resource
324 $overlay = imagecreatefromstring($watermark->render());
326 // Get the width and height of the watermark
327 $width = imagesx($overlay);
328 $height = imagesy($overlay);
330 if ($opacity < 100)
332 // Convert an opacity range of 0-100 to 127-0
333 $opacity = round(abs(($opacity * 127 / 100) - 127));
335 // Allocate transparent white
336 $color = imagecolorallocatealpha($overlay, 255, 255, 255, $opacity);
338 // The transparent image will overlay the watermark
339 imagelayereffect($overlay, IMG_EFFECT_OVERLAY);
341 // Fill the background with transparent white
342 imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
345 // Alpha blending must be enabled on the background!
346 imagealphablending($this->_image, TRUE);
348 if (imagecopy($this->_image, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
350 // Destroy the overlay image
351 imagedestroy($overlay);
355 protected function _do_background($r, $g, $b, $opacity)
357 // Convert an opacity range of 0-100 to 127-0
358 $opacity = round(abs(($opacity * 127 / 100) - 127));
360 // Create a new background
361 $background = $this->_create($this->width, $this->height);
363 // Allocate the color
364 $color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
366 // Fill the image with white
367 imagefilledrectangle($background, 0, 0, $this->width, $this->height, $color);
369 // Alpha blending must be enabled on the background!
370 imagealphablending($background, TRUE);
372 // Copy the image onto a white background to remove all transparency
373 if (imagecopy($background, $this->_image, 0, 0, 0, 0, $this->width, $this->height))
375 // Swap the new image for the old one
376 imagedestroy($this->_image);
377 $this->_image = $background;
381 protected function _do_save($file, $quality)
383 // Get the extension of the file
384 $extension = pathinfo($file, PATHINFO_EXTENSION);
386 // Get the save function and IMAGETYPE
387 list($save, $type) = $this->_save_function($extension, $quality);
389 // Save the image to a file
390 $status = isset($quality) ? $save($this->_image, $file, $quality) : $save($this->_image, $file);
392 if ($status === TRUE AND $type !== $this->type)
394 // Reset the image type and mime type
395 $this->type = $type;
396 $this->mime = image_type_to_mime_type($type);
399 return TRUE;
402 protected function _do_render($type, $quality)
404 // Get the save function and IMAGETYPE
405 list($save, $type) = $this->_save_function($type, $quality);
407 // Capture the output
408 ob_start();
410 // Render the image
411 $status = isset($quality) ? $save($this->_image, NULL, $quality) : $save($this->_image, NULL);
413 if ($status === TRUE AND $type !== $this->type)
415 // Reset the image type and mime type
416 $this->type = $type;
417 $this->mime = image_type_to_mime_type($type);
420 return ob_get_clean();
424 * Get the GD saving function and image type for this extension.
425 * Also normalizes the quality setting
427 * @throws Kohana_Exception
428 * @param string image type: png, jpg, etc
429 * @param integer image quality
430 * @return array save function, IMAGETYPE_* constant
432 protected function _save_function($extension, & $quality)
434 switch (strtolower($extension))
436 case 'jpg':
437 case 'jpeg':
438 // Save a JPG file
439 $save = 'imagejpeg';
440 $type = IMAGETYPE_JPEG;
441 break;
442 case 'gif':
443 // Save a GIF file
444 $save = 'imagegif';
445 $type = IMAGETYPE_GIF;
447 // GIFs do not a quality setting
448 $quality = NULL;
449 break;
450 case 'png':
451 // Save a PNG file
452 $save = 'imagepng';
453 $type = IMAGETYPE_PNG;
455 // Use a compression level of 9 (does not affect quality!)
456 $quality = 9;
457 break;
458 default:
459 throw new Kohana_Exception('Installed GD does not support :type images',
460 array(':type' => $type));
461 break;
464 return array($save, $type);
468 * Create an empty image with the given width and height.
470 * @param integer image width
471 * @param integer image height
472 * @return resource
474 protected function _create($width, $height)
476 // Create an empty image
477 $image = imagecreatetruecolor($width, $height);
479 // Do not apply alpha blending
480 imagealphablending($image, FALSE);
482 // Save alpha levels
483 imagesavealpha($image, TRUE);
485 return $image;
488 } // End Image_GD