Resizer   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 149
dl 0
loc 215
rs 9.52
c 1
b 0
f 0
wmc 36

3 Methods

Rating   Name   Duplication   Size   Complexity  
C resizeImage() 0 70 12
C mergeImage() 0 56 13
B resizeAndCrop() 0 64 11
1
<?php
2
3
namespace XoopsModules\Tdmcreate\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
        $new_width  = 0;
60
        $new_height = 0;
61
62
        if ($width > $this->maxWidth || $height > $this->maxHeight) {
63
            // recalc image size based on this->maxWidth/this->maxHeight
64
            if ($width > $height) {
65
                if ($width < $this->maxWidth) {
66
                    $new_width = $width;
67
                } else {
68
                    $new_width  = $this->maxWidth;
69
                    $divisor    = $width / $new_width;
70
                    $new_height = floor($height / $divisor);
71
                }
72
            } elseif ($height < $this->maxHeight) {
73
                $new_height = $height;
74
            } else {
75
                $new_height = $this->maxHeight;
76
                $divisor    = $height / $new_height;
77
                $new_width  = floor($width / $divisor);
78
            }
79
80
            // Create a new temporary image.
81
            $tmpimg = imagecreatetruecolor($new_width, $new_height);
0 ignored issues
show
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

81
            $tmpimg = imagecreatetruecolor($new_width, /** @scrutinizer ignore-type */ $new_height);
Loading history...
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

81
            $tmpimg = imagecreatetruecolor(/** @scrutinizer ignore-type */ $new_width, $new_height);
Loading history...
82
            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

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

83
            imagesavealpha(/** @scrutinizer ignore-type */ $tmpimg, true);
Loading history...
84
85
            // Copy and resize old image into new image.
86
            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

86
            imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, /** @scrutinizer ignore-type */ $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

86
            imagecopyresampled($tmpimg, /** @scrutinizer ignore-type */ $img, 0, 0, 0, 0, $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

86
            imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, /** @scrutinizer ignore-type */ $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

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

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

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

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

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

195
                    imagecopy($dest, /** @scrutinizer ignore-type */ $src, 0, 0, 0, 0, $imgWidth, $imgHeight); //top left
Loading history...
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

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

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

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