Resizer::resizeAndCrop()   B
last analyzed

Complexity

Conditions 11
Paths 88

Size

Total Lines 64
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 42
c 1
b 0
f 0
nc 88
nop 0
dl 0
loc 64
rs 7.3166

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\Wgfilemanager\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      module for xoops
19
 * @license        GPL 2.0 or later
20
 * @package        XOOPS common
21
 * @author         Goffy - Wedega - Email:<[email protected]> - Website:<https://wedega.com>
22
 * @version        $Id: 1.0 Resizer.php 1 Mon 2019-02-09 10:04:49Z XOOPS Project (www.xoops.org) $
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
        switch ($this->imageMimetype) {
45
            case 'image/png':
46
                $img = \imagecreatefrompng($this->sourceFile);
47
                break;
48
            case 'image/jpeg':
49
                $img = \imagecreatefromjpeg($this->sourceFile);
50
                if (!$img) {
51
                    $img = \imagecreatefromstring(\file_get_contents($this->sourceFile));
52
                }
53
                break;
54
            case 'image/gif':
55
                $img = \imagecreatefromgif($this->sourceFile);
56
                break;
57
            default:
58
                return 'Unsupported format';
59
        }
60
61
        $width  = \imagesx($img);
62
        $height = \imagesy($img);
63
64
        if ($width > $this->maxWidth || $height > $this->maxHeight) {
65
            // recalc image size based on this->maxWidth/this->maxHeight
66
            $new_width  = 0;
67
            $new_height = 0;
68
            if ($width > $height) {
69
                if ($width < $this->maxWidth) {
70
                    $new_width = $width;
71
                } else {
72
                    $new_width  = $this->maxWidth;
73
                    $divisor    = $width / $new_width;
74
                    $new_height = \floor($height / $divisor);
75
                }
76
            } elseif ($height < $this->maxHeight) {
77
                $new_height = $height;
78
            } else {
79
                $new_height = $this->maxHeight;
80
                $divisor    = $height / $new_height;
81
                $new_width  = \floor($width / $divisor);
82
            }
83
84
            // Create a new temporary image.
85
            $tmpimg = \imagecreatetruecolor($new_width, $new_height);
0 ignored issues
show
Bug introduced by
It seems like $new_width can also be of type double; 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

85
            $tmpimg = \imagecreatetruecolor(/** @scrutinizer ignore-type */ $new_width, $new_height);
Loading history...
Bug introduced by
It seems like $new_height can also be of type double; 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

85
            $tmpimg = \imagecreatetruecolor($new_width, /** @scrutinizer ignore-type */ $new_height);
Loading history...
86
            \imagealphablending($tmpimg, false);
87
            \imagesavealpha($tmpimg, true);
88
89
            // Copy and resize old image into new image.
90
            \imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
0 ignored issues
show
Bug introduced by
It seems like $new_width can also be of type double; 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

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

90
            \imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, /** @scrutinizer ignore-type */ $new_height, $width, $height);
Loading history...
91
92
            \unlink($this->endFile);
93
            //compressing the file
94
            switch ($this->imageMimetype) {
95
                case 'image/png':
96
                    \imagepng($tmpimg, $this->endFile, 0);
97
                    break;
98
                case 'image/jpeg':
99
                    \imagejpeg($tmpimg, $this->endFile, 100);
100
                    break;
101
                case 'image/gif':
102
                    \imagegif($tmpimg, $this->endFile);
103
                    break;
104
            }
105
106
            // release the memory
107
            \imagedestroy($tmpimg);
108
        } else {
109
            return 'copy';
110
        }
111
        \imagedestroy($img);
112
113
        return true;
114
    }
115
116
    /**
117
     * @return bool|string
118
     */
119
    public function resizeAndCrop()
120
    {
121
        // check file extension
122
        switch ($this->imageMimetype) {
123
            case 'image/png':
124
                $original = \imagecreatefrompng($this->sourceFile);
125
                break;
126
            case 'image/jpeg':
127
                $original = \imagecreatefromjpeg($this->sourceFile);
128
                break;
129
            case 'image/gif':
130
                $original = \imagecreatefromgif($this->sourceFile);
131
                break;
132
            default:
133
                return 'Unsupported format';
134
        }
135
136
        if (!$original) {
137
            return false;
138
        }
139
        // GET ORIGINAL IMAGE DIMENSIONS
140
        [$original_w, $original_h] = \getimagesize($this->sourceFile);
141
142
        // RESIZE IMAGE AND PRESERVE PROPORTIONS
143
        $max_width_resize  = $this->maxWidth;
144
        $max_height_resize = $this->maxHeight;
145
        if ($original_w > $original_h) {
146
            $max_height_ratio = $this->maxHeight / $original_h;
147
            $max_width_resize = (int)\round($original_w * $max_height_ratio);
148
        } else {
149
            $max_width_ratio   = $this->maxWidth / $original_w;
150
            $max_height_resize = (int)\round($original_h * $max_width_ratio);
151
        }
152
        if ($max_width_resize < $this->maxWidth) {
153
            $max_height_ratio  = $this->maxWidth / $max_width_resize;
154
            $max_height_resize = (int)\round($this->maxHeight * $max_height_ratio);
155
            $max_width_resize  = $this->maxWidth;
156
        }
157
158
        // CREATE THE PROPORTIONAL IMAGE RESOURCE
159
        $thumb = \imagecreatetruecolor($max_width_resize, $max_height_resize);
160
        if (!\imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $max_height_resize, $original_w, $original_h)) {
161
            return false;
162
        }
163
        // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
164
        $final = \imagecreatetruecolor($this->maxWidth, $this->maxHeight);
165
166
        $max_width_offset  = 0;
167
        $max_height_offset = 0;
168
        if ($this->maxWidth < $max_width_resize) {
169
            $max_width_offset = (int)\round(($max_width_resize - $this->maxWidth) / 2);
170
        } else {
171
            $max_height_offset = (int)\round(($max_height_resize - $this->maxHeight) / 2);
172
        }
173
174
        if (!\imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $max_height_resize)) {
175
            return false;
176
        }
177
        // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile
178
        if (!\imagejpeg($final, $this->endFile, $this->jpgQuality)) {
179
            return false;
180
        }
181
182
        return true;
183
    }
184
185
    public function mergeImage()
186
    {
187
        $dest = \imagecreatefromjpeg($this->endFile);
188
        $src  = \imagecreatefromjpeg($this->sourceFile);
189
        if (4 == $this->mergeType) {
190
            $imgWidth  = (int)\round($this->maxWidth / 2 - 1);
191
            $imgHeight = (int)\round($this->maxHeight / 2 - 1);
192
            $posCol2   = (int)\round($this->maxWidth / 2 + 1);
193
            $posRow2   = (int)\round($this->maxHeight / 2 + 1);
194
            switch ($this->mergePos) {
195
                case 1:
196
                    \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
197
                    break;
198
                case 2:
199
                    \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right
200
                    break;
201
                case 3:
202
                    \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left
203
                    break;
204
                case 4:
205
                    \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right
206
                    break;
207
            }
208
        }
209
        if (6 == $this->mergeType) {
210
            $imgWidth  = (int)\round($this->maxWidth / 3 - 1);
211
            $imgHeight = (int)\round($this->maxHeight / 2 - 1);
212
            $posCol2   = (int)\round($this->maxWidth / 3 + 1);
213
            $posCol3   = $posCol2 + (int)\round($this->maxWidth / 3 + 1);
214
            $posRow2   = (int)\round($this->maxHeight / 2 + 1);
215
216
            switch ($this->mergePos) {
217
                case 1:
218
                    \imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
219
                    break;
220
                case 2:
221
                    \imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center
222
                    break;
223
                case 3:
224
                    \imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right
225
                    break;
226
                case 4:
227
                    \imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left
228
                    break;
229
                case 5:
230
                    \imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center
231
                    break;
232
                case 6:
233
                    \imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right
234
                    break;
235
            }
236
        }
237
        \imagejpeg($dest, $this->endFile);
238
239
        \imagedestroy($src);
240
        \imagedestroy($dest);
241
    }
242
243
    /**
244
     * @return bool|string
245
     */
246
    public function rotateImage()
247
    {
248
        // check file extension
249
        switch ($this->imageMimetype) {
250
            case 'image/png':
251
                $original = \imagecreatefrompng($this->sourceFile);
252
                break;
253
            case 'image/jpeg':
254
                $original = \imagecreatefromjpeg($this->sourceFile);
255
                break;
256
            case 'image/gif':
257
                $original = \imagecreatefromgif($this->sourceFile);
258
                break;
259
            default:
260
                return 'Unsupported format';
261
        }
262
263
        if (!$original) {
264
            return false;
265
        }
266
        // Rotate
267
        $tmpimg = \imagerotate($original, $this->degrees, 0);
268
269
        \unlink($this->endFile);
270
        //compressing the file
271
        switch ($this->imageMimetype) {
272
            case 'image/png':
273
                if (!\imagepng($tmpimg, $this->endFile, 0)) {
274
                    return false;
275
                }
276
                break;
277
            case 'image/jpeg':
278
                if (!\imagejpeg($tmpimg, $this->endFile, $this->jpgQuality)) {
279
                    return false;
280
                }
281
                break;
282
            case 'image/gif':
283
                if (!\imagegif($tmpimg, $this->endFile)) {
284
                    return false;
285
                }
286
                break;
287
        }
288
289
        // release the memory
290
        \imagedestroy($tmpimg);
291
292
        return true;
293
    }
294
}
295