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_gmagick.php
1 <?php
2
3 /** This file is part of KCFinder project
4 *
5 * @desc GraphicsMagick 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 class image_gmagick extends image {
18
19 static $MIMES = array(
20 //'tif' => "image/tiff"
21 );
22
23
24 // ABSTRACT PUBLIC METHODS
25
26 public function resize($width, $height) {//
27 if (!$width) $width = 1;
28 if (!$height) $height = 1;
29 try {
30 $this->image->scaleImage($width, $height);
31 } catch (\Exception $e) {
32 return false;
33 }
34 $this->width = $width;
35 $this->height = $height;
36 return true;
37 }
38
39 public function resizeFit($width, $height, $background=false) {//
40 if (!$width) $width = 1;
41 if (!$height) $height = 1;
42
43 try {
44 $this->image->scaleImage($width, $height, true);
45 $w = $this->image->getImageWidth();
46 $h = $this->image->getImageHeight();
47 } catch (\Exception $e) {
48 return false;
49 }
50
51 if ($background === false) {
52 $this->width = $w;
53 $this->height = $h;
54 return true;
55
56 } else {
57 try {
58 $this->image->setImageBackgroundColor($background);
59 $x = round(($width - $w) / 2);
60 $y = round(($height - $h) / 2);
61 $img = new \Gmagick();
62 $img->newImage($width, $height, $background);
63 $img->compositeImage($this->image, 1, $x, $y);
64 } catch (\Exception $e) {
65 return false;
66 }
67 $this->image = $img;
68 $this->width = $width;
69 $this->height = $height;
70 return true;
71 }
72 }
73
74 public function resizeCrop($width, $height, $offset=false) {
75 if (!$width) $width = 1;
76 if (!$height) $height = 1;
77
78 if (($this->width / $this->height) > ($width / $height)) {
79 $h = $height;
80 $w = ($this->width * $h) / $this->height;
81 $y = 0;
82 if ($offset !== false) {
83 if ($offset > 0)
84 $offset = -$offset;
85 if (($w + $offset) <= $width)
86 $offset = $width - $w;
87 $x = $offset;
88 } else
89 $x = ($width - $w) / 2;
90
91 } else {
92 $w = $width;
93 $h = ($this->height * $w) / $this->width;
94 $x = 0;
95 if ($offset !== false) {
96 if ($offset > 0)
97 $offset = -$offset;
98 if (($h + $offset) <= $height)
99 $offset = $height - $h;
100 $y = $offset;
101 } else
102 $y = ($height - $h) / 2;
103 }
104
105 $x = round($x);
106 $y = round($y);
107 $w = round($w);
108 $h = round($h);
109 if (!$w) $w = 1;
110 if (!$h) $h = 1;
111
112 try {
113 $this->image->scaleImage($w, $h);
114 $this->image->cropImage($width, $height, -$x, -$y);
115 } catch (\Exception $e) {
116 return false;
117 }
118
119 $this->width = $width;
120 $this->height = $height;
121 return true;
122 }
123
124 public function rotate($angle, $background="#000000") {
125 try {
126 $this->image->rotateImage($background, $angle);
127 $w = $this->image->getImageWidth();
128 $h = $this->image->getImageHeight();
129 } catch (\Exception $e) {
130 return false;
131 }
132 $this->width = $w;
133 $this->height = $h;
134 return true;
135 }
136
137 public function flipHorizontal() {
138 try {
139 $this->image->flopImage();
140 } catch (\Exception $e) {
141 return false;
142 }
143 return true;
144 }
145
146 public function flipVertical() {
147 try {
148 $this->image->flipImage();
149 } catch (\Exception $e) {
150 return false;
151 }
152 return true;
153 }
154
155 public function watermark($file, $left=false, $top=false) {
156 try {
157 $wm = new \Gmagick($file);
158 $w = $wm->getImageWidth();
159 $h = $wm->getImageHeight();
160 } catch (\Exception $e) {
161 return false;
162 }
163
164 $x =
165 ($left === true) ? 0 : (
166 ($left === null) ? round(($this->width - $w) / 2) : (
167 (($left === false) || !preg_match('/^\d+$/', $left)) ? ($this->width - $w) : $left));
168 $y =
169 ($top === true) ? 0 : (
170 ($top === null) ? round(($this->height - $h) / 2) : (
171 (($top === false) || !preg_match('/^\d+$/', $top)) ? ($this->height - $h) : $top));
172
173 if ((($x + $w) > $this->width) ||
174 (($y + $h) > $this->height) ||
175 ($x < 0) || ($y < 0)
176 )
177 return false;
178
179 try {
180 $this->image->compositeImage($wm, 1, $x, $y);
181 } catch (\Exception $e) {
182 return false;
183 }
184 return true;
185 }
186
187
188 // ABSTRACT PROTECTED METHODS
189
190 protected function getBlankImage($width, $height) {
191 try {
192 $img = new \Gmagick();
193 $img->newImage($width, $height, "none");
194 } catch (\Exception $e) {
195 return false;
196 }
197 return $img;
198 }
199
200 protected function getImage($image, &$width, &$height) {
201
202 if (is_object($image) && ($image instanceof image_gmagick)) {
203 $width = $image->width;
204 $height = $image->height;
205 return $image->image;
206
207 } elseif (is_object($image) && ($image instanceof \Gmagick)) {
208 try {
209 $w = $image->getImageWidth();
210 $h = $image->getImageHeight();
211 } catch (\Exception $e) {
212 return false;
213 }
214 $width = $w;
215 $height = $h;
216 return $image;
217
218 } elseif (is_string($image)) {
219 try {
220 $image = new \Gmagick($image);
221 $w = $image->getImageWidth();
222 $h = $image->getImageHeight();
223 } catch (\Exception $e) {
224 return false;
225 }
226 $width = $w;
227 $height = $h;
228 return $image;
229
230 } else
231 return false;
232 }
233
234
235 // PSEUDO-ABSTRACT STATIC METHODS
236
237 static function available() {
238 return class_exists("Gmagick");
239 }
240
241 static function checkImage($file) {
242 try {
243 $img = new \Gmagick($file);
244 } catch (\Exception $e) {
245 return false;
246 }
247 return true;
248 }
249
250
251 // INHERIT METHODS
252
253 public function output($type="jpeg", array $options=array()) {
254 $type = strtolower($type);
255 try {
256 $this->image->setImageFormat($type);
257 } catch (\Exception $e) {
258 return false;
259 }
260 $method = "optimize_$type";
261 if (method_exists($this, $method) && !$this->$method($options))
262 return false;
263
264 if (!isset($options['file'])) {
265 if (!headers_sent()) {
266 $mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
267 header("Content-Type: $mime");
268 }
269 echo $this->image;
270
271 } else {
272 $file = $options['file'] . ".$type";
273 try {
274 $this->image->writeImage($file);
275 } catch (\Exception $e) {
276 @unlink($file);
277 return false;
278 }
279
280 if (!@rename($file, $options['file'])) {
281 @unlink($file);
282 return false;
283 }
284 }
285
286 return true;
287 }
288
289
290 // OWN METHODS
291
292 protected function optimize_jpeg(array $options=array()) {
293 $quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
294 try {
295 $this->image->setCompressionQuality($quality);
296 } catch (\Exception $e) {
297 return false;
298 }
299 return true;
300 }
301
302 }
303
304 ?>