ImageResize   B
last analyzed

Complexity

Total Complexity 48

Size/Duplication

Total Lines 318
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 48
eloc 127
dl 0
loc 318
rs 8.5599
c 2
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeight() 0 3 1
A getFile() 0 3 1
A setMime() 0 3 1
A getBits() 0 3 1
A setWidth() 0 3 1
A setFile() 0 3 1
A save() 0 16 6
A getWidth() 0 3 1
A setImage() 0 3 1
A merge() 0 3 1
A setHeight() 0 3 1
A setBits() 0 3 1
A imageRotate() 0 9 1
B upload() 0 26 7
A getImage() 0 3 1
B resizeImage() 0 49 9
A getMime() 0 3 1
A html2rgb() 0 19 4
A crop() 0 10 1
A watermark() 0 26 5
A text() 0 5 1
A filter() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like ImageResize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ImageResize, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * KNUT7 K7F (https://marciozebedeu.com/)
4
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @link      https://github.com/knut7/framework/ for the canonical source repository
11
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
12
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
13
 * @author    Marcio Zebedeu - [email protected]
14
 * @version   1.0.2
15
 */
16
17
namespace Ballybran\Helpers\Images;
18
19
20
use Ballybran\Helpers\Images\ImageInterface\ResizeInterface;
21
22
class ImageResize implements ResizeInterface
23
{
24
25
26
    private $file;
27
    private $image;
28
    private $width;
29
30
    /**
31
     * @return mixed
32
     */
33
    public function getFile()
34
    {
35
        return $this->file;
36
    }
37
38
    /**
39
     * @return resource
40
     */
41
    public function getImage()
42
    {
43
        return $this->image;
44
    }
45
46
    /**
47
     * @return mixed
48
     */
49
    public function getWidth()
50
    {
51
        return $this->width;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getHeight()
58
    {
59
        return $this->height;
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    public function getBits()
66
    {
67
        return $this->bits;
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73
    public function getMime()
74
    {
75
        return $this->mime;
76
    }
77
78
    /**
79
     * @param mixed $file
80
     */
81
    public function setFile($file)
82
    {
83
        $this->file = $file;
84
    }
85
86
    /**
87
     * @param resource $image
88
     */
89
    public function setImage($image)
90
    {
91
        $this->image = $image;
92
    }
93
94
    /**
95
     * @param mixed $width
96
     */
97
    public function setWidth($width)
98
    {
99
        $this->width = $width;
100
    }
101
102
    /**
103
     * @param mixed $height
104
     */
105
    public function setHeight($height)
106
    {
107
        $this->height = $height;
108
    }
109
110
    /**
111
     * @param string $bits
112
     */
113
    public function setBits(string $bits)
114
    {
115
        $this->bits = $bits;
116
    }
117
118
    /**
119
     * @param string $mime
120
     */
121
    public function setMime(string $mime)
122
    {
123
        $this->mime = $mime;
124
    }
125
126
    private $height;
127
    private $bits;
128
    private $mime;
129
130
    public function upload($file)
131
    {
132
        if (file_exists($file)) {
133
            $this->file = $file;
134
135
            $info = getimagesize($file);
136
137
            $this->setWidth($info[0]);
138
            $this->setHeight($info[1]);
139
            $this->setBits(isset($info['bits']) ? $info['bits'] : '');
140
            $this->setMime(isset($info['mime']) ? $info['mime'] : '');
141
142
            if ($this->getMime() == 'image/gif') {
143
                $this->image = imagecreatefromgif($file);
144
                $this->setImage($this->image);
0 ignored issues
show
Bug introduced by
It seems like $this->image can also be of type GdImage; however, parameter $image of Ballybran\Helpers\Images\ImageResize::setImage() 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

144
                $this->setImage(/** @scrutinizer ignore-type */ $this->image);
Loading history...
145
            } elseif ($this->mime == 'image/png') {
146
                $this->image = imagecreatefrompng($file);
147
                $this->setImage($this->image);
148
149
            } elseif ($this->mime == 'image/jpeg') {
150
                $this->image = imagecreatefromjpeg($file);
151
                $this->setImage($this->image);
152
153
            }
154
        } else {
155
            exit('Error: Could not load image ' . $file . '!');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
156
        }
157
    }
158
159
160
    /**
161
     *
162
     * @param type $file Inser you file
163
     * @param type $quality Isert optional quality for image
0 ignored issues
show
Bug introduced by
The type Ballybran\Helpers\Images\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
164
     * Exemple save('exemple.jpg, 100);
165
     */
166
    public function save(string $savePath, int $imageQuality = 100)
167
    {
168
        $info = pathinfo($savePath);
169
170
        $extension = strtolower($info['extension']);
171
172
        if (is_resource($this->image)) {
173
            if ($extension == 'jpeg' || $extension == 'jpg') {
174
                imagejpeg($this->image, $savePath, $imageQuality);
175
            } elseif ($extension == 'png') {
176
                imagepng($this->image, $savePath);
177
            } elseif ($extension == 'gif') {
178
                imagegif($this->image, $savePath);
179
            }
180
181
            imagedestroy($this->image);
182
        }
183
    }
184
185
    /**
186
     *
187
     * @param type $width Inser Width for you image
188
     * @param type $height Inser height for you image
189
     * @param type $default nser you scale (where w = width end h = height)
190
     * @return type
191
     * Exemplae: resize(800, 600, 'w');
192
     */
193
    public function resizeImage($width = 0, $height = 0, $option = 'auto')
194
    {
195
        if (!$this->width || !$this->height) {
196
            return;
197
        }
198
199
        $xpos = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $xpos is dead and can be removed.
Loading history...
200
        $ypos = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $ypos is dead and can be removed.
Loading history...
201
        $scale = 1;
202
203
        $scale_w = $width / $this->width;
204
        $scale_h = $height / $this->height;
205
206
        if ($option == 'w') {
207
            $scale = $scale_w;
208
        } elseif ($option == 'h') {
209
            $scale = $scale_h;
210
        } else {
211
            $scale = min($scale_w, $scale_h);
212
        }
213
214
        if ($scale == 1 && $scale_h == $scale_w && $this->mime != 'image/png') {
215
            return;
216
        }
217
218
        $new_width = (int)($this->width * $scale);
219
        $new_height = (int)($this->height * $scale);
220
        $xpos = (int)(($width - $new_width) / 2);
221
        $ypos = (int)(($height - $new_height) / 2);
222
223
        $image_old = $this->image;
224
        $this->image = imagecreatetruecolor($width, $height);
0 ignored issues
show
Bug introduced by
It seems like $height can also be of type Ballybran\Helpers\Images\type; 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

224
        $this->image = imagecreatetruecolor($width, /** @scrutinizer ignore-type */ $height);
Loading history...
Bug introduced by
It seems like $width can also be of type Ballybran\Helpers\Images\type; 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

224
        $this->image = imagecreatetruecolor(/** @scrutinizer ignore-type */ $width, $height);
Loading history...
225
226
        if ($this->mime == 'image/png') {
227
            imagealphablending($this->image, false);
228
            imagesavealpha($this->image, true);
229
            $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127);
230
            imagecolortransparent($this->image, $background);
231
        } else {
232
            $background = imagecolorallocate($this->image, 255, 255, 255);
233
        }
234
235
        imagefilledrectangle($this->image, 0, 0, $width, $height, $background);
0 ignored issues
show
Bug introduced by
It seems like $height can also be of type Ballybran\Helpers\Images\type; however, parameter $y2 of imagefilledrectangle() 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

235
        imagefilledrectangle($this->image, 0, 0, $width, /** @scrutinizer ignore-type */ $height, $background);
Loading history...
Bug introduced by
It seems like $width can also be of type Ballybran\Helpers\Images\type; however, parameter $x2 of imagefilledrectangle() 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

235
        imagefilledrectangle($this->image, 0, 0, /** @scrutinizer ignore-type */ $width, $height, $background);
Loading history...
236
237
        imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->width, $this->height);
238
        imagedestroy($image_old);
239
240
        $this->width = $width;
241
        $this->height = $height;
242
    }
243
244
245
    public function crop($top_x, $top_y, $bottom_x, $bottom_y)
246
    {
247
        $image_old = $this->image;
248
        $this->image = imagecreatetruecolor($bottom_x - $top_x, $bottom_y - $top_y);
249
250
        imagecopy($this->image, $image_old, 0, 0, $top_x, $top_y, $this->width, $this->height);
251
        imagedestroy($image_old);
252
253
        $this->width = $bottom_x - $top_x;
254
        $this->height = $bottom_y - $top_y;
255
    }
256
257
    public function imageRotate(int $degree, $color = '000000')
258
    {
259
260
        $rgb = $this->html2rgb($color);
261
262
        $this->image = imagerotate($this->image, $degree, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
263
264
        $this->width = imagesx($this->image);
265
        $this->height = imagesy($this->image);
266
    }
267
268
269
    /**
270
     *
271
     * @param type $watermark
272
     * @param type $position
273
     */
274
    public function watermark($watermark, $position = 'bottomright')
0 ignored issues
show
Unused Code introduced by
The parameter $watermark is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

274
    public function watermark(/** @scrutinizer ignore-unused */ $watermark, $position = 'bottomright')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
275
    {
276
        $watermark = imagecreatefromjpeg('DDDDD');
277
278
        switch ($position) {
279
            case 'topleft':
280
                $watermark_pos_x = 0;
281
                $watermark_pos_y = 0;
282
                break;
283
            case 'topright':
284
                $watermark_pos_x = $this->width - 10;
285
                $watermark_pos_y = 0;
286
                break;
287
            case 'bottomleft':
288
                $watermark_pos_x = 0;
289
                $watermark_pos_y = $this->height - 10;
290
                break;
291
            case 'bottomright':
292
                $watermark_pos_x = $this->width - 5;
293
                $watermark_pos_y = $this->height - 5;
294
                break;
295
        }
296
297
        imagecopy($this->image, $watermark, $watermark_pos_x, $watermark_pos_y, 0, 0, $this->getWidth(), $this->getHeight());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $watermark_pos_y does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $watermark_pos_x does not seem to be defined for all execution paths leading up to this point.
Loading history...
298
299
        imagedestroy($watermark);
300
    }
301
302
    private function filter()
0 ignored issues
show
Unused Code introduced by
The method filter() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
303
    {
304
        $args = func_get_args();
305
306
        call_user_func_array('imagefilter', $args);
307
    }
308
309
    private function text($text, $x = 0, $y = 0, $size = 5, $color = '000000')
310
    {
311
        $rgb = $this->html2rgb($color);
312
313
        imagestring($this->image, $size, $x, $y, $text, imagecolorallocate($this->image, $rgb[0], $rgb[1], $rgb[2]));
314
    }
315
316
    private function merge($merge, $x = 0, $y = 0, $opacity = 100)
0 ignored issues
show
Unused Code introduced by
The method merge() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
Unused Code introduced by
The parameter $merge is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

316
    private function merge(/** @scrutinizer ignore-unused */ $merge, $x = 0, $y = 0, $opacity = 100)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
317
    {
318
        imagecopymerge($this->image, $this->getImage(), $x, $y, 0, 0, $this->getWidth(), $this->getHeight(), $opacity);
319
    }
320
321
    private function html2rgb($color)
322
    {
323
        if ($color[0] == '#') {
324
            $color = substr($color, 1);
325
        }
326
327
        if (strlen($color) == 6) {
328
            list($r, $g, $b) = array($color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5]);
329
        } elseif (strlen($color) == 3) {
330
            list($r, $g, $b) = array($color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2]);
331
        } else {
332
            return false;
333
        }
334
335
        $r = hexdec($r);
336
        $g = hexdec($g);
337
        $b = hexdec($b);
338
339
        return array($r, $g, $b);
340
    }
341
342
}
343