commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / kcfinder / lib / class_image.php
1 <?php
2
3 /** This file is part of KCFinder project
4 *
5 * @desc Abstract image driver class
6 * @package KCFinder
7 * @version 3.12
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
13 */
14
15 namespace kcfinder;
16
17 abstract class image {
18 const DEFAULT_JPEG_QUALITY = 75;
19
20 /** Image resource or object
21 * @var mixed */
22 protected $image;
23
24 /** Image width in pixels
25 * @var integer */
26 protected $width;
27
28 /** Image height in pixels
29 * @var integer */
30 protected $height;
31
32 /** Init error
33 * @var bool */
34 protected $initError = false;
35
36 /** Driver specific options
37 * @var array */
38 protected $options = array();
39
40
41 /** Magic method which allows read-only access to all protected or private
42 * class properties
43 * @param string $property
44 * @return mixed */
45
46 final public function __get($property) {
47 return property_exists($this, $property) ? $this->$property : null;
48 }
49
50
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
59 * @param mixed $image
60 * @param array $options */
61
62 public function __construct($image, array $options=array()) {
63 $this->image = $this->width = $this->height = null;
64 $imageDetails = $this->buildImage($image);
65
66 if ($imageDetails !== false)
67 list($this->image, $this->width, $this->height) = $imageDetails;
68 else
69 $this->initError = true;
70 $this->options = $options;
71 }
72
73
74 /** Factory pattern to load selected driver. $image and $options are passed
75 * to the constructor of the image driver
76 * @param string $driver
77 * @param mixed $image
78 * @return object */
79
80 final static function factory($driver, $image, array $options=array()) {
81 $class = __NAMESPACE__ . "\\image_$driver";
82 return new $class($image, $options);
83 }
84
85
86 /** Checks if the drivers in the array parameter could be used. Returns first
87 * found one
88 * @param array $drivers
89 * @return string */
90
91 final static function getDriver(array $drivers=array('gd')) {
92 foreach ($drivers as $driver) {
93 if (!preg_match('/^[a-z0-9\_]+$/i', $driver))
94 continue;
95 $class = __NAMESPACE__ . "\\image_$driver";
96 if (class_exists($class) && method_exists($class, "available")) {
97 eval("\$avail = $class::available();");
98 if ($avail) return $driver;
99 }
100 }
101 return false;
102 }
103
104
105 /** Returns an array. Element 0 - image resource. Element 1 - width. Element 2 - height.
106 * Returns FALSE on failure.
107 * @param mixed $image
108 * @return array */
109
110 final protected function buildImage($image) {
111 $class = get_class($this);
112
113 if ($image instanceof $class) {
114 $width = $image->width;
115 $height = $image->height;
116 $img = $image->image;
117
118 } elseif (is_array($image)) {
119 list($key, $width) = each($image);
120 list($key, $height) = each($image);
121 $img = $this->getBlankImage($width, $height);
122
123 } else
124 $img = $this->getImage($image, $width, $height);
125
126 return ($img !== false)
127 ? array($img, $width, $height)
128 : false;
129 }
130
131
132 /** Returns calculated proportional width from the given height
133 * @param integer $resizedHeight
134 * @return integer */
135
136 final public function getPropWidth($resizedHeight) {
137 $width = round(($this->width * $resizedHeight) / $this->height);
138 if (!$width) $width = 1;
139 return $width;
140 }
141
142
143 /** Returns calculated proportional height from the given width
144 * @param integer $resizedWidth
145 * @return integer */
146
147 final public function getPropHeight($resizedWidth) {
148 $height = round(($this->height * $resizedWidth) / $this->width);
149 if (!$height) $height = 1;
150 return $height;
151 }
152
153
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
156 * methods
157 * @return bool */
158 static function available() { return false; }
159
160 /** Checks if file is an image. This static method should be implemented into
161 * driver classes like abstract methods
162 * @param string $file
163 * @return bool */
164 static function checkImage($file) { return false; }
165
166 /** Resize image. Should return TRUE on success or FALSE on failure
167 * @param integer $width
168 * @param integer $height
169 * @return bool */
170 abstract public function resize($width, $height);
171
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
179 * @return bool */
180 abstract public function resizeFit($width, $height, $background=false);
181
182 /** Resize and crop the image to fit in given resolution. Returns TRUE on
183 * success or FALSE on failure
184 * @param mixed $src
185 * @param integer $offset
186 * @return bool */
187 abstract public function resizeCrop($width, $height, $offset=false);
188
189
190 /** Rotate image
191 * @param integer $angle
192 * @param string $background
193 * @return bool */
194 abstract public function rotate($angle, $background="#000000");
195
196 abstract public function flipHorizontal();
197
198 abstract public function flipVertical();
199
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
207 * @param mixed $top
208 * @param mixed $left
209 * @return bool */
210 abstract public function watermark($file, $left=false, $top=false);
211
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
218 * @return bool */
219 abstract public function output($type='jpeg', array $options=array());
220
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
223 * $image property
224 * @param integer $width
225 * @param integer $height
226 * @return mixed */
227 abstract protected function getBlankImage($width, $height);
228
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
234 * property
235 * @param mixed $image
236 * @param integer $width
237 * @param integer $height
238 * @return mixed */
239 abstract protected function getImage($image, &$width, &$height);
240
241 }
242
243 ?>