Passed
Push — master ( 5ccd86...1b90b9 )
by Gabriel
03:29
created

Nip_File_Image   F

Complexity

Total Complexity 70

Size/Duplication

Total Lines 462
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 70
eloc 228
c 0
b 0
f 0
dl 0
loc 462
rs 2.8

22 Methods

Rating   Name   Duplication   Size   Complexity  
A setResource() 0 3 1
A getWidth() 0 7 3
A setName() 0 5 1
A resizeToLarge() 0 32 4
B save() 0 40 7
A grayscaleFilter() 0 3 1
A copyResource() 0 7 1
B resize() 0 40 6
A crop() 0 15 1
B setResourceFromFile() 0 36 8
A getFile() 0 3 1
A getRatio() 0 3 1
A getExtension() 0 3 1
A setHeight() 0 3 1
A getResource() 0 3 1
A setBaseName() 0 4 1
A cropToCenter() 0 11 1
F unsharpMask() 0 109 24
A setWidth() 0 3 1
A getHeight() 0 7 3
A setResourceFromUpload() 0 4 1
A grayscaleFade() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Nip_File_Image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Nip_File_Image, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * Nip Framework.
5
 *
6
 * @category   Nip
7
 *
8
 * @copyright  2009 Nip Framework
9
 * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
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
0 ignored issues
show
Bug introduced by
The type Nip_File_Handler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
134
        $this->url = dirname($this->url).'/'.$this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
135
        $this->path = dirname($this->path).'/'.$this->name;
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
136
    }
137
138
    /**
139
     * @return bool
140
     */
141
    public function save()
142
    {
143
        if (Nip_File_System::instance()->createDirectory(dirname($this->path))) {
0 ignored issues
show
Bug introduced by
The type Nip_File_System was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $newImg can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

157
                    imagealphablending(/** @scrutinizer ignore-type */ $newImg, false);
Loading history...
158
                    imagesavealpha($newImg, true);
0 ignored issues
show
Bug introduced by
It seems like $newImg can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

158
                    imagesavealpha(/** @scrutinizer ignore-type */ $newImg, true);
Loading history...
159
160
                    imagecopyresampled($newImg, $this->_resource, 0, 0, 0, 0, $this->_width, $this->_height, $this->_width, $this->_height);
0 ignored issues
show
Bug introduced by
It seems like $newImg can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

160
                    imagecopyresampled(/** @scrutinizer ignore-type */ $newImg, $this->_resource, 0, 0, 0, 0, $this->_width, $this->_height, $this->_width, $this->_height);
Loading history...
161
162
                    $return = imagepng($newImg, $this->path, $this->quality);
0 ignored issues
show
Bug introduced by
It seems like $newImg can also be of type false; however, parameter $image of imagepng() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

162
                    $return = imagepng(/** @scrutinizer ignore-type */ $newImg, $this->path, $this->quality);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

224
        imagealphablending(/** @scrutinizer ignore-type */ $image, false);
Loading history...
225
        imagesavealpha($image, true);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

225
        imagesavealpha(/** @scrutinizer ignore-type */ $image, true);
Loading history...
226
227
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

227
        imagecopyresampled(/** @scrutinizer ignore-type */ $image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $new_height can also be of type boolean; however, parameter $height of imagecreatetruecolor() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

281
        $image = imagecreatetruecolor($new_width, /** @scrutinizer ignore-type */ $new_height);
Loading history...
Bug introduced by
It seems like $new_width can also be of type boolean; however, parameter $width of imagecreatetruecolor() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

281
        $image = imagecreatetruecolor(/** @scrutinizer ignore-type */ $new_width, $new_height);
Loading history...
282
        imagealphablending($image, false);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

282
        imagealphablending(/** @scrutinizer ignore-type */ $image, false);
Loading history...
283
        imagesavealpha($image, true);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

283
        imagesavealpha(/** @scrutinizer ignore-type */ $image, true);
Loading history...
284
285
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

285
        imagecopyresampled(/** @scrutinizer ignore-type */ $image, $this->_resource, 0, 0, 0, 0, $new_width, $new_height, $this->getWidth(), $this->getHeight());
Loading history...
Bug introduced by
It seems like $new_height can also be of type boolean; however, parameter $dst_h of imagecopyresampled() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

285
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, $new_width, /** @scrutinizer ignore-type */ $new_height, $this->getWidth(), $this->getHeight());
Loading history...
Bug introduced by
It seems like $new_width can also be of type boolean; however, parameter $dst_w of imagecopyresampled() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

285
        imagecopyresampled($image, $this->_resource, 0, 0, 0, 0, /** @scrutinizer ignore-type */ $new_width, $new_height, $this->getWidth(), $this->getHeight());
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagealphablending() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

305
        imagealphablending(/** @scrutinizer ignore-type */ $image, false);
Loading history...
306
        imagesavealpha($image, true);
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $image of imagesavealpha() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

306
        imagesavealpha(/** @scrutinizer ignore-type */ $image, true);
Loading history...
307
308
        imagecopyresampled($image, $this->_resource,
0 ignored issues
show
Bug introduced by
It seems like $image can also be of type false; however, parameter $dst_image of imagecopyresampled() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

308
        imagecopyresampled(/** @scrutinizer ignore-type */ $image, $this->_resource,
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $imgBlur can also be of type false; however, parameter $dst_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

355
            imagecopy(/** @scrutinizer ignore-type */ $imgBlur, $img, 0, 0, 0, 0, $w, $h);
Loading history...
356
            imageconvolution($imgBlur, $matrix, 16, 0);
0 ignored issues
show
Bug introduced by
It seems like $imgBlur can also be of type false; however, parameter $image of imageconvolution() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

356
            imageconvolution(/** @scrutinizer ignore-type */ $imgBlur, $matrix, 16, 0);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $imgBlur can also be of type false; however, parameter $dst_im of imagecopymerge() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

360
                imagecopymerge(/** @scrutinizer ignore-type */ $imgBlur, $img, 1, 0, 0, 0, $w, $h, 50);
Loading history...
361
                imagecopymerge($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50);
362
                imagecopy($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);
0 ignored issues
show
Bug introduced by
It seems like $imgBlur can also be of type false; however, parameter $src_im of imagecopy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

362
                imagecopy($imgCanvas, /** @scrutinizer ignore-type */ $imgBlur, 0, 0, 0, 0, $w, $h);
Loading history...
363
364
                imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333);
0 ignored issues
show
Bug introduced by
It seems like $imgCanvas can also be of type false; however, parameter $src_im of imagecopymerge() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

364
                imagecopymerge($imgBlur, /** @scrutinizer ignore-type */ $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333);
Loading history...
Bug introduced by
33.33333 of type double is incompatible with the type integer expected by parameter $pct of imagecopymerge(). ( Ignorable by Annotation )

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

364
                imagecopymerge($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, /** @scrutinizer ignore-type */ 33.33333);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $imgBlur can also be of type false; however, parameter $image of imagecolorat() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

377
                    $rgbBlur = imagecolorat(/** @scrutinizer ignore-type */ $imgBlur, $x, $y);
Loading history...
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);
0 ignored issues
show
Bug introduced by
$rgbNew of type double is incompatible with the type integer expected by parameter $color of imagesetpixel(). ( Ignorable by Annotation )

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

426
                    imagesetpixel($img, $x, $y, /** @scrutinizer ignore-type */ $rgbNew);
Loading history...
427
                }
428
            }
429
        }
430
431
        imagedestroy($imgCanvas);
0 ignored issues
show
Bug introduced by
It seems like $imgCanvas can also be of type false; however, parameter $image of imagedestroy() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

431
        imagedestroy(/** @scrutinizer ignore-type */ $imgCanvas);
Loading history...
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