Code Duplication    Length = 425-464 lines in 2 locations

legacy/Image.php 1 location

@@ 13-476 (lines=464) @@
10
 *
11
 * @version    SVN: $Id: Image.php 193 2009-06-23 23:11:53Z victorstanciu $
12
 */
13
class Nip_File_Image extends Nip_File_Handler
14
{
15
    public $extensions = ['jpg', 'jpeg', 'gif', 'png'];
16
    public $quality = 90;
17
    public $type = 'jpg';
18
    public $max_width = false;
19
    public $errors = [];
20
21
    protected $_resource;
22
    protected $_file;
23
    protected $_upload;
24
    protected $_width;
25
    protected $_height;
26
27
    /**
28
     * @param array $upload
29
     */
30
    public function setResourceFromUpload($upload)
31
    {
32
        $this->_upload = $upload;
33
        $this->setResourceFromFile($upload['tmp_name']);
34
    }
35
36
    /**
37
     * @param string $path
38
     *
39
     * @return bool
40
     */
41
    public function setResourceFromFile($path)
42
    {
43
        $this->_file = $path;
44
        if (file_exists($path)) {
45
            $details = getimagesize($path);
46
47
            switch ($details['mime']) {
48
                case 'image/gif':
49
                    $this->type = 'gif';
50
                    if (imagetypes() & IMG_GIF) {
51
                        $this->_resource = imagecreatefromgif($path);
52
                    }
53
                    break;
54
                case 'image/jpeg':
55
                    $this->type = 'jpg';
56
                    if (imagetypes() & IMG_JPG) {
57
                        $this->_resource = imagecreatefromjpeg($path);
58
                    }
59
                    break;
60
                case 'image/png':
61
                    $this->type = 'png';
62
                    if (imagetypes() & IMG_PNG) {
63
                        $this->_resource = imagecreatefrompng($path);
64
                    }
65
                    break;
66
            }
67
68
            $this->getWidth();
69
            $this->getHeight();
70
71
            return true;
72
        } else {
73
            trigger_error("Cannot find file $path", E_USER_ERROR);
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * @return int
81
     */
82
    public function getWidth()
83
    {
84
        if (!$this->_width && $this->_resource) {
85
            $this->setWidth(imagesx($this->_resource));
86
        }
87
88
        return $this->_width;
89
    }
90
91
    /**
92
     * @param int $width
93
     */
94
    public function setWidth($width)
95
    {
96
        $this->_width = $width;
97
    }
98
99
    /**
100
     * @return int
101
     */
102
    public function getHeight()
103
    {
104
        if (!$this->_height && $this->_resource) {
105
            $this->setHeight(imagesy($this->_resource));
106
        }
107
108
        return $this->_height;
109
    }
110
111
    /**
112
     * @param int $height
113
     */
114
    public function setHeight($height)
115
    {
116
        $this->_height = $height;
117
    }
118
119
    /**
120
     * @param string $name
121
     */
122
    public function setBaseName($name)
123
    {
124
        $name = $name.'.'.$this->type;
125
        $this->setName($name);
126
    }
127
128
    /**
129
     * @param string $name
130
     */
131
    public function setName($name)
132
    {
133
        $this->name = $name;
134
        $this->url = dirname($this->url).'/'.$this->name;
135
        $this->path = dirname($this->path).'/'.$this->name;
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function save()
142
    {
143
        if (Nip_File_System::instance()->createDirectory(dirname($this->path))) {
144
            switch ($this->type) {
145
                case 'png':
146
                    if ($this->quality > 9) {
147
                        if ($this->quality < 100) {
148
                            $this->quality = $this->quality / 10;
149
                        } else {
150
                            $this->quality = 9;
151
                        }
152
                    }
153
                    $this->quality = abs($this->quality - 9);
154
                    $this->quality = 0;
155
156
                    $newImg = imagecreatetruecolor($this->_width, $this->_height);
157
                    imagealphablending($newImg, false);
158
                    imagesavealpha($newImg, true);
159
160
                    imagecopyresampled($newImg, $this->_resource, 0, 0, 0, 0, $this->_width, $this->_height, $this->_width, $this->_height);
161
162
                    $return = imagepng($newImg, $this->path, $this->quality);
163
                    break;
164
                case 'jpg':
165
                default:
166
                    $return = imagejpeg($this->_resource, $this->path, $this->quality);
167
                    break;
168
            }
169
170
            if ($return) {
171
                chmod($this->path, 0777);
172
173
                return true;
174
            }
175
            $this->errors[] = 'Error saving file';
176
        } else {
177
            $this->errors[] = 'Error creating directory';
178
        }
179
180
        return false;
181
    }
182
183
    public function grayscaleFade()
184
    {
185
        $this->grayscaleFilter();
186
        imagefilter($this->_resource, IMG_FILTER_BRIGHTNESS, 50);
187
    }
188
189
    public function grayscaleFilter()
190
    {
191
        imagefilter($this->_resource, IMG_FILTER_GRAYSCALE);
192
    }
193
194
    public function resize($max_width = false, $max_height = false)
195
    {
196
        if (!$max_width) {
197
            if ($this->max_width) {
198
                $max_width = $this->max_width;
199
            } else {
200
                $max_width = $this->getWidth();
201
            }
202
        }
203
204
        if (!$max_height) {
205
            if ($this->max_height) {
206
                $max_height = $this->max_height;
207
            } else {
208
                $max_height = $this->getHeight();
209
            }
210
        }
211
212
        $ratio = $this->getRatio();
213
        $target_ratio = $max_width / $max_height;
214
215
        if ($ratio > $target_ratio) {
216
            $new_width = $max_width;
217
            $new_height = round($max_width / $ratio);
218
        } else {
219
            $new_height = $max_height;
220
            $new_width = round($max_height * $ratio);
221
        }
222
223
        $image = imagecreatetruecolor($new_width, $new_height);
224
        imagealphablending($image, false);
225
        imagesavealpha($image, true);
226
227
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
228
229
        $this->_width = $new_width;
230
        $this->_height = $new_height;
231
        $this->_resource = $image;
232
233
        return $this;
234
    }
235
236
    public function getRatio()
237
    {
238
        return $this->getWidth() / $this->getHeight();
239
    }
240
241
    public function cropToCenter($cWidth, $cHeight)
242
    {
243
        $this->resizeToLarge($cWidth, $cHeight);
244
245
        $width = $this->getWidth();
246
        $height = $this->getHeight();
247
248
        $x0 = round(abs(($width - $cWidth) / 2), 0);
249
        $y0 = round(abs(($height - $cHeight) / 2), 0);
250
251
        $this->crop($x0, $y0, $cWidth, $cHeight, $cWidth, $cHeight);
252
    }
253
254
    /**
255
     * @param bool|int $max_width
256
     * @param bool|int $max_height
257
     *
258
     * @return $this
259
     */
260
    public function resizeToLarge($max_width = false, $max_height = false)
261
    {
262
        if (!$max_width) {
263
            $max_width = $this->getWidth();
264
        }
265
266
        if (!$max_height) {
267
            $max_height = $this->getHeight();
268
        }
269
270
        $sourceRatio = $this->getRatio();
271
        $target_ratio = $max_width / $max_height;
272
273
        if ($sourceRatio > $target_ratio) {
274
            $new_height = $max_height;
275
            $new_width = (int) ($max_height * $sourceRatio);
276
        } else {
277
            $new_width = $max_width;
278
            $new_height = (int) ($max_width / $sourceRatio);
279
        }
280
281
        $image = imagecreatetruecolor($new_width, $new_height);
282
        imagealphablending($image, false);
283
        imagesavealpha($image, true);
284
285
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
286
287
        $this->_width = $new_width;
288
        $this->_height = $new_height;
289
        $this->_resource = $image;
290
291
        return $this;
292
    }
293
294
    /**
295
     * @param $x
296
     * @param $y
297
     * @param $dwidth
298
     * @param $dheight
299
     * @param $swidth
300
     * @param $sheight
301
     */
302
    public function crop($x, $y, $dwidth, $dheight, $swidth, $sheight)
303
    {
304
        $image = imagecreatetruecolor($dwidth, $dheight);
305
        imagealphablending($image, false);
306
        imagesavealpha($image, true);
307
308
        imagecopyresampled($image, $this->_resource,
309
            0, 0,
310
            $x, $y,
311
            $dwidth, $dheight,
312
            $swidth, $sheight);
313
314
        $this->_width = $dwidth;
315
        $this->_height = $dheight;
316
        $this->_resource = $image;
317
    }
318
319
    /**
320
     * @param int   $amount
321
     * @param float $radius
322
     * @param int   $threshold
323
     *
324
     * @return $this
325
     */
326
    public function unsharpMask($amount = 80, $radius = 0.5, $threshold = 3)
327
    {
328
        $img = &$this->_resource;
329
330
        if ($amount > 500) {
331
            $amount = 500;
332
        }
333
        $amount = $amount * 0.016;
334
        if ($radius > 50) {
335
            $radius = 50;
336
        }
337
        $radius = $radius * 2;
338
        if ($threshold > 255) {
339
            $threshold = 255;
340
        }
341
342
        $radius = abs(round($radius));
343
        if ($radius == 0) {
344
            return;
345
        }
346
347
        $w = $this->_width;
348
        $h = $this->_height;
349
350
        $imgCanvas = imagecreatetruecolor($w, $h);
351
        $imgBlur = imagecreatetruecolor($w, $h);
352
353
        if (function_exists('imageconvolution')) {
354
            $matrix = [[1, 2, 1], [2, 4, 2], [1, 2, 1]];
355
            imagecopy($imgBlur, $img, 0, 0, 0, 0, $w, $h);
356
            imageconvolution($imgBlur, $matrix, 16, 0);
357
        } else {
358
            for ($i = 0; $i < $radius; $i++) {
359
                imagecopy($imgBlur, $img, 0, 0, 1, 0, $w - 1, $h);
360
                imagecopymerge($imgBlur, $img, 1, 0, 0, 0, $w, $h, 50);
361
                imagecopymerge($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50);
362
                imagecopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);
363
364
                imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333);
365
                imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25);
366
            }
367
        }
368
369
        if ($threshold > 0) {
370
            for ($x = 0; $x < $w - 1; $x++) {
371
                for ($y = 0; $y < $h; $y++) {
372
                    $rgbOrig = imagecolorat($img, $x, $y);
373
                    $rOrig = (($rgbOrig >> 16) & 0xFF);
374
                    $gOrig = (($rgbOrig >> 8) & 0xFF);
375
                    $bOrig = ($rgbOrig & 0xFF);
376
377
                    $rgbBlur = imagecolorat($imgBlur, $x, $y);
378
379
                    $rBlur = (($rgbBlur >> 16) & 0xFF);
380
                    $gBlur = (($rgbBlur >> 8) & 0xFF);
381
                    $bBlur = ($rgbBlur & 0xFF);
382
383
                    $rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
384
                    $gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
385
                    $bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
386
387
                    if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
388
                        $pixCol = imagecolorallocate($img, $rNew, $gNew, $bNew);
389
                        imagesetpixel($img, $x, $y, $pixCol);
390
                    }
391
                }
392
            }
393
        } else {
394
            for ($x = 0; $x < $w; $x++) {
395
                for ($y = 0; $y < $h; $y++) {
396
                    $rgbOrig = imagecolorat($img, $x, $y);
397
                    $rOrig = (($rgbOrig >> 16) & 0xFF);
398
                    $gOrig = (($rgbOrig >> 8) & 0xFF);
399
                    $bOrig = ($rgbOrig & 0xFF);
400
401
                    $rgbBlur = imagecolorat($imgBlur, $x, $y);
402
403
                    $rBlur = (($rgbBlur >> 16) & 0xFF);
404
                    $gBlur = (($rgbBlur >> 8) & 0xFF);
405
                    $bBlur = ($rgbBlur & 0xFF);
406
407
                    $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;
408
                    if ($rNew > 255) {
409
                        $rNew = 255;
410
                    } elseif ($rNew < 0) {
411
                        $rNew = 0;
412
                    }
413
                    $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
414
                    if ($gNew > 255) {
415
                        $gNew = 255;
416
                    } elseif ($gNew < 0) {
417
                        $gNew = 0;
418
                    }
419
                    $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;
420
                    if ($bNew > 255) {
421
                        $bNew = 255;
422
                    } elseif ($bNew < 0) {
423
                        $bNew = 0;
424
                    }
425
                    $rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew;
426
                    imagesetpixel($img, $x, $y, $rgbNew);
427
                }
428
            }
429
        }
430
431
        imagedestroy($imgCanvas);
432
        imagedestroy($imgBlur);
433
434
        return $this;
435
    }
436
437
    /**
438
     * @param Nip_File_Image $image
439
     *
440
     * @return $this
441
     */
442
    public function copyResource(self $image)
443
    {
444
        $this->_width = $image->getWidth();
445
        $this->_height = $image->getHeight();
446
        $this->_resource = $image->getResource();
447
448
        return $this;
449
    }
450
451
    public function getResource()
452
    {
453
        return $this->_resource;
454
    }
455
456
    public function setResource($gdImage)
457
    {
458
        $this->_resource = $gdImage;
459
    }
460
461
    /**
462
     * @return mixed
463
     */
464
    public function getFile()
465
    {
466
        return $this->_file;
467
    }
468
469
    /**
470
     * @return string
471
     */
472
    public function getExtension()
473
    {
474
        return Nip_File_System::instance()->getExtension($this->path);
475
    }
476
}
477

src/Image.php 1 location

@@ 13-437 (lines=425) @@
10
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
11
 * @version    SVN: $Id: Image.php 193 2009-06-23 23:11:53Z victorstanciu $
12
 */
13
class Image extends File
14
{
15
    public $extensions = ["jpg", "jpeg", "gif", "png"];
16
    public $quality = 90;
17
    public $type = 'jpg';
18
    public $max_width = false;
19
    public $errors = [];
20
    protected $_resource;
21
    protected $_file;
22
    protected $_upload;
23
    protected $_width;
24
    protected $_height;
25
26
    /**
27
     * @param array $upload
28
     */
29
    public function setResourceFromUpload($upload)
30
    {
31
        $this->_upload = $upload;
32
        $this->setResourceFromFile($upload['tmp_name']);
33
    }
34
35
    /**
36
     * @param string $path
37
     * @return bool
38
     */
39
    public function setResourceFromFile($path)
40
    {
41
        $this->_file = $path;
42
        if (file_exists($path)) {
43
            $details = getimagesize($path);
44
            switch ($details['mime']) {
45
                case 'image/gif':
46
                    $this->type = 'gif';
47
                    if (imagetypes() & IMG_GIF) {
48
                        $this->_resource = imagecreatefromgif($path);
49
                    }
50
                    break;
51
                case 'image/jpeg':
52
                    $this->type = 'jpg';
53
                    if (imagetypes() & IMG_JPG) {
54
                        $this->_resource = imagecreatefromjpeg($path);
55
                    }
56
                    break;
57
                case 'image/png':
58
                    $this->type = 'png';
59
                    if (imagetypes() & IMG_PNG) {
60
                        $this->_resource = imagecreatefrompng($path);
61
                    }
62
                    break;
63
            }
64
            $this->getWidth();
65
            $this->getHeight();
66
67
            return true;
68
        } else {
69
            trigger_error("Cannot find file $path", E_USER_ERROR);
70
        }
71
72
        return false;
73
    }
74
75
    /**
76
     * @return int
77
     */
78
    public function getWidth()
79
    {
80
        if (!$this->_width && $this->_resource) {
81
            $this->setWidth(imagesx($this->_resource));
82
        }
83
84
        return $this->_width;
85
    }
86
87
    /**
88
     * @param int $width
89
     */
90
    public function setWidth($width)
91
    {
92
        $this->_width = $width;
93
    }
94
95
    /**
96
     * @return int
97
     */
98
    public function getHeight()
99
    {
100
        if (!$this->_height && $this->_resource) {
101
            $this->setHeight(imagesy($this->_resource));
102
        }
103
104
        return $this->_height;
105
    }
106
107
    /**
108
     * @param int $height
109
     */
110
    public function setHeight($height)
111
    {
112
        $this->_height = $height;
113
    }
114
115
    /**
116
     * @param string $name
117
     */
118
    public function setBaseName($name)
119
    {
120
        $name = $name . '.' . $this->type;
121
        $this->setName($name);
122
    }
123
124
    /**
125
     * @param string $name
126
     */
127
    public function setName($name)
128
    {
129
        $this->name = $name;
130
        $this->url = dirname($this->url) . '/' . $this->name;
131
        $this->path = dirname($this->path) . '/' . $this->name;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function save()
138
    {
139
        if (Nip_File_System::instance()->createDirectory(dirname($this->path))) {
140
            switch ($this->type) {
141
                case 'png':
142
                    if ($this->quality > 9) {
143
                        if ($this->quality < 100) {
144
                            $this->quality = $this->quality / 10;
145
                        } else {
146
                            $this->quality = 9;
147
                        }
148
                    }
149
                    $this->quality = abs($this->quality - 9);
150
                    $this->quality = 0;
151
                    $newImg = imagecreatetruecolor($this->_width, $this->_height);
152
                    imagealphablending($newImg, false);
153
                    imagesavealpha($newImg, true);
154
                    imagecopyresampled($newImg, $this->_resource, 0, 0, 0, 0, $this->_width, $this->_height,
155
                        $this->_width, $this->_height);
156
                    $return = imagepng($newImg, $this->path, $this->quality);
157
                    break;
158
                case 'jpg':
159
                default:
160
                    $return = imagejpeg($this->_resource, $this->path, $this->quality);
161
                    break;
162
            }
163
            if ($return) {
164
                chmod($this->path, 0777);
165
                return true;
166
            }
167
            $this->errors[] = 'Error saving file';
168
        } else {
169
            $this->errors[] = 'Error creating directory';
170
        }
171
        return false;
172
    }
173
174
    public function grayscaleFade()
175
    {
176
        $this->grayscaleFilter();
177
        imagefilter($this->_resource, IMG_FILTER_BRIGHTNESS, 50);
178
    }
179
180
    public function grayscaleFilter()
181
    {
182
        imagefilter($this->_resource, IMG_FILTER_GRAYSCALE);
183
    }
184
185
    public function resize($max_width = false, $max_height = false)
186
    {
187
        if (!$max_width) {
188
            if ($this->max_width) {
189
                $max_width = $this->max_width;
190
            } else {
191
                $max_width = $this->getWidth();
192
            }
193
        }
194
        if (!$max_height) {
195
            if ($this->max_height) {
196
                $max_height = $this->max_height;
197
            } else {
198
                $max_height = $this->getHeight();
199
            }
200
        }
201
        $ratio = $this->getRatio();
202
        $target_ratio = $max_width / $max_height;
203
        if ($ratio > $target_ratio) {
204
            $new_width = $max_width;
205
            $new_height = round($max_width / $ratio);
206
        } else {
207
            $new_height = $max_height;
208
            $new_width = round($max_height * $ratio);
209
        }
210
        $image = imagecreatetruecolor($new_width, $new_height);
211
        imagealphablending($image, false);
212
        imagesavealpha($image, true);
213
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(),
214
            $this->getHeight());
215
        $this->_width = $new_width;
216
        $this->_height = $new_height;
217
        $this->_resource = $image;
218
219
        return $this;
220
    }
221
222
    public function getRatio()
223
    {
224
        return $this->getWidth() / $this->getHeight();
225
    }
226
227
    public function cropToCenter($cWidth, $cHeight)
228
    {
229
        $this->resizeToLarge($cWidth, $cHeight);
230
        $width = $this->getWidth();
231
        $height = $this->getHeight();
232
        $x0 = round(abs(($width - $cWidth) / 2), 0);
233
        $y0 = round(abs(($height - $cHeight) / 2), 0);
234
        $this->crop($x0, $y0, $cWidth, $cHeight, $cWidth, $cHeight);
235
    }
236
237
    /**
238
     * @param bool|int $max_width
239
     * @param bool|int $max_height
240
     * @return $this
241
     */
242
    public function resizeToLarge($max_width = false, $max_height = false)
243
    {
244
        if (!$max_width) {
245
            $max_width = $this->getWidth();
246
        }
247
        if (!$max_height) {
248
            $max_height = $this->getHeight();
249
        }
250
        $sourceRatio = $this->getRatio();
251
        $target_ratio = $max_width / $max_height;
252
        if ($sourceRatio > $target_ratio) {
253
            $new_height = $max_height;
254
            $new_width = (int)($max_height * $sourceRatio);
255
        } else {
256
            $new_width = $max_width;
257
            $new_height = (int)($max_width / $sourceRatio);
258
        }
259
        $image = imagecreatetruecolor($new_width, $new_height);
260
        imagealphablending($image, false);
261
        imagesavealpha($image, true);
262
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(),
263
            $this->getHeight());
264
        $this->_width = $new_width;
265
        $this->_height = $new_height;
266
        $this->_resource = $image;
267
268
        return $this;
269
    }
270
271
    /**
272
     * @param double $x
273
     * @param double $y
274
     * @param $dwidth
275
     * @param $dheight
276
     * @param $swidth
277
     * @param $sheight
278
     */
279
    public function crop($x, $y, $dwidth, $dheight, $swidth, $sheight)
280
    {
281
        $image = imagecreatetruecolor($dwidth, $dheight);
282
        imagealphablending($image, false);
283
        imagesavealpha($image, true);
284
        imagecopyresampled($image, $this->_resource,
285
            0, 0,
286
            $x, $y,
287
            $dwidth, $dheight,
288
            $swidth, $sheight);
289
        $this->_width = $dwidth;
290
        $this->_height = $dheight;
291
        $this->_resource = $image;
292
    }
293
294
    /**
295
     * @param int $amount
296
     * @param float $radius
297
     * @param int $threshold
298
     * @return $this
299
     */
300
    public function unsharpMask($amount = 80, $radius = 0.5, $threshold = 3)
301
    {
302
        $img = &$this->_resource;
303
        if ($amount > 500) {
304
            $amount = 500;
305
        }
306
        $amount = $amount * 0.016;
307
        if ($radius > 50) {
308
            $radius = 50;
309
        }
310
        $radius = $radius * 2;
311
        if ($threshold > 255) {
312
            $threshold = 255;
313
        }
314
        $radius = abs(round($radius));
315
        if ($radius == 0) {
316
            return;
317
        }
318
        $w = $this->_width;
319
        $h = $this->_height;
320
        $imgCanvas = imagecreatetruecolor($w, $h);
321
        $imgBlur = imagecreatetruecolor($w, $h);
322
        if (function_exists('imageconvolution')) {
323
            $matrix = [[1, 2, 1], [2, 4, 2], [1, 2, 1]];
324
            imagecopy($imgBlur, $img, 0, 0, 0, 0, $w, $h);
325
            imageconvolution($imgBlur, $matrix, 16, 0);
326
        } else {
327
            for ($i = 0; $i < $radius; $i++) {
328
                imagecopy($imgBlur, $img, 0, 0, 1, 0, $w - 1, $h);
329
                imagecopymerge($imgBlur, $img, 1, 0, 0, 0, $w, $h, 50);
330
                imagecopymerge($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50);
331
                imagecopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);
332
                imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333);
333
                imagecopymerge($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25);
334
            }
335
        }
336
        if ($threshold > 0) {
337
            for ($x = 0; $x < $w - 1; $x++) {
338
                for ($y = 0; $y < $h; $y++) {
339
                    $rgbOrig = ImageColorAt($img, $x, $y);
340
                    $rOrig = (($rgbOrig >> 16) & 0xFF);
341
                    $gOrig = (($rgbOrig >> 8) & 0xFF);
342
                    $bOrig = ($rgbOrig & 0xFF);
343
                    $rgbBlur = ImageColorAt($imgBlur, $x, $y);
344
                    $rBlur = (($rgbBlur >> 16) & 0xFF);
345
                    $gBlur = (($rgbBlur >> 8) & 0xFF);
346
                    $bBlur = ($rgbBlur & 0xFF);
347
                    $rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0,
348
                        min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
349
                    $gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0,
350
                        min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
351
                    $bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0,
352
                        min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
353
                    if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {
354
                        $pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
355
                        ImageSetPixel($img, $x, $y, $pixCol);
356
                    }
357
                }
358
            }
359
        } else {
360
            for ($x = 0; $x < $w; $x++) {
361
                for ($y = 0; $y < $h; $y++) {
362
                    $rgbOrig = ImageColorAt($img, $x, $y);
363
                    $rOrig = (($rgbOrig >> 16) & 0xFF);
364
                    $gOrig = (($rgbOrig >> 8) & 0xFF);
365
                    $bOrig = ($rgbOrig & 0xFF);
366
                    $rgbBlur = ImageColorAt($imgBlur, $x, $y);
367
                    $rBlur = (($rgbBlur >> 16) & 0xFF);
368
                    $gBlur = (($rgbBlur >> 8) & 0xFF);
369
                    $bBlur = ($rgbBlur & 0xFF);
370
                    $rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;
371
                    if ($rNew > 255) {
372
                        $rNew = 255;
373
                    } elseif ($rNew < 0) {
374
                        $rNew = 0;
375
                    }
376
                    $gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;
377
                    if ($gNew > 255) {
378
                        $gNew = 255;
379
                    } elseif ($gNew < 0) {
380
                        $gNew = 0;
381
                    }
382
                    $bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;
383
                    if ($bNew > 255) {
384
                        $bNew = 255;
385
                    } elseif ($bNew < 0) {
386
                        $bNew = 0;
387
                    }
388
                    $rgbNew = ($rNew << 16) + ($gNew << 8) + $bNew;
389
                    ImageSetPixel($img, $x, $y, $rgbNew);
390
                }
391
            }
392
        }
393
        imagedestroy($imgCanvas);
394
        imagedestroy($imgBlur);
395
396
        return $this;
397
    }
398
399
    /**
400
     * @param Image $image
401
     * @return $this
402
     */
403
    public function copyResource($image)
404
    {
405
        $this->_width = $image->getWidth();
406
        $this->_height = $image->getHeight();
407
        $this->_resource = $image->getResource();
408
409
        return $this;
410
    }
411
412
    public function getResource()
413
    {
414
        return $this->_resource;
415
    }
416
417
    public function setResource($gdImage)
418
    {
419
        $this->_resource = $gdImage;
420
    }
421
422
    /**
423
     * @return string
424
     */
425
    public function getFile()
426
    {
427
        return $this->_file;
428
    }
429
430
    /**
431
     * @return string
432
     */
433
    public function getExtension()
434
    {
435
        return Nip_File_System::instance()->getExtension($this->path);
436
    }
437
}
438