Use scaleImage instead of resizeImage to provide the most default behavior
[kohana-image.git] / classes / kohana / image / imagick.php
blob317eca24af86c04d42d990a67920b9b0e609f1dc
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * Support for image manipulation using [Imagick](http://php.net/Imagick).
5 * @package Kohana/Image
6 * @category Drivers
7 * @author Tamas Mihalik tamas.mihalik@gmail.com
8 * @copyright (c) 2009-2010 Kohana Team
9 * @license http://kohanaphp.com/license.html
11 class Kohana_Image_Imagick extends Image {
13 /**
14 * @var Imagick
16 protected $im;
18 /**
19 * Checks if ImageMagick is enabled.
21 * @throws Kohana_Exception
22 * @return boolean
24 public static function check()
26 if ( ! extension_loaded('imagick'))
28 throw new Kohana_Exception('Imagick is not installed, or the extension is not loaded');
31 return Image_Imagick::$_checked = TRUE;
34 /**
35 * Runs [Image_Imagick::check] and loads the image.
37 * @return void
38 * @throws Kohana_Exception
40 public function __construct($file)
42 if ( ! Image_Imagick::$_checked)
44 // Run the install check
45 Image_Imagick::check();
48 parent::__construct($file);
50 $this->im = new Imagick;
51 $this->im->readImage($file);
54 /**
55 * Destroys the loaded image to free up resources.
57 * @return void
59 public function __destruct()
61 $this->im->clear();
62 $this->im->destroy();
65 protected function _do_resize($width, $height)
67 if ($this->im->scaleImage($width, $height))
69 // Reset the width and height
70 $this->width = $this->im->getImageWidth();
71 $this->height = $this->im->getImageHeight();
73 return TRUE;
76 return FALSE;
79 protected function _do_crop($width, $height, $offset_x, $offset_y)
81 if ($this->im->cropImage($width, $height, $offset_x, $offset_y))
83 // Reset the width and height
84 $this->width = $this->im->getImageWidth();
85 $this->height = $this->im->getImageHeight();
87 return TRUE;
90 return FALSE;
93 protected function _do_rotate($degrees)
95 if ($this->im->rotateImage(new ImagickPixel, $degrees))
97 // Reset the width and height
98 $this->width = $this->im->getImageWidth();
99 $this->height = $this->im->getImageHeight();
101 return TRUE;
104 return FALSE;
107 protected function _do_flip($direction)
109 if ($direction === Image::HORIZONTAL)
111 $this->im->flopImage();
113 else
115 $this->im->flipImage();
119 // Reset the width and height
120 $this->width = $this->im->getImageWidth();
121 $this->height = $this->im->getImageHeight();
124 protected function _do_sharpen($amount)
126 //IM not support $amount under 5 (0.15)
127 $amount = ($amount < 5) ? 5 : $amount;
129 // Amount should be in the range of 0.0 to 3.0
130 $amount = ($amount * 3.0) / 100;
132 if ($this->im->sharpenImage(0, $amount))
134 // Reset the width and height
135 $this->width = $this->im->getImageWidth();
136 $this->height = $this->im->getImageHeight();
138 return TRUE;
141 return FALSE;
144 protected function _do_reflection($height, $opacity, $fade_in)
146 // TODO
149 protected function _do_watermark(Image $image, $offset_x, $offset_y, $opacity)
151 $this->im->compositeImage($image->im, Imagick::COMPOSITE_DISSOLVE, $offset_x, $offset_y);
154 protected function _do_background($r, $g, $b, $opacity)
156 $opacity = $opacity / 100;
158 // TODO
161 protected function _do_save($file, $quality)
163 // Get the extension of the file
164 $extension = pathinfo($file, PATHINFO_EXTENSION);
166 // Get the save function and IMAGETYPE
167 $type = $this->_save_function($extension, $quality);
169 $this->im->setImageCompressionQuality($quality);
171 if ($this->im->writeImage($file))
173 // Reset the image type and mime type
174 $this->type = $type;
175 $this->mime = image_type_to_mime_type($type);
177 return TRUE;
180 return FALSE;
183 protected function _do_render($type, $quality)
185 // Get the save function and IMAGETYPE
186 $type = $this->_save_function($type, $quality);
188 $this->im->setImageCompressionQuality($quality);
190 // Reset the image type and mime type
191 $this->type = $type;
192 $this->mime = image_type_to_mime_type($type);
194 return $this->im->__toString();
198 * Get the image type for this extension.
199 * Also normalizes the quality setting
201 * @param string image type: png, jpg, etc
202 * @param integer image quality
203 * @return string IMAGETYPE_* constant
204 * @throws Kohana_Exception
206 protected function _save_function($extension, & $quality)
208 switch (strtolower($extension))
210 case 'jpg':
211 case 'jpeg':
212 $type = IMAGETYPE_JPEG;
213 $this->im->setImageFormat('jpeg');
214 break;
215 case 'gif':
216 $type = IMAGETYPE_GIF;
217 $this->im->setImageFormat('gif');
218 break;
219 case 'png':
220 $type = IMAGETYPE_PNG;
221 $this->im->setImageFormat('png');
222 break;
223 default:
224 throw new Kohana_Exception('Installed ImageMagick does not support :type images',
225 array(':type' => $extension));
226 break;
229 $quality = $quality - 5;
231 return $type;
233 } // End Kohana_Image_Imagick