image_imagick::getImage()   B
last analyzed

Complexity

Conditions 9
Paths 10

Size

Total Lines 34
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

298
                /** @scrutinizer ignore-unhandled */ @unlink($file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
299
                return false;
300
            }
301
302
            if (!@rename($file, $options['file'])) {
303
                @unlink($file);
304
                return false;
305
            }
306
        }
307
308
        return true;
309
    }
310
311
    // OWN METHODS
312
313
    protected function optimize_jpeg(array $options = [])
314
    {
315
        $quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
316
        try {
317
            $this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
318
            $this->image->setImageCompressionQuality($quality);
319
        } catch (\Exception $e) {
320
            return false;
321
        }
322
        return true;
323
    }
324
325
}
326