/var/www/hkosl.com/innoutstorage2019/uploader/lib/class_image_imagick.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php

/** This file is part of KCFinder project
  *
  *      @desc ImageMagick image driver class
  *   @package KCFinder
  *   @version 3.12
  *    @author Pavel Tzonkov <sunhater@sunhater.com>
  * @copyright 2010-2014 KCFinder Project
  *   @license http://opensource.org/licenses/GPL-3.0 GPLv3
  *   @license http://opensource.org/licenses/LGPL-3.0 LGPLv3
  *      @link http://kcfinder.sunhater.com
  */

namespace kcfinder;

class 
image_imagick extends image {

    static 
$MIMES = array(
        
//'tif' => "image/tiff"
    
);


    
// ABSTRACT PUBLIC METHODS

    
public function resize($width$height) {//
        
if (!$width$width 1;
        if (!
$height$height 1;
        try {
            
$this->image->scaleImage($width$height);
        } catch (\
Exception $e) {
            return 
false;
        }
        
$this->width $width;
        
$this->height $height;
        return 
true;
    }

    public function 
resizeFit($width$height$background=false) {//
        
if (!$width$width 1;
        if (!
$height$height 1;

        try {
            
$this->image->scaleImage($width$heighttrue);
            
$size $this->image->getImageGeometry();
        } catch (\
Exception $e) {
            return 
false;
        }

        if (
$background === false) {
            
$this->width $size['width'];
            
$this->height $size['height'];
            return 
true;

        } else {
            try {
                
$this->image->setImageBackgroundColor($background);
                
$x = -round(($width $size['width']) / 2);
                
$y = -round(($height $size['height']) / 2);
                
$this->image->extentImage($width$height$x$y);
            } catch (\
Exception $e) {
                return 
false;
            }
            
$this->width $width;
            
$this->height $height;
            return 
true;
        }
    }

    public function 
resizeCrop($width$height$offset=false) {
        if (!
$width$width 1;
        if (!
$height$height 1;

        if ((
$this->width $this->height) > ($width $height)) {
            
$h $height;
            
$w = ($this->width $h) / $this->height;
            
$y 0;
            if (
$offset !== false) {
                if (
$offset 0)
                    
$offset = -$offset;
                if ((
$w $offset) <= $width)
                    
$offset $width $w;
                
$x $offset;
            } else
                
$x = ($width $w) / 2;

        } else {
            
$w $width;
            
$h = ($this->height $w) / $this->width;
            
$x 0;
            if (
$offset !== false) {
                if (
$offset 0)
                    
$offset = -$offset;
                if ((
$h $offset) <= $height)
                    
$offset $height $h;
                
$y $offset;
            } else
                
$y = ($height $h) / 2;
        }

        
$x round($x);
        
$y round($y);
        
$w round($w);
        
$h round($h);
        if (!
$w$w 1;
        if (!
$h$h 1;

        try {
            
$this->image->scaleImage($w$h);
            
$this->image->cropImage($width$height, -$x, -$y);
        } catch (\
Exception $e) {
            return 
false;
        }

        
$this->width $width;
        
$this->height $height;
        return 
true;
    }

    public function 
rotate($angle$background="#000000") {
        try {
            
$this->image->rotateImage(new \ImagickPixel($background), $angle);
            
$size $this->image->getImageGeometry();
        } catch (\
Exception $e) {
            return 
false;
        }
        
$this->width $size['width'];
        
$this->height $size['height'];
        return 
true;
    }

    public function 
flipHorizontal() {
        try {
            
$this->image->flopImage();
        } catch (\
Exception $e) {
            return 
false;
        }
        return 
true;
    }

    public function 
flipVertical() {
        try {
            
$this->image->flipImage();
        } catch (\
Exception $e) {
            return 
false;
        }
        return 
true;
    }

    public function 
watermark($file$left=false$top=false) {
        try {
            
$wm = new \Imagick($file);
            
$size $wm->getImageGeometry();
        } catch (\
Exception $e) {
            return 
false;
        }

        
$w $size['width'];
        
$h $size['height'];
        
$x =
            (
$left === true) ? : (
            (
$left === null) ? round(($this->width $w) / 2) : (
            ((
$left === false) || !preg_match('/^\d+$/'$left)) ? ($this->width $w) : $left));
        
$y =
            (
$top === true) ? : (
            (
$top === null) ? round(($this->height $h) / 2) : (
            ((
$top === false) || !preg_match('/^\d+$/'$top)) ? ($this->height $h) : $top));

        if (((
$x $w) > $this->width) ||
            ((
$y $h) > $this->height) ||
            (
$x 0) || ($y 0)
        )
            return 
false;

        try {
            
$this->image->compositeImage($wm, \Imagick::COMPOSITE_DEFAULT$x$y);
        } catch (\
Exception $e) {
            return 
false;
        }
        return 
true;
    }


    
// ABSTRACT PROTECTED METHODS

    
protected function getBlankImage($width$height) {
        try {
            
$img = new \Imagick();
            
$img->newImage($width$height"none");
            
$img->setImageCompressionQuality(100);
        } catch (\
Exception $e) {
            return 
false;
        }
        return 
$img;
    }

    protected function 
getImage($image, &$width, &$height) {

        if (
is_object($image) && ($image instanceof image_imagick)) {
            try {
                
$image->image->setImageCompressionQuality(100);
            } catch (\
Exception $e) {
                return 
false;
            }
            
$width $image->width;
            
$height $image->height;
            return 
$image->image;

        } elseif (
is_object($image) && ($image instanceof \Imagick)) {
            try {
                
$image->setImageCompressionQuality(100);
                
$size $image->getImageGeometry();
            } catch (\
Exception $e) {
                return 
false;
            }
            
$width $size['width'];
            
$height $size['height'];
            return 
$image;

        } elseif (
is_string($image)) {
            try {
                
$image = new \Imagick($image);
                
$image->setImageCompressionQuality(100);
                
$size $image->getImageGeometry();
            } catch (\
Exception $e) {
                return 
false;
            }
            
$width $size['width'];
            
$height $size['height'];
            return 
$image;

        } else
            return 
false;
    }


    
// PSEUDO-ABSTRACT STATIC METHODS

    
static function available() {
        return 
class_exists("\\Imagick");
    }

    static function 
checkImage($file) {
        try {
            
$img = new \Imagick($file);
        } catch (\
Exception $e) {
            return 
false;
        }
        return 
true;
    }


    
// INHERIT METHODS

    
public function output($type="jpeg", array $options=array()) {
        
$type strtolower($type);
        try {
            
$this->image->setImageFormat($type);
        } catch (\
Exception $e) {
            return 
false;
        }
        
$method "optimize_$type";
        if (
method_exists($this$method) && !$this->$method($options))
            return 
false;

        if (!isset(
$options['file'])) {
            if (!
headers_sent()) {
                
$mime = isset(self::$MIMES[$type]) ? self::$MIMES[$type] : "image/$type";
                
header("Content-Type: $mime");
            }
            echo 
$this->image;

        } else {
            
$file $options['file'] . ".$type";
            try {
                
$this->image->writeImage($file);
            } catch (\
Exception $e) {
                @
unlink($file);
                return 
false;
            }

            if (!@
rename($file$options['file'])) {
                @
unlink($file);
                return 
false;
            }
        }

        return 
true;
    }


    
// OWN METHODS

    
protected function optimize_jpeg(array $options=array()) {
        
$quality = isset($options['quality']) ? $options['quality'] : self::DEFAULT_JPEG_QUALITY;
        try {
            
$this->image->setImageCompression(\Imagick::COMPRESSION_JPEG);
            
$this->image->setImageCompressionQuality($quality);
        } catch (\
Exception $e) {
            return 
false;
        }
        return 
true;
    }

}

?>