Resizer::resizeAndCrop()   B
last analyzed

Complexity

Conditions 11
Paths 88

Size

Total Lines 83
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 42
nc 88
nop 0
dl 0
loc 83
rs 7.3166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace XoopsModules\Chess\Common;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
*/
14
15
/**
16
 * Image resizer class for xoops
17
 *
18
 * @copyright      2000-2020 XOOPS Project (https://xoops.org)
19
 * @license        GPL 2.0 or later
20
 * @package      Chess
21
 * @since        2.01
22
 * @author         Goffy - Wedega - Email:<[email protected]> - Website:<https://wedega.com>
23
 */
24
class Resizer
25
{
26
    public $sourceFile = '';
27
    public $endFile = '';
28
    public $maxWidth = 0;
29
    public $maxHeight = 0;
30
    public $imageMimetype = '';
31
    public $jpgQuality = 90;
32
    public $mergeType = 0;
33
    public $mergePos = 0;
34
    public $degrees = 0;
35
    public $error = '';
36
37
    /**
38
     * resize image if size exceed given width/height
39
     * @return string|bool
40
     */
41
    public function resizeImage()
42
    {
43
        // check file extension
44
45
        switch ($this->imageMimetype) {
46
            case 'image/png':
47
                $img = \imagecreatefrompng($this->sourceFile);
48
                break;
49
            case 'image/jpeg':
50
                $img = \imagecreatefromjpeg($this->sourceFile);
51
                if (!$img) {
0 ignored issues
show
introduced by
$img is of type false|resource, thus it always evaluated to false.
Loading history...
52
                    $img = \imagecreatefromstring(file_get_contents($this->sourceFile));
53
                }
54
                break;
55
            case 'image/gif':
56
                $img = \imagecreatefromgif($this->sourceFile);
57
                break;
58
            default:
59
                return 'Unsupported format';
60
        }
61
62
        $width = \imagesx($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesx() 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

62
        $width = \imagesx(/** @scrutinizer ignore-type */ $img);
Loading history...
63
64
        $height = \imagesy($img);
0 ignored issues
show
Bug introduced by
It seems like $img can also be of type false; however, parameter $image of imagesy() 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

64
        $height = \imagesy(/** @scrutinizer ignore-type */ $img);
Loading history...
65
66
        if ($width > $this->maxWidth || $height > $this->maxHeight) {
67
            // recalc image size based on this->maxWidth/this->maxHeight
68
69
            if ($width > $height) {
70
                if ($width < $this->maxWidth) {
71
                    $new_width = $width;
72
                } else {
73
                    $new_width = $this->maxWidth;
74
75
                    $divisor = $width / $new_width;
76
77
                    $new_height = \floor($height / $divisor);
78
                }
79
            } elseif ($height < $this->maxHeight) {
80
                $new_height = $height;
81
            } else {
82
                $new_height = $this->maxHeight;
83
84
                $divisor = $height / $new_height;
85
86
                $new_width = \floor($width / $divisor);
87
            }
88
89
            // Create a new temporary image.
90
91
            $tmpimg = \imagecreatetruecolor($new_width, $new_height);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $new_height does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $new_width does not seem to be defined for all execution paths leading up to this point.
Loading history...
92
93
            imagealphablending($tmpimg, false);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg 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

93
            imagealphablending(/** @scrutinizer ignore-type */ $tmpimg, false);
Loading history...
94
95
            imagesavealpha($tmpimg, true);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg 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

95
            imagesavealpha(/** @scrutinizer ignore-type */ $tmpimg, true);
Loading history...
96
97
            // Copy and resize old image into new image.
98
99
            \imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg 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

99
            \imagecopyresampled(/** @scrutinizer ignore-type */ $tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Loading history...
Bug introduced by
It seems like $img can also be of type false; however, parameter $src_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

99
            \imagecopyresampled($tmpimg, /** @scrutinizer ignore-type */ $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Loading history...
100
101
            \unlink($this->endFile);
102
103
            //compressing the file
104
105
            switch ($this->imageMimetype) {
106
                case 'image/png':
107
                    \imagepng($tmpimg, $this->endFile, 0);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg 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

107
                    \imagepng(/** @scrutinizer ignore-type */ $tmpimg, $this->endFile, 0);
Loading history...
108
                    break;
109
                case 'image/jpeg':
110
                    \imagejpeg($tmpimg, $this->endFile, 100);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg can also be of type false; however, parameter $image of imagejpeg() 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

110
                    \imagejpeg(/** @scrutinizer ignore-type */ $tmpimg, $this->endFile, 100);
Loading history...
111
                    break;
112
                case 'image/gif':
113
                    \imagegif($tmpimg, $this->endFile);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg can also be of type false; however, parameter $image of imagegif() 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

113
                    \imagegif(/** @scrutinizer ignore-type */ $tmpimg, $this->endFile);
Loading history...
114
                    break;
115
            }
116
117
            // release the memory
118
119
            \imagedestroy($tmpimg);
0 ignored issues
show
Bug introduced by
It seems like $tmpimg 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

119
            \imagedestroy(/** @scrutinizer ignore-type */ $tmpimg);
Loading history...
120
        } else {
121
            return 'copy';
122
        }
123
124
        \imagedestroy($img);
125
126
        return true;
127
    }
128
129
    /**
130
     * @return bool|string
131
     */
132
    public function resizeAndCrop()
133
    {
134
        // check file extension
135
136
        switch ($this->imageMimetype) {
137
            case 'image/png':
138
                $original = \imagecreatefrompng($this->sourceFile);
139
                break;
140
            case 'image/jpeg':
141
                $original = \imagecreatefromjpeg($this->sourceFile);
142
                break;
143
            case 'image/gif':
144
                $original = \imagecreatefromgif($this->sourceFile);
145
                break;
146
            default:
147
                return 'Unsupported format';
148
        }
149
150
        if (!$original) {
0 ignored issues
show
introduced by
$original is of type false|resource, thus it always evaluated to false.
Loading history...
151
            return false;
152
        }
153
154
        // GET ORIGINAL IMAGE DIMENSIONS
155
156
        [$original_w, $original_h] = \getimagesize($this->sourceFile);
157
158
        // RESIZE IMAGE AND PRESERVE PROPORTIONS
159
160
        $max_width_resize = $this->maxWidth;
161
162
        $max_height_resize = $this->maxHeight;
163
164
        if ($original_w > $original_h) {
165
            $max_height_ratio = $this->maxHeight / $original_h;
166
167
            $max_width_resize = (int)\round($original_w * $max_height_ratio);
168
        } else {
169
            $max_width_ratio = $this->maxWidth / $original_w;
170
171
            $max_height_resize = (int)\round($original_h * $max_width_ratio);
172
        }
173
174
        if ($max_width_resize < $this->maxWidth) {
175
            $max_height_ratio = $this->maxWidth / $max_width_resize;
176
177
            $max_height_resize = (int)\round($this->maxHeight * $max_height_ratio);
178
179
            $max_width_resize = $this->maxWidth;
180
        }
181
182
        // CREATE THE PROPORTIONAL IMAGE RESOURCE
183
184
        $thumb = \imagecreatetruecolor($max_width_resize, $max_height_resize);
185
186
        if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $max_height_resize, $original_w, $original_h)) {
187
            return false;
188
        }
189
190
        // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
191
192
        $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight);
193
194
        $max_width_offset = 0;
195
196
        $max_height_offset = 0;
197
198
        if ($this->maxWidth < $max_width_resize) {
199
            $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2);
200
        } else {
201
            $max_height_offset = (int)\round(($max_height_resize - $this->maxHeight) / 2);
202
        }
203
204
        if (!\imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $max_height_resize)) {
205
            return false;
206
        }
207
208
        // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile
209
210
        if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) {
211
            return false;
212
        }
213
214
        return true;
215
    }
216
217
    public function mergeImage()
218
    {
219
        $dest = \imagecreatefromjpeg($this->endFile);
220
221
        $src = \imagecreatefromjpeg($this->sourceFile);
222
223
        if (4 == $this->mergeType) {
224
            $imgWidth = (int)\round($this->maxWidth / 2 - 1);
225
226
            $imgHeight = (int)\round($this->maxHeight / 2 - 1);
227
228
            $posCol2 = (int)\round($this->maxWidth / 2 + 1);
229
230
            $posRow2 = (int)\round($this->maxHeight / 2 + 1);
231
232
            switch ($this->mergePos) {
233
                case 1:
234
                    \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
0 ignored issues
show
Bug introduced by
It seems like $dest 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

234
                    \imagecopy(/** @scrutinizer ignore-type */ $dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
Loading history...
Bug introduced by
It seems like $src 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

234
                    \imagecopy($dest, /** @scrutinizer ignore-type */ $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
Loading history...
235
                    break;
236
                case 2:
237
                    \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right
238
                    break;
239
                case 3:
240
                    \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left
241
                    break;
242
                case 4:
243
                    \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right
244
                    break;
245
            }
246
        }
247
248
        if (6 == $this->mergeType) {
249
            $imgWidth = (int)\round($this->maxWidth / 3 - 1);
250
251
            $imgHeight = (int)\round($this->maxHeight / 2 - 1);
252
253
            $posCol2 = (int)\round($this->maxWidth / 3 + 1);
254
255
            $posCol3 = $posCol2 + (int)\round($this->maxWidth / 3 + 1);
256
257
            $posRow2 = (int)\round($this->maxHeight / 2 + 1);
258
259
            switch ($this->mergePos) {
260
                case 1:
261
                    \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
262
                    break;
263
                case 2:
264
                    \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center
265
                    break;
266
                case 3:
267
                    \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right
268
                    break;
269
                case 4:
270
                    \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left
271
                    break;
272
                case 5:
273
                    \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center
274
                    break;
275
                case 6:
276
                    \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right
277
                    break;
278
            }
279
        }
280
281
        \imagejpeg($dest, $this->endFile);
0 ignored issues
show
Bug introduced by
It seems like $dest can also be of type false; however, parameter $image of imagejpeg() 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

281
        \imagejpeg(/** @scrutinizer ignore-type */ $dest, $this->endFile);
Loading history...
282
283
        \imagedestroy($src);
0 ignored issues
show
Bug introduced by
It seems like $src 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

283
        \imagedestroy(/** @scrutinizer ignore-type */ $src);
Loading history...
284
285
        \imagedestroy($dest);
286
    }
287
288
    /**
289
     * @return bool|string
290
     */
291
    public function rotateImage()
292
    {
293
        // check file extension
294
295
        switch ($this->imageMimetype) {
296
            case 'image/png':
297
                $original = \imagecreatefrompng($this->sourceFile);
298
                break;
299
            case 'image/jpeg':
300
                $original = \imagecreatefromjpeg($this->sourceFile);
301
                break;
302
            case 'image/gif':
303
                $original = \imagecreatefromgif($this->sourceFile);
304
                break;
305
            default:
306
                return 'Unsupported format';
307
        }
308
309
        if (!$original) {
0 ignored issues
show
introduced by
$original is of type false|resource, thus it always evaluated to false.
Loading history...
310
            return false;
311
        }
312
313
        // Rotate
314
315
        $tmpimg = \imagerotate($original, $this->degrees, 0);
316
317
        \unlink($this->endFile);
318
319
        //compressing the file
320
321
        switch ($this->imageMimetype) {
322
            case 'image/png':
323
                if (!\imagepng($tmpimg, $this->endFile, 0)) {
324
                    return false;
325
                }
326
                break;
327
            case 'image/jpeg':
328
                if (!\imagejpeg($tmpimg, $this->endFile, $this->jpgQuality)) {
329
                    return false;
330
                }
331
                break;
332
            case 'image/gif':
333
                if (!\imagegif($tmpimg, $this->endFile)) {
334
                    return false;
335
                }
336
                break;
337
        }
338
339
        // release the memory
340
341
        \imagedestroy($tmpimg);
342
343
        return true;
344
    }
345
}
346