3 /** This file is part of KCFinder project
5 * @desc Abstract image driver class
8 * @author Pavel Tzonkov <sunhater@sunhater.com>
9 * @copyright 2010-2014 KCFinder Project
10 * @license http://opensource.org/licenses/GPL-3.0 GPLv3
11 * @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
12 * @link http://kcfinder.sunhater.com
17 abstract class image
{
18 const DEFAULT_JPEG_QUALITY
= 75;
20 /** Image resource or object
24 /** Image width in pixels
28 /** Image height in pixels
34 protected $initError = false;
36 /** Driver specific options
38 protected $options = array();
41 /** Magic method which allows read-only access to all protected or private
43 * @param string $property
46 final public function __get($property) {
47 return property_exists($this, $property) ?
$this->$property : null;
51 /** Constructor. Parameter $image should be:
52 * 1. An instance of image driver class (copy instance).
53 * 2. An image represented by the type of the $image property
54 * (resource or object).
55 * 3. An array with two elements. First - width, second - height.
56 * Creates a blank image.
57 * 4. A filename string. Get image form file.
58 * Second paramaeter is used by pass some specific image driver options
60 * @param array $options */
62 public function __construct($image, array $options=array()) {
63 $this->image
= $this->width
= $this->height
= null;
64 $imageDetails = $this->buildImage($image);
66 if ($imageDetails !== false)
67 list($this->image
, $this->width
, $this->height
) = $imageDetails;
69 $this->initError
= true;
70 $this->options
= $options;
74 /** Factory pattern to load selected driver. $image and $options are passed
75 * to the constructor of the image driver
76 * @param string $driver
80 final static function factory($driver, $image, array $options=array()) {
81 $class = __NAMESPACE__
. "\\image_$driver";
82 return new $class($image, $options);
86 /** Checks if the drivers in the array parameter could be used. Returns first
88 * @param array $drivers
91 final static function getDriver(array $drivers=array('gd')) {
92 foreach ($drivers as $driver) {
93 if (!preg_match('/^[a-z0-9\_]+$/i', $driver))
95 $class = __NAMESPACE__
. "\\image_$driver";
96 if (class_exists($class) && method_exists($class, "available")) {
97 eval("\$avail = $class::available();");
98 if ($avail) return $driver;
105 /** Returns an array. Element 0 - image resource. Element 1 - width. Element 2 - height.
106 * Returns FALSE on failure.
107 * @param mixed $image
110 final protected function buildImage($image) {
111 $class = get_class($this);
113 if ($image instanceof $class) {
114 $width = $image->width
;
115 $height = $image->height
;
116 $img = $image->image
;
118 } elseif (is_array($image)) {
119 list($key, $width) = each($image);
120 list($key, $height) = each($image);
121 $img = $this->getBlankImage($width, $height);
124 $img = $this->getImage($image, $width, $height);
126 return ($img !== false)
127 ?
array($img, $width, $height)
132 /** Returns calculated proportional width from the given height
133 * @param integer $resizedHeight
136 final public function getPropWidth($resizedHeight) {
137 $width = round(($this->width
* $resizedHeight) / $this->height
);
138 if (!$width) $width = 1;
143 /** Returns calculated proportional height from the given width
144 * @param integer $resizedWidth
147 final public function getPropHeight($resizedWidth) {
148 $height = round(($this->height
* $resizedWidth) / $this->width
);
149 if (!$height) $height = 1;
154 /** Checks if PHP needs some extra extensions to use the image driver. This
155 * static method should be implemented into driver classes like abstract
158 static function available() { return false; }
160 /** Checks if file is an image. This static method should be implemented into
161 * driver classes like abstract methods
162 * @param string $file
164 static function checkImage($file) { return false; }
166 /** Resize image. Should return TRUE on success or FALSE on failure
167 * @param integer $width
168 * @param integer $height
170 abstract public function resize($width, $height);
172 /** Resize image to fit in given resolution. Should returns TRUE on success
173 * or FALSE on failure. If $background is set, the image size will be
174 * $width x $height and the empty spaces (if any) will be filled with defined
175 * color. Background color examples: "#5f5", "#ff67ca", array(255, 255, 255)
176 * @param integer $width
177 * @param integer $height
178 * @param mixed $background
180 abstract public function resizeFit($width, $height, $background=false);
182 /** Resize and crop the image to fit in given resolution. Returns TRUE on
183 * success or FALSE on failure
185 * @param integer $offset
187 abstract public function resizeCrop($width, $height, $offset=false);
191 * @param integer $angle
192 * @param string $background
194 abstract public function rotate($angle, $background="#000000");
196 abstract public function flipHorizontal();
198 abstract public function flipVertical();
200 /** Apply a PNG or GIF watermark to the image. $top and $left parameters sets
201 * the offset of the watermark in pixels. Boolean and NULL values are possible
202 * too. In default case (FALSE, FALSE) the watermark should be applyed to
203 * the bottom right corner. NULL values means center aligning. If the
204 * watermark is bigger than the image or it's partialy or fully outside the
205 * image, it shoudn't be applied
206 * @param string $file
210 abstract public function watermark($file, $left=false, $top=false);
212 /** Should output the image. Second parameter is used to pass some options like
213 * 'file' - if is set, the output will be written to a file
214 * 'quality' - compression quality
215 * It's possible to use extra specific options required by image type ($type)
216 * @param string $type
217 * @param array $options
219 abstract public function output($type='jpeg', array $options=array());
221 /** This method should create a blank image with selected size. Should returns
222 * resource or object related to the created image, which will be passed to
224 * @param integer $width
225 * @param integer $height
227 abstract protected function getBlankImage($width, $height);
229 /** This method should create an image from source image. Only first parameter
230 * ($image) is input. Its type should be filename string or a type of the
231 * $image property. See the constructor reference for details. The
232 * parametters $width and $height are output only. Should returns resource or
233 * object related to the created image, which will be passed to $image
235 * @param mixed $image
236 * @param integer $width
237 * @param integer $height
239 abstract protected function getImage($image, &$width, &$height);