3 /** This file is part of KCFinder project
5 * @desc GraphicsMagick 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 class image_gmagick
extends image
{
19 static $MIMES = array(
20 //'tif' => "image/tiff"
24 // ABSTRACT PUBLIC METHODS
26 public function resize($width, $height) {//
27 if (!$width) $width = 1;
28 if (!$height) $height = 1;
30 $this->image
->scaleImage($width, $height);
31 } catch (\Exception
$e) {
34 $this->width
= $width;
35 $this->height
= $height;
39 public function resizeFit($width, $height, $background=false) {//
40 if (!$width) $width = 1;
41 if (!$height) $height = 1;
44 $this->image
->scaleImage($width, $height, true);
45 $w = $this->image
->getImageWidth();
46 $h = $this->image
->getImageHeight();
47 } catch (\Exception
$e) {
51 if ($background === false) {
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) {
68 $this->width
= $width;
69 $this->height
= $height;
74 public function resizeCrop($width, $height, $offset=false) {
75 if (!$width) $width = 1;
76 if (!$height) $height = 1;
78 if (($this->width
/ $this->height
) > ($width / $height)) {
80 $w = ($this->width
* $h) / $this->height
;
82 if ($offset !== false) {
85 if (($w +
$offset) <= $width)
86 $offset = $width - $w;
89 $x = ($width - $w) / 2;
93 $h = ($this->height
* $w) / $this->width
;
95 if ($offset !== false) {
98 if (($h +
$offset) <= $height)
99 $offset = $height - $h;
102 $y = ($height - $h) / 2;
113 $this->image
->scaleImage($w, $h);
114 $this->image
->cropImage($width, $height, -$x, -$y);
115 } catch (\Exception
$e) {
119 $this->width
= $width;
120 $this->height
= $height;
124 public function rotate($angle, $background="#000000") {
126 $this->image
->rotateImage($background, $angle);
127 $w = $this->image
->getImageWidth();
128 $h = $this->image
->getImageHeight();
129 } catch (\Exception
$e) {
137 public function flipHorizontal() {
139 $this->image
->flopImage();
140 } catch (\Exception
$e) {
146 public function flipVertical() {
148 $this->image
->flipImage();
149 } catch (\Exception
$e) {
155 public function watermark($file, $left=false, $top=false) {
157 $wm = new \
Gmagick($file);
158 $w = $wm->getImageWidth();
159 $h = $wm->getImageHeight();
160 } catch (\Exception
$e) {
165 ($left === true) ?
0 : (
166 ($left === null) ?
round(($this->width
- $w) / 2) : (
167 (($left === false) ||
!preg_match('/^\d+$/', $left)) ?
($this->width
- $w) : $left));
169 ($top === true) ?
0 : (
170 ($top === null) ?
round(($this->height
- $h) / 2) : (
171 (($top === false) ||
!preg_match('/^\d+$/', $top)) ?
($this->height
- $h) : $top));
173 if ((($x +
$w) > $this->width
) ||
174 (($y +
$h) > $this->height
) ||
180 $this->image
->compositeImage($wm, 1, $x, $y);
181 } catch (\Exception
$e) {
188 // ABSTRACT PROTECTED METHODS
190 protected function getBlankImage($width, $height) {
192 $img = new \
Gmagick();
193 $img->newImage($width, $height, "none");
194 } catch (\Exception
$e) {
200 protected function getImage($image, &$width, &$height) {
202 if (is_object($image) && ($image instanceof image_gmagick
)) {
203 $width = $image->width
;
204 $height = $image->height
;
205 return $image->image
;
207 } elseif (is_object($image) && ($image instanceof \Gmagick
)) {
209 $w = $image->getImageWidth();
210 $h = $image->getImageHeight();
211 } catch (\Exception
$e) {
218 } elseif (is_string($image)) {
220 $image = new \
Gmagick($image);
221 $w = $image->getImageWidth();
222 $h = $image->getImageHeight();
223 } catch (\Exception
$e) {
235 // PSEUDO-ABSTRACT STATIC METHODS
237 static function available() {
238 return class_exists("Gmagick");
241 static function checkImage($file) {
243 $img = new \
Gmagick($file);
244 } catch (\Exception
$e) {
253 public function output($type="jpeg", array $options=array()) {
254 $type = strtolower($type);
256 $this->image
->setImageFormat($type);
257 } catch (\Exception
$e) {
260 $method = "optimize_$type";
261 if (method_exists($this, $method) && !$this->$method($options))
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");
272 $file = $options['file'] . ".$type";
274 $this->image
->writeImage($file);
275 } catch (\Exception
$e) {
280 if (!@rename
($file, $options['file'])) {
292 protected function optimize_jpeg(array $options=array()) {
293 $quality = isset($options['quality']) ?
$options['quality'] : self
::DEFAULT_JPEG_QUALITY
;
295 $this->image
->setCompressionQuality($quality);
296 } catch (\Exception
$e) {