Passed
Pull Request — master (#58)
by Michael
03:14
created

ImageResizer::mergeImage()   C

Complexity

Conditions 13
Paths 48

Size

Total Lines 48
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 39
c 0
b 0
f 0
nc 48
nop 4
dl 0
loc 48
rs 6.6166

How to fix   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\Tdmdownloads\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
 *
17
 * @copyright      module for xoops
18
 * @license        GPL 2.0 or later
19
 * @package        wggallery
20
 * @since          1.0
21
 * @min_xoops      2.5.9
22
 * @author         Wedega - Email:<[email protected]> - Website:<https://wedega.com>
23
 */
24
25
trait ImageResizer
26
{
27
28
    /**
29
     * resize image if size exceed given width/height
30
     * @param        $sourcefile
31
     * @param string $endfile
32
     * @param int    $max_width
33
     * @param int    $max_height
34
     * @param        $imageMimetype
35
     * @return string|boolean
36
     */
37
38
    public function resizeImage($sourcefile, $endfile, $max_width, $max_height, $imageMimetype)
39
    {
40
        // check file extension
41
        switch ($imageMimetype) {
42
            case'image/png':
43
                $img = imagecreatefrompng($sourcefile);
44
                break;
45
            case'image/jpeg':
46
                $img = imagecreatefromjpeg($sourcefile);
47
                break;
48
            case'image/gif':
49
                $img = imagecreatefromgif($sourcefile);
50
                break;
51
            default:
52
                return 'Unsupported format';
53
        }
54
55
        $width  = imagesx($img);
56
        $height = imagesy($img);
57
58
        if ($width > $max_width || $height > $max_height) {
59
            // recalc image size based on max_width/max_height
60
            if ($width > $height) {
61
                if ($width < $max_width) {
62
                    $new_width = $width;
63
                } else {
64
                    $new_width  = $max_width;
65
                    $divisor    = $width / $new_width;
66
                    $new_height = floor($height / $divisor);
67
                }
68
            } else if ($height < $max_height) {
69
                $new_height = $height;
70
            } else {
71
                $new_height = $max_height;
72
                $divisor    = $height / $new_height;
73
                $new_width  = floor($width / $divisor);
74
            }
75
76
            // Create a new temporary image.
77
            $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...
78
            imagealphablending($tmpimg, false);
79
            imagesavealpha($tmpimg, true);
80
81
            // Copy and resize old image into new image.
82
            imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
83
84
            unlink($endfile);
85
            //compressing the file
86
            switch ($imageMimetype) {
87
                case'image/png':
88
                    imagepng($tmpimg, $endfile, 0);
89
                    break;
90
                case'image/jpeg':
91
                    imagejpeg($tmpimg, $endfile, 100);
92
                    break;
93
                case'image/gif':
94
                    imagegif($tmpimg, $endfile);
95
                    break;
96
            }
97
98
            // release the memory
99
            imagedestroy($tmpimg);
100
        } else {
101
            return 'copy';
102
        }
103
        imagedestroy($img);
104
        return true;
105
    }
106
107
    /**
108
     * @param     $src_url
109
     * @param     $src_mimetype
110
     * @param     $dest_url
111
     * @param     $dest_w
112
     * @param     $dest_h
113
     * @param int $quality
114
     * @return bool
115
     */
116
    public function resizeAndCrop($src_url, $src_mimetype, $dest_url, $dest_w, $dest_h, $quality = 90)
117
    {
118
        // check file extension
119
        switch ($src_mimetype) {
120
            case 'image/png':
121
                $original = imagecreatefrompng($src_url);
122
                break;
123
            case 'image/jpeg':
124
                $original = imagecreatefromjpeg($src_url);
125
                break;
126
            case 'image/gif':
127
                $original = imagecreatefromgif($src_url);
128
                break;
129
            default:
130
                return 'Unsupported format';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'Unsupported format' returns the type string which is incompatible with the documented return type boolean.
Loading history...
131
        }
132
133
        if (!$original) {
0 ignored issues
show
introduced by
$original is of type resource, thus it always evaluated to false.
Loading history...
134
            return false;
135
        }
136
137
        // GET ORIGINAL IMAGE DIMENSIONS
138
        list($original_w, $original_h) = getimagesize($src_url);
139
140
        // RESIZE IMAGE AND PRESERVE PROPORTIONS
141
        $dest_w_resize = $dest_w;
142
        $dest_h_resize = $dest_h;
143
        if ($original_w > $original_h) {
144
            $dest_h_ratio  = $dest_h / $original_h;
145
            $dest_w_resize = (int)round($original_w * $dest_h_ratio);
146
        } else {
147
            $dest_w_ratio  = $dest_w / $original_w;
148
            $dest_h_resize = (int)round($original_h * $dest_w_ratio);
149
        }
150
        if ($dest_w_resize < $dest_w) {
151
            $dest_h_ratio  = $dest_w / $dest_w_resize;
152
            $dest_h_resize = (int)round($dest_h * $dest_h_ratio);
153
            $dest_w_resize = $dest_w;
154
        }
155
156
        // CREATE THE PROPORTIONAL IMAGE RESOURCE
157
        $thumb = imagecreatetruecolor($dest_w_resize, $dest_h_resize);
158
        if (!imagecopyresampled($thumb, $original, 0, 0, 0, 0, $dest_w_resize, $dest_h_resize, $original_w, $original_h)) {
159
            return false;
160
        }
161
162
        // CREATE THE CENTERED CROPPED IMAGE TO THE SPECIFIED DIMENSIONS
163
        $final = imagecreatetruecolor($dest_w, $dest_h);
164
165
        $dest_w_offset = 0;
166
        $dest_h_offset = 0;
167
        if ($dest_w < $dest_w_resize) {
168
            $dest_w_offset = (int)round(($dest_w_resize - $dest_w) / 2);
169
        } else {
170
            $dest_h_offset = (int)round(($dest_h_resize - $dest_h) / 2);
171
        }
172
173
        if (!imagecopy($final, $thumb, 0, 0, $dest_w_offset, $dest_h_offset, $dest_w_resize, $dest_h_resize)) {
174
            return false;
175
        }
176
177
        // STORE THE FINAL IMAGE - WILL OVERWRITE $dest_url
178
        if (!imagejpeg($final, $dest_url, $quality)) {
179
            return false;
180
        }
181
        return true;
182
    }
183
184
    /**
185
     * @param $src_url
186
     * @param $dest_url
187
     * @param $pos
188
     * @param $of
189
     */
190
    public function mergeImage($src_url, $dest_url, $pos, $of)
191
    {
192
        $dest = imagecreatefromjpeg($dest_url);
193
        $src  = imagecreatefromjpeg($src_url);
194
        // ImageCopy ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h )
195
//        $src = imagecreatefromjpeg($src_url);
196
        if (4 == $of) {
197
            switch ($pos) {
198
                case 1:
199
                    imagecopy($dest, $src, 0, 0, 0, 0, 199, 149); //top left
200
                    break;
201
                case 2:
202
                    imagecopy($dest, $src, 201, 0, 0, 0, 199, 149); //top right
203
                    break;
204
                case 3:
205
                    imagecopy($dest, $src, 0, 151, 0, 0, 199, 149); //bottom left
206
                    break;
207
                case 4:
208
                    imagecopy($dest, $src, 201, 151, 0, 0, 199, 149); //bottom right
209
                    break;
210
            }
211
        }
212
        if (6 == $of) {
213
            switch ($pos) {
214
                case 1:
215
                    imagecopy($dest, $src, 0, 0, 0, 0, 133, 149); //top left
216
                    break;
217
                case 2:
218
                    imagecopy($dest, $src, 134, 0, 0, 0, 133, 149); //top center
219
                    break;
220
                case 3:
221
                    imagecopy($dest, $src, 268, 0, 0, 0, 133, 149); //top right
222
                    break;
223
                case 4:
224
                    imagecopy($dest, $src, 0, 151, 0, 0, 133, 149); //bottom left
225
                    break;
226
                case 5:
227
                    imagecopy($dest, $src, 134, 151, 0, 0, 133, 149); //bottom center
228
                    break;
229
                case 6:
230
                    imagecopy($dest, $src, 268, 151, 0, 0, 133, 149); //bottom right
231
                    break;
232
            }
233
        }
234
        imagejpeg($dest, $dest_url);
235
236
        imagedestroy($src);
237
        imagedestroy($dest);
238
    }
239
240
    /**
241
     * resize image if size exceed given width/height
242
     * @param string $endfile
243
     * @param int    $max_width
244
     * @param int    $max_height
245
     * @return string|boolean
246
     */
247
    /*     private function resizeImageSave($endfile, $max_width, $max_height){
248
            // check file extension
249
            switch ($this->imageMimetype) {
250
                case'image/png':
251
                    $img = imagecreatefrompng($this->imagePath);
252
253
                    break;
254
                case'image/jpeg':
255
                    $img = imagecreatefromjpeg($this->imagePath);
256
                    break;
257
                case'image/gif':
258
                    $img = imagecreatefromgif($this->imagePath);
259
                    break;
260
                default:
261
                    return 'Unsupported format';
262
            }
263
264
            $width  = imagesx($img);
265
            $height = imagesy($img);
266
267
            if ($width > $max_width || $height > $max_height) {
268
                // recalc image size based on max_width/max_height
269
                if ($width > $height) {
270
                    if ($width < $max_width) {
271
                        $new_width = $width;
272
                    } else {
273
                        $new_width  = $max_width;
274
                        $divisor    = $width / $new_width;
275
                        $new_height = floor($height / $divisor);
276
                    }
277
                } else if($height < $max_height){
278
                        $new_height = $height;
279
                    } else {
280
                        $new_height = $max_height;
281
                        $divisor    = $height / $new_height;
282
                        $new_width  = floor($width / $divisor);
283
                    }
284
285
                // Create a new temporary image.
286
                $tmpimg = imagecreatetruecolor($new_width, $new_height);
287
                imagealphablending($tmpimg, false);
288
                imagesavealpha($tmpimg, true);
289
290
                // Copy and resize old image into new image.
291
                imagecopyresampled($tmpimg, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
292
293
                //compressing the file
294
                switch ($this->imageMimetype) {
295
                    case'image/png':
296
                        imagepng($tmpimg, $endfile, 0);
297
                        break;
298
                    case'image/jpeg':
299
                        imagejpeg($tmpimg, $endfile, 100);
300
                        break;
301
                    case'image/gif':
302
                        imagegif($tmpimg, $endfile);
303
                        break;
304
                }
305
306
                // release the memory
307
                imagedestroy($tmpimg);
308
            } else {
309
                return 'copy';
310
            }
311
            imagedestroy($img);
312
            return true;
313
        } */
314
}
315