1 <?php
defined('SYSPATH') or die('No direct script access.');
3 * Image manipulation class using {@link http://php.net/gd GD}.
7 * @copyright (c) 2008-2009 Kohana Team
8 * @license http://kohanaphp.com/license.html
10 class 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_VERSION'))
21 // Get the version via a constant, available in PHP 5.2.4+
22 $version = GD_VERSION
;
26 // Get the version information
27 $version = current(gd_info());
29 // Extract the version number
30 preg_match('/\d+\.\d+(?:\.\d+)?/', $version, $matches);
32 // Get the major version
33 $version = $matches[0];
36 if ( ! version_compare($version, '2.0', '>='))
38 throw new Kohana_Exception('Image_GD requires GD version 2.0 or greater, you have :version',
39 array(':version' => $version));
42 return Image_GD
::$_checked = TRUE;
45 // Temporary image resource
48 public function __construct($file)
50 if ( ! Image_GD
::$_checked)
52 // Run the install check
56 parent
::__construct($file);
58 // Set the image creation function name
62 $create = 'imagecreatefromjpeg';
65 $create = 'imagecreatefromgif';
68 $create = 'imagecreatefrompng';
72 if ( ! isset($create) OR ! function_exists($create))
74 throw new Kohana_Exception('Installed GD does not support :type images',
75 array(':type' => image_type_to_extension($this->type
, FALSE)));
78 // Open the temporary image
79 $this->_image
= $create($this->file
);
81 // Preserve transparency when saving
82 imagesavealpha($this->_image
, TRUE);
85 public function __destruct()
87 if (is_resource($this->_image
))
90 imagedestroy($this->_image
);
94 protected function _do_resize($width, $height)
96 // Presize width and height
97 $pre_width = $this->width
;
98 $pre_height = $this->height
;
100 // Test if we can do a resize without resampling to speed up the final resize
101 if ($width > ($this->width
/ 2) AND $height > ($this->height
/ 2))
103 // The maximum reduction is 10% greater than the final size
104 $reduction_width = round($width * 1.1);
105 $reduction_height = round($height * 1.1);
107 while ($pre_width / 2 > $reduction_width AND $pre_height / 2 > $reduction_height)
109 // Reduce the size using an O(2n) algorithm, until it reaches the maximum reduction
114 // Create the temporary image to copy to
115 $image = $this->_create($pre_width, $pre_height);
117 if (imagecopyresized($image, $this->_image
, 0, 0, 0, 0, $pre_width, $pre_height, $this->width
, $this->height
))
119 // Swap the new image for the old one
120 imagedestroy($this->_image
);
121 $this->_image
= $image;
125 // Create the temporary image to copy to
126 $image = $this->_create($width, $height);
128 // Execute the resize
129 if (imagecopyresampled($image, $this->_image
, 0, 0, 0, 0, $width, $height, $pre_width, $pre_height))
131 // Swap the new image for the old one
132 imagedestroy($this->_image
);
133 $this->_image
= $image;
135 // Reset the width and height
136 $this->width
= imagesx($image);
137 $this->height
= imagesy($image);
141 protected function _do_crop($width, $height, $offset_x, $offset_y)
143 // Create the temporary image to copy to
144 $image = $this->_create($width, $height);
147 if (imagecopyresampled($image, $this->_image
, 0, 0, $offset_x, $offset_y, $width, $height, $width, $height))
149 // Swap the new image for the old one
150 imagedestroy($this->_image
);
151 $this->_image
= $image;
153 // Reset the width and height
154 $this->width
= imagesx($image);
155 $this->height
= imagesy($image);
159 protected function _do_rotate($degrees)
161 // Transparent black will be used as the background for the uncovered region
162 $transparent = imagecolorallocatealpha($this->_image
, 0, 0, 0, 127);
164 // Rotate, setting the transparent color
165 $image = imagerotate($this->_image
, 360 - $degrees, $transparent, 1);
167 // Save the alpha of the rotated image
168 imagesavealpha($image, TRUE);
170 // Get the width and height of the rotated image
171 $width = imagesx($image);
172 $height = imagesy($image);
174 if (imagecopymerge($this->_image
, $image, 0, 0, 0, 0, $width, $height, 100))
176 // Swap the new image for the old one
177 imagedestroy($this->_image
);
178 $this->_image
= $image;
180 // Reset the width and height
181 $this->width
= $width;
182 $this->height
= $height;
186 protected function _do_flip($direction)
188 // Create the flipped image
189 $flipped = $this->_create($this->width
, $this->height
);
191 if ($direction === Image
::HORIZONTAL
)
193 for ($x = 0; $x < $this->width
; $x++
)
195 // Flip each row from top to bottom
196 imagecopy($flipped, $this->_image
, $x, 0, $this->width
- $x - 1, 0, 1, $this->height
);
201 for ($y = 0; $y < $this->height
; $y++
)
203 // Flip each column from left to right
204 imagecopy($flipped, $this->_image
, 0, $y, 0, $this->height
- $y - 1, $this->width
, 1);
208 // Swap the new image for the old one
209 imagedestroy($this->_image
);
210 $this->_image
= $flipped;
212 // Reset the width and height
213 $this->width
= imagesx($flipped);
214 $this->height
= imagesy($flipped);
217 protected function _do_sharpen($amount)
219 // Amount should be in the range of 18-10
220 $amount = round(abs(-18 +
($amount * 0.08)), 2);
222 // Gaussian blur matrix
226 array(-1, $amount, -1),
230 // Perform the sharpen
231 if ($image = imageconvolution($this->_image
, $matrix, $amount - 8, 0))
233 // Swap the new image for the old one
234 imagedestroy($this->_image
);
235 $this->_image
= $image;
237 // Reset the width and height
238 $this->width
= imagesx($image);
239 $this->height
= imagesy($image);
243 protected function _do_reflection($height, $opacity, $fade_in)
245 // Convert an opacity range of 0-100 to 127-0
246 $opacity = round(abs(($opacity * 127 / 100) - 127));
250 // Calculate the opacity stepping
251 $stepping = (127 - $opacity) / $height;
255 // Avoid a "divide by zero" error
256 $stepping = 127 / $height;
259 // Create the reflection image
260 $reflection = $this->_create($this->width
, $this->height +
$height);
262 // Copy the image to the reflection
263 imagecopy($reflection, $this->_image
, 0, 0, 0, 0, $this->width
, $this->height
);
265 for ($offset = 0; $height >= $offset; $offset++
)
267 // Read the next line down
268 $src_y = $this->height
- $offset - 1;
270 // Place the line at the bottom of the reflection
271 $dst_y = $this->height +
$offset;
273 if ($fade_in === TRUE)
275 // Start with the most transparent line first
276 $dst_opacity = round($opacity +
($stepping * ($height - $offset)));
280 // Start with the most opaque line first
281 $dst_opacity = round($opacity +
($stepping * $offset));
284 // Create a single line of the image
285 $line = $this->_create($this->width
, 1);
287 // Copy a single line from the current image into the line
288 imagecopy($line, $this->_image
, 0, 0, 0, $src_y, $this->width
, 1);
290 // Colorize the line to add the correct alpha level
291 imagefilter($line, IMG_FILTER_COLORIZE
, 0, 0, 0, $dst_opacity);
293 // Copy a the line into the reflection
294 imagecopy($reflection, $line, 0, $dst_y, 0, 0, $this->width
, 1);
297 // Swap the new image for the old one
298 imagedestroy($this->_image
);
299 $this->_image
= $reflection;
301 // Reset the width and height
302 $this->width
= imagesx($reflection);
303 $this->height
= imagesy($reflection);
306 protected function _do_watermark(Image
$watermark, $offset_x, $offset_y, $opacity)
308 // Create the watermark image resource
309 $overlay = imagecreatefromstring($watermark->render());
311 // Get the width and height of the watermark
312 $width = imagesx($overlay);
313 $height = imagesy($overlay);
317 // Convert an opacity range of 0-100 to 127-0
318 $opacity = round(abs(($opacity * 127 / 100) - 127));
320 // Allocate transparent white
321 $color = imagecolorallocatealpha($overlay, 255, 255, 255, $opacity);
323 // The transparent image will overlay the watermark
324 imagelayereffect($overlay, IMG_EFFECT_OVERLAY
);
326 // Fill the background with transparent white
327 imagefilledrectangle($overlay, 0, 0, $width, $height, $color);
330 // Alpha blending must be enabled on the background!
331 imagealphablending($this->_image
, TRUE);
333 if (imagecopy($this->_image
, $overlay, $offset_x, $offset_y, 0, 0, $width, $height))
335 // Destroy the overlay image
336 imagedestroy($overlay);
340 protected function _do_background($r, $g, $b, $opacity)
342 // Convert an opacity range of 0-100 to 127-0
343 $opacity = round(abs(($opacity * 127 / 100) - 127));
345 // Create a new background
346 $background = $this->_create($this->width
, $this->height
);
348 // Allocate the color
349 $color = imagecolorallocatealpha($background, $r, $g, $b, $opacity);
351 // Fill the image with white
352 imagefilledrectangle($background, 0, 0, $this->width
, $this->height
, $color);
354 // Alpha blending must be enabled on the background!
355 imagealphablending($background, TRUE);
357 // Copy the image onto a white background to remove all transparency
358 if (imagecopy($background, $this->_image
, 0, 0, 0, 0, $this->width
, $this->height
))
360 // Swap the new image for the old one
361 imagedestroy($this->_image
);
362 $this->_image
= $background;
366 protected function _do_save($file, $quality)
368 // Get the extension of the file
369 $extension = pathinfo($file, PATHINFO_EXTENSION
);
371 // Get the save function and IMAGETYPE
372 list($save, $type) = $this->_save_function($extension, $quality);
374 // Save the image to a file
375 $status = isset($quality) ?
$save($this->_image
, NULL, $quality) : $save($this->_image
, NULL);
377 if ($status === TRUE AND $type !== $this->type
)
379 // Reset the image type and mime type
381 $this->mime
= image_type_to_mime_type($type);
387 protected function _do_render($type, $quality)
389 // Get the save function and IMAGETYPE
390 list($save, $type) = $this->_save_function($type, $quality);
392 // Capture the output
396 $status = isset($quality) ?
$save($this->_image
, NULL, $quality) : $save($this->_image
, NULL);
398 if ($status === TRUE AND $type !== $this->type
)
400 // Reset the image type and mime type
402 $this->mime
= image_type_to_mime_type($type);
405 return ob_get_clean();
409 * Get the GD saving function and image type for this extension.
410 * Also normalizes the quality setting
412 * @throws Kohana_Exception
413 * @param string image type: png, jpg, etc
414 * @param integer image quality
415 * @return array save function, IMAGETYPE_* constant
417 protected function _save_function($extension, & $quality)
425 $type = IMAGETYPE_JPEG
;
430 $type = IMAGETYPE_GIF
;
432 // GIFs do not a quality setting
438 $type = IMAGETYPE_PNG
;
440 // Use a compression level of 9 (does not affect quality!)
444 throw new Kohana_Exception('Installed GD does not support :type images',
445 array(':type' => $type));
449 return array($save, $type);
453 * Create an empty image with the given width and height.
455 * @param integer image width
456 * @param integer image height
459 protected function _create($width, $height)
461 // Create an empty image
462 $image = imagecreatetruecolor($width, $height);
464 // Do not apply alpha blending
465 imagealphablending($image, FALSE);
468 imagesavealpha($image, TRUE);