1 <?php
defined('SYSPATH') or die('No direct script access.');
3 * Support for image manipulation using [Imagick](http://php.net/Imagick).
5 * @package Kohana/Image
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
{
19 * Checks if ImageMagick is enabled.
21 * @throws Kohana_Exception
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;
35 * Runs [Image_Imagick::check] and loads the image.
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);
55 * Destroys the loaded image to free up resources.
59 public function __destruct()
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();
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();
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();
107 protected function _do_flip($direction)
109 if ($direction === Image
::HORIZONTAL
)
111 $this->im
->flopImage();
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();
144 protected function _do_reflection($height, $opacity, $fade_in)
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;
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
175 $this->mime
= image_type_to_mime_type($type);
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
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))
212 $type = IMAGETYPE_JPEG
;
213 $this->im
->setImageFormat('jpeg');
216 $type = IMAGETYPE_GIF
;
217 $this->im
->setImageFormat('gif');
220 $type = IMAGETYPE_PNG
;
221 $this->im
->setImageFormat('png');
224 throw new Kohana_Exception('Installed ImageMagick does not support :type images',
225 array(':type' => $extension));
229 $quality = $quality - 5;
233 } // End Kohana_Image_Imagick