Resizer::mergeImage()   C
last analyzed

Complexity

Conditions 13
Paths 48

Size

Total Lines 56
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 48
nc 48
nop 0
dl 0
loc 56
rs 6.6166
c 1
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\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
 * @version        $Id: 1.0 imagehandler.php 1 Mon 2018-03-19 10:04:49Z XOOPS Project (www.xoops.org) $
25
 */
26
class Resizer
27
{
28
    public $sourceFile    = '';
29
    public $endFile       = '';
30
    public $maxWidth      = 0;
31
    public $maxHeight     = 0;
32
    public $imageMimetype = '';
33
    public $jpgQuality    = 90;
34
    public $mergeType     = 0;
35
    public $mergePos      = 0;
36
37
    /**
38
     * resize image if size exceed given width/height
39
     * @return string|boolean
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
                break;
51
            case'image/gif':
52
                $img = imagecreatefromgif($this->sourceFile);
53
                break;
54
            default:
55
                return 'Unsupported format';
56
        }
57
58
        $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

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

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

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

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

85
            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

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

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

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

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

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

194
                    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

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

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

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