Resizer   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 147
dl 0
loc 213
rs 9.52
c 0
b 0
f 0
wmc 36

3 Methods

Rating   Name   Duplication   Size   Complexity  
B resizeAndCrop() 0 64 11
C resizeImage() 0 68 12
C mergeImage() 0 56 13
1
<?php
2
3
namespace XoopsModules\Mymodule\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
 * wgGallery module for xoops
17
 *
18
 * @copyright      module for xoops
19
 * @license        GPL 2.0 or later
20
 * @package        wggallery
21
 * @since          1.0
22
 * @min_xoops      2.5.9
23
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
24
 */
25
class Resizer
26
{
27
    public $sourceFile    = '';
28
    public $endFile       = '';
29
    public $maxWidth      = 0;
30
    public $maxHeight     = 0;
31
    public $imageMimetype = '';
32
    public $jpgQuality    = 90;
33
    public $mergeType     = 0;
34
    public $mergePos      = 0;
35
36
    /**
37
     * resize image if size exceed given width/height
38
     * @return string|bool
39
     */
40
    public function resizeImage()
41
    {
42
        // check file extension
43
        switch ($this->imageMimetype) {
44
            case 'image/png':
45
                $img = imagecreatefrompng($this->sourceFile);
46
                break;
47
            case 'image/jpeg':
48
                $img = imagecreatefromjpeg($this->sourceFile);
49
                break;
50
            case 'image/gif':
51
                $img = imagecreatefromgif($this->sourceFile);
52
                break;
53
            default:
54
                return 'Unsupported format';
55
        }
56
57
        $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

57
        $width  = imagesx(/** @scrutinizer ignore-type */ $img);
Loading history...
58
        $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

58
        $height = imagesy(/** @scrutinizer ignore-type */ $img);
Loading history...
59
60
        if ($width > $this->maxWidth || $height > $this->maxHeight) {
61
            // recalc image size based on this->maxWidth/this->maxHeight
62
            if ($width > $height) {
63
                if ($width < $this->maxWidth) {
64
                    $new_width = $width;
65
                } else {
66
                    $new_width  = $this->maxWidth;
67
                    $divisor    = $width / $new_width;
68
                    $new_height = floor($height / $divisor);
69
                }
70
            } elseif ($height < $this->maxHeight) {
71
                $new_height = $height;
72
            } else {
73
                $new_height = $this->maxHeight;
74
                $divisor    = $height / $new_height;
75
                $new_width  = floor($width / $divisor);
76
            }
77
78
            // Create a new temporary image.
79
            $tmpimg = imagecreatetruecolor($new_width, $new_height);
0 ignored issues
show
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...
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...
80
            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

80
            imagealphablending(/** @scrutinizer ignore-type */ $tmpimg, false);
Loading history...
81
            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

81
            imagesavealpha(/** @scrutinizer ignore-type */ $tmpimg, true);
Loading history...
82
83
            // Copy and resize old image into new image.
84
            imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
0 ignored issues
show
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

84
            imagecopyresampled($tmpimg, /** @scrutinizer ignore-type */ $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Loading history...
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

84
            imagecopyresampled(/** @scrutinizer ignore-type */ $tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Loading history...
85
86
            unlink($this->endFile);
87
            //compressing the file
88
            switch ($this->imageMimetype) {
89
                case 'image/png':
90
                    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

90
                    imagepng(/** @scrutinizer ignore-type */ $tmpimg, $this->endFile, 0);
Loading history...
91
                    break;
92
                case 'image/jpeg':
93
                    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

93
                    imagejpeg(/** @scrutinizer ignore-type */ $tmpimg, $this->endFile, 100);
Loading history...
94
                    break;
95
                case 'image/gif':
96
                    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

96
                    imagegif(/** @scrutinizer ignore-type */ $tmpimg, $this->endFile);
Loading history...
97
                    break;
98
            }
99
100
            // release the memory
101
            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

101
            imagedestroy(/** @scrutinizer ignore-type */ $tmpimg);
Loading history...
102
        } else {
103
            return 'copy';
104
        }
105
        imagedestroy($img);
106
107
        return true;
108
    }
109
110
    // public function resizeAndCrop($this->sourceFile, $this->imageMimetype, $this->endFile, $this->maxWidth, $this->maxHeight, $this->jpgQuality=90)
111
112
    /**
113
     * @return bool|string
114
     */
115
    public function resizeAndCrop()
116
    {
117
        // check file extension
118
        switch ($this->imageMimetype) {
119
            case 'image/png':
120
                $original = imagecreatefrompng($this->sourceFile);
121
                break;
122
            case 'image/jpeg':
123
                $original = imagecreatefromjpeg($this->sourceFile);
124
                break;
125
            case 'image/gif':
126
                $original = imagecreatefromgif($this->sourceFile);
127
                break;
128
            default:
129
                return 'Unsupported format';
130
        }
131
132
        if (!$original) {
0 ignored issues
show
introduced by
$original is of type false|resource, thus it always evaluated to false.
Loading history...
133
            return false;
134
        }
135
        // GET ORIGINAL IMAGE DIMENSIONS
136
        list($original_w, $original_h) = getimagesize($this->sourceFile);
137
138
        // RESIZE IMAGE AND PRESERVE PROPORTIONS
139
        $max_width_resize  = $this->maxWidth;
140
        $max_height_resize = $this->maxHeight;
141
        if ($original_w > $original_h) {
142
            $max_height_ratio = $this->maxHeight / $original_h;
143
            $max_width_resize = (int)round($original_w * $max_height_ratio);
144
        } else {
145
            $max_width_ratio   = $this->maxWidth / $original_w;
146
            $max_height_resize = (int)round($original_h * $max_width_ratio);
147
        }
148
        if ($max_width_resize < $this->maxWidth) {
149
            $max_height_ratio  = $this->maxWidth / $max_width_resize;
150
            $max_height_resize = (int)round($this->maxHeight * $max_height_ratio);
151
            $max_width_resize  = $this->maxWidth;
152
        }
153
154
        // CREATE THE PROPORTIONAL IMAGE RESOURCE
155
        $thumb = imagecreatetruecolor($max_width_resize, $max_height_resize);
156
        if (!imagecopyresampled($thumb, $original, 0, 0, 0, 0, $max_width_resize, $max_height_resize, $original_w, $original_h)) {
157
            return false;
158
        }
159
        // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
160
        $final = imagecreatetruecolor($this->maxWidth, $this->maxHeight);
161
162
        $max_width_offset  = 0;
163
        $max_height_offset = 0;
164
        if ($this->maxWidth < $max_width_resize) {
165
            $max_width_offset = (int)round(($max_width_resize - $this->maxWidth) / 2);
166
        } else {
167
            $max_height_offset = (int)round(($max_height_resize - $this->maxHeight) / 2);
168
        }
169
170
        if (!imagecopy($final, $thumb, 0, 0, $max_width_offset, $max_height_offset, $max_width_resize, $max_height_resize)) {
171
            return false;
172
        }
173
        // STORE THE FINAL IMAGE - WILL OVERWRITE $this->endFile
174
        if (!imagejpeg($final, $this->endFile, $this->jpgQuality)) {
175
            return false;
176
        }
177
178
        return true;
179
    }
180
181
    // public function mergeImage($this->sourceFile, $this->endFile, $this->mergePos, $this->mergeType)
182
    public function mergeImage()
183
    {
184
        $dest = imagecreatefromjpeg($this->endFile);
185
        $src  = imagecreatefromjpeg($this->sourceFile);
186
        if (4 == $this->mergeType) {
187
            $imgWidth  = (int)round($this->maxWidth / 2 - 1);
188
            $imgHeight = (int)round($this->maxHeight / 2 - 1);
189
            $posCol2   = (int)round($this->maxWidth / 2 + 1);
190
            $posRow2   = (int)round($this->maxHeight / 2 + 1);
191
            switch ($this->mergePos) {
192
                case 1:
193
                    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

193
                    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

193
                    imagecopy($dest, /** @scrutinizer ignore-type */ $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
Loading history...
194
                    break;
195
                case 2:
196
                    imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top right
197
                    break;
198
                case 3:
199
                    imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left
200
                    break;
201
                case 4:
202
                    imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right
203
                    break;
204
            }
205
        }
206
        if (6 == $this->mergeType) {
207
            $imgWidth  = (int)round($this->maxWidth / 3 - 1);
208
            $imgHeight = (int)round($this->maxHeight / 2 - 1);
209
            $posCol2   = (int)round($this->maxWidth / 3 + 1);
210
            $posCol3   = $posCol2 + (int)round($this->maxWidth / 3 + 1);
211
            $posRow2   = (int)round($this->maxHeight / 2 + 1);
212
213
            switch ($this->mergePos) {
214
                case 1:
215
                    imagecopy($dest, $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
216
                    break;
217
                case 2:
218
                    imagecopy($dest, $src, $posCol2, 0, 0, 0, $imgWidth, $imgHeight); //top center
219
                    break;
220
                case 3:
221
                    imagecopy($dest, $src, $posCol3, 0, 0, 0, $imgWidth, $imgHeight); //top right
222
                    break;
223
                case 4:
224
                    imagecopy($dest, $src, 0, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom left
225
                    break;
226
                case 5:
227
                    imagecopy($dest, $src, $posCol2, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom center
228
                    break;
229
                case 6:
230
                    imagecopy($dest, $src, $posCol3, $posRow2, 0, 0, $imgWidth, $imgHeight); //bottom right
231
                    break;
232
            }
233
        }
234
        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

234
        imagejpeg(/** @scrutinizer ignore-type */ $dest, $this->endFile);
Loading history...
235
236
        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

236
        imagedestroy(/** @scrutinizer ignore-type */ $src);
Loading history...
237
        imagedestroy($dest);
238
    }
239
}
240