Passed
Branch master (f2d2e3)
by Michael
18:45
created

Image_Transform_Driver_GD   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 546
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 261
c 1
b 0
f 0
dl 0
loc 546
rs 2.64
wmc 72

14 Methods

Rating   Name   Duplication   Size   Complexity  
A free() 0 4 2
A Image_Transform_GD() 0 3 1
A gamma() 0 2 1
A flip() 0 25 4
F rotate() 0 167 32
A display() 0 14 3
A _resize() 0 23 4
A crop() 0 19 3
A save() 0 14 4
A load() 0 14 3
A addText() 0 32 5
A preserveTransparencyForPalette() 0 35 5
A paletteToTrueColorWithTransparency() 0 7 1
A newImgPreserveAlpha() 0 40 4

How to fix   Complexity   

Complex Class

Complex classes like Image_Transform_Driver_GD 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 Image_Transform_Driver_GD, and based on these observations, apply Extract Interface, too.

1
<?php
2
/***********************************************************************
3
** Title.........:  GD Driver
4
** Version.......:  1.0
5
** Author........:  Xiang Wei ZHUO <[email protected]>
6
** Filename......:  GD.php
7
** Last changed..:  30 Aug 2003 
8
** Notes.........:  Orginal is from PEAR
9
**/
10
// +----------------------------------------------------------------------+
11
// | PHP Version 4                                                        |
12
// +----------------------------------------------------------------------+
13
// | Copyright (c) 1997-2002 The PHP Group                                |
14
// +----------------------------------------------------------------------+
15
// | This source file is subject to version 2.02 of the PHP license,      |
16
// | that is bundled with this package in the file LICENSE, and is        |
17
// | available at through the world-wide-web at                           |
18
// | http://www.php.net/license/2_02.txt.                                 |
19
// | If you did not receive a copy of the PHP license and are unable to   |
20
// | obtain it through the world-wide-web, please send a note to          |
21
// | [email protected] so we can mail you a copy immediately.               |
22
// +----------------------------------------------------------------------+
23
// | Authors: Peter Bowyer <[email protected]>                      |
24
// |          Alan Knowles <[email protected]>                            |
25
// +----------------------------------------------------------------------+
26
//
27
//    Usage :
28
//    $img    = new Image_Transform_GD();
29
//    $angle  = -78;
30
//    $img->load('magick.png');
31
//
32
//    if($img->rotate($angle,array('autoresize'=>true,'color_mask'=>array(255,0,0)))){
33
//        $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'/usr/share/fonts/default/TrueType/cogb____.ttf'));
34
//        $img->display();
35
//    } else {
36
//        echo "Error";
37
//    }
38
//
39
//
40
// $Id:GD.php 938 2008-01-22 20:13:47Z ray $
41
//
42
// Image Transformation interface using the GD library
43
//
44
45
require_once "../ImageManager/Classes/Transform.php";
46
47
Class Image_Transform_Driver_GD extends Image_Transform
48
{
49
    /**
50
     * Holds the image file for manipulation
51
     */
52
    var $imageHandle = '';
53
54
    /**
55
     * Holds the original image file
56
     */
57
    var $old_image = '';
58
59
    /**
60
     * Check settings
61
     *
62
     * @return mixed true or  or a PEAR error object on error
63
     *
64
     * @see PEAR::isError()
65
     */
66
    function Image_Transform_GD()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
    {
68
        return;
69
    } // End function Image
70
71
    /**
72
     * Load image
73
     *
74
     * @param string filename
0 ignored issues
show
Bug introduced by
The type filename 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...
75
     *
76
     * @return mixed none or a PEAR error object on error
77
     * @see PEAR::isError()
78
     */
79
    function load($image)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
    {
81
        $this->uid = md5($_SERVER['REMOTE_ADDR']);
82
        $this->image = $image;
83
        $this->_get_image_details($image);
84
        $functionName = 'ImageCreateFrom' . $this->type;
85
		
86
		if(function_exists($functionName))
87
		{
88
			$this->imageHandle = $functionName($this->image);
89
			if ( $this->type == 'png')
90
			{
91
				imageAlphaBlending($this->imageHandle, false);
92
				imageSaveAlpha($this->imageHandle, true);
93
			}
94
		}
95
    } // End load
96
97
    /**
98
     * addText
99
     *
100
     * @param   array   options     Array contains options
0 ignored issues
show
Bug introduced by
The type options 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...
101
     *                              array(
102
     *                                  'text'  The string to draw
103
     *                                  'x'     Horizontal position
104
     *                                  'y'     Vertical Position
105
     *                                  'Color' Font color
106
     *                                  'font'  Font to be used
107
     *                                  'size'  Size of the fonts in pixel
108
     *                                  'resize_first'  Tell if the image has to be resized
109
     *                                                  before drawing the text
110
     *                              )
111
     *
112
     * @return none
0 ignored issues
show
Bug introduced by
The type none 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...
113
     * @see PEAR::isError()
114
     */
115
    function addText($params)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
    {
117
        $default_params = array(
118
                                'text' => 'This is Text',
119
                                'x' => 10,
120
                                'y' => 20,
121
                                'color' => array(255,0,0),
122
                                'font' => 'Arial.ttf',
123
                                'size' => '12',
124
                                'angle' => 0,
125
                                'resize_first' => false // Carry out the scaling of the image before annotation?  Not used for GD
126
                                );
127
        $params = array_merge($default_params, $params);
128
        extract($params);
129
130
        if( !is_array($color) ){
131
            if ($color[0]=='#'){
132
                $this->colorhex2colorarray( $color );
133
            } else {
134
                include_once('Image/Transform/Driver/ColorsDefs.php');
135
                $color = isset($colornames[$color])?$colornames[$color]:false;
136
            }
137
        }
138
139
        $c = imagecolorresolve ($this->imageHandle, $color[0], $color[1], $color[2]);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolorresolve(). ( Ignorable by Annotation )

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

139
        $c = imagecolorresolve (/** @scrutinizer ignore-type */ $this->imageHandle, $color[0], $color[1], $color[2]);
Loading history...
140
141
        if ('ttf' == substr($font, -3)) {
142
            ImageTTFText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagettftext(). ( Ignorable by Annotation )

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

142
            ImageTTFText(/** @scrutinizer ignore-type */ $this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
Loading history...
143
        } else {
144
            ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagepstext(). ( Ignorable by Annotation )

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

144
            ImagePSText(/** @scrutinizer ignore-type */ $this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
Loading history...
Deprecated Code introduced by
The function imagepstext() has been deprecated: 7.0.0 This function was REMOVED in PHP 7.0.0. ( Ignorable by Annotation )

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

144
            /** @scrutinizer ignore-deprecated */ ImagePSText($this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
145
        }
146
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The expression return true returns the type true which is incompatible with the documented return type none.
Loading history...
147
    } // End addText
148
149
150
    /**
151
     * Rotate image by the given angle
152
     * Uses a fast rotation algorythm for custom angles
153
     * or lines copy for multiple of 90 degrees
154
     *
155
     * @param int       $angle      Rotation angle
156
     * @param array     $options    array(  'autoresize'=>true|false,
157
     *                                      'color_mask'=>array(r,g,b), named color or #rrggbb
158
     *                                   )
159
     * @author Pierre-Alain Joye
160
     * @return mixed none or a PEAR error object on error
161
     * @see PEAR::isError()
162
     */
163
    function rotate($angle, $options=null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
164
    {
165
        if(function_exists('imagerotate')) {
166
            $white = imagecolorallocatealpha ($this->imageHandle, 255, 255, 255, 127);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolorallocatealpha(). ( Ignorable by Annotation )

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

166
            $white = imagecolorallocatealpha (/** @scrutinizer ignore-type */ $this->imageHandle, 255, 255, 255, 127);
Loading history...
167
			$this->imageHandle = imagerotate($this->imageHandle, $angle, $white);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagerotate(). ( Ignorable by Annotation )

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

167
			$this->imageHandle = imagerotate(/** @scrutinizer ignore-type */ $this->imageHandle, $angle, $white);
Loading history...
168
            return true;
169
        }
170
171
        if ( $options==null ){
172
            $autoresize = true;
173
            $color_mask = array(105,255,255);
174
        } else {
175
            extract( $options );
176
        }
177
178
        while ($angle <= -45) {
179
            $angle  += 360;
180
        }
181
        while ($angle > 270) {
182
            $angle  -= 360;
183
        }
184
185
        $t      = deg2rad($angle);
186
187
        if( !is_array($color_mask) ){
188
            if ($color[0]=='#'){
189
                $this->colorhex2colorarray( $color_mask );
190
            } else {
191
                include_once('Image/Transform/Driver/ColorDefs.php');
192
                $color = isset($colornames[$color_mask])?$colornames[$color_mask]:false;
0 ignored issues
show
Unused Code introduced by
The assignment to $color is dead and can be removed.
Loading history...
193
            }
194
        }
195
196
        // Do not round it, too much lost of quality
197
        $cosT   = cos($t);
198
        $sinT   = sin($t);
199
200
        $img    =& $this->imageHandle;
201
202
        $width  = $max_x  = $this->img_x;
203
        $height = $max_y  = $this->img_y;
204
        $min_y  = 0;
205
        $min_x  = 0;
206
207
        $x1     = round($max_x/2,0);
208
        $y1     = round($max_y/2,0);
209
210
        if ( $autoresize ){
211
            $t      = abs($t);
212
            $a      = round($angle,0);
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
213
            switch((int)($angle)){
214
                case 0:
215
                        $width2     = $width;
216
                        $height2    = $height;
217
                    break;
218
                case 90:
219
                        $width2     = $height;
220
                        $height2    = $width;
221
                    break;
222
                case 180:
223
                        $width2     = $width;
224
                        $height2    = $height;
225
                    break;
226
                case 270:
227
                        $width2     = $height;
228
                        $height2    = $width;
229
                    break;
230
                default:
231
                    $width2     = (int)(abs(sin($t) * $height + cos($t) * $width));
232
                    $height2    = (int)(abs(cos($t) * $height+sin($t) * $width));
233
            }
234
235
            $width2     -= $width2%2;
236
            $height2    -= $height2%2;
237
238
            $d_width    = abs($width - $width2);
239
            $d_height   = abs($height - $height2);
240
            $x_offset   = $d_width/2;
241
            $y_offset   = $d_height/2;
242
            $min_x2     = -abs($x_offset);
243
            $min_y2     = -abs($y_offset);
244
            $max_x2     = $width2;
245
            $max_y2     = $height2;
246
        }
247
248
        $img2   = @$this->newImgPreserveAlpha( imagecreateTrueColor($width2,$height2) );
249
250
        if ( !is_resource($img2) ){
251
            return false;/*PEAR::raiseError('Cannot create buffer for the rotataion.',
252
                                null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/
253
        }
254
255
        $this->img_x = $width2;
256
        $this->img_y = $height2;
257
258
259
        imagepalettecopy($img2,$img);
0 ignored issues
show
Bug introduced by
$img of type string is incompatible with the type resource expected by parameter $source of imagepalettecopy(). ( Ignorable by Annotation )

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

259
        imagepalettecopy($img2,/** @scrutinizer ignore-type */ $img);
Loading history...
260
261
       $mask = imagecolorallocatealpha ($img2,$color_mask[0],$color_mask[1],$color_mask[2],127);
262
        // use simple lines copy for axes angles
263
        switch((int)($angle)){
264
            case 0:
265
                imagefill ($img2, 0, 0,$mask);
266
                for ($y=0; $y < $max_y; $y++) {
267
                    for ($x = $min_x; $x < $max_x; $x++){
268
                        $c  = @imagecolorat ( $img, $x, $y);
0 ignored issues
show
Bug introduced by
$img of type string is incompatible with the type resource expected by parameter $image of imagecolorat(). ( Ignorable by Annotation )

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

268
                        $c  = @imagecolorat ( /** @scrutinizer ignore-type */ $img, $x, $y);
Loading history...
269
                        imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c);
270
                    }
271
                }
272
                break;
273
            case 90:
274
                imagefill ($img2, 0, 0,$mask);
275
                for ($x = $min_x; $x < $max_x; $x++){
276
                    for ($y=$min_y; $y < $max_y; $y++) {
277
                        $c  = imagecolorat ( $img, $x, $y);
278
                        imagesetpixel($img2,$max_y-$y-1,$x,$c);
279
                    }
280
                }
281
                break;
282
            case 180:
283
                imagefill ($img2, 0, 0,$mask);
284
                for ($y=0; $y < $max_y; $y++) {
285
                    for ($x = $min_x; $x < $max_x; $x++){
286
                        $c  = @imagecolorat ( $img, $x, $y);
287
                        imagesetpixel($img2, $max_x2-$x-1, $max_y2-$y-1, $c);
288
                    }
289
                }
290
                break;
291
            case 270:
292
                imagefill ($img2, 0, 0,$mask);
293
                for ($y=0; $y < $max_y; $y++) {
294
                    for ($x = $max_x; $x >= $min_x; $x--){
295
                        $c  = @imagecolorat ( $img, $x, $y);
296
                        imagesetpixel($img2,$y,$max_x-$x-1,$c);
297
                    }
298
                }
299
                break;
300
            // simple reverse rotation algo
301
            default:
302
                $i=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
303
                for ($y = $min_y2; $y < $max_y2; $y++){
304
305
                    // Algebra :)
306
                    $x2 = round((($min_x2-$x1) * $cosT) + (($y-$y1) * $sinT + $x1),0);
307
                    $y2 = round((($y-$y1) * $cosT - ($min_x2-$x1) * $sinT + $y1),0);
308
309
                    for ($x = $min_x2; $x < $max_x2; $x++){
310
311
                        // Check if we are out of original bounces, if we are
312
                        // use the default color mask
313
                        if ( $x2>=0 && $x2<$max_x && $y2>=0 && $y2<$max_y ){
314
                            $c  = imagecolorat ( $img, $x2, $y2);
0 ignored issues
show
Bug introduced by
$y2 of type double is incompatible with the type integer expected by parameter $y of imagecolorat(). ( Ignorable by Annotation )

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

314
                            $c  = imagecolorat ( $img, $x2, /** @scrutinizer ignore-type */ $y2);
Loading history...
Bug introduced by
$x2 of type double is incompatible with the type integer expected by parameter $x of imagecolorat(). ( Ignorable by Annotation )

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

314
                            $c  = imagecolorat ( $img, /** @scrutinizer ignore-type */ $x2, $y2);
Loading history...
315
                        } else {
316
                            $c  = $mask;
317
                        }
318
                        imagesetpixel($img2,$x+$x_offset,$y+$y_offset,$c);
319
320
                        // round verboten!
321
                        $x2  += $cosT;
322
                        $y2  -= $sinT;
323
                    }
324
                }
325
                break;
326
        }
327
        $this->old_image    = $this->imageHandle;
328
        $this->imageHandle  =  $img2;
329
        return true;
330
    }
331
332
333
   /**
334
    * Resize Action
335
    *
336
    * For GD 2.01+ the new copyresampled function is used
337
    * It uses a bicubic interpolation algorithm to get far
338
    * better result.
339
    *
340
    * @param int  $new_x new width
341
    * @param int  $new_y new height
342
    *
343
    * @return true on success or pear error
344
    * @see PEAR::isError()
345
    */
346
    function _resize($new_x, $new_y) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
347
        if ($this->resized === true) {
348
            return false; /*PEAR::raiseError('You have already resized the image without saving it.  Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);*/
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type true.
Loading history...
349
        }
350
        if(function_exists('ImageCreateTrueColor')){
351
           $new_img = $this->newImgPreserveAlpha( ImageCreateTrueColor($new_x,$new_y) );
352
        } else {
353
            $new_img =ImageCreate($new_x,$new_y);
354
        }
355
356
        if(function_exists('ImageCopyResampled')){
357
            ImageCopyResampled($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
0 ignored issues
show
Bug introduced by
It seems like $new_img 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

357
            ImageCopyResampled(/** @scrutinizer ignore-type */ $new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
Loading history...
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $src_image of imagecopyresampled(). ( Ignorable by Annotation )

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

357
            ImageCopyResampled($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
Loading history...
358
        } else {
359
            ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
0 ignored issues
show
Bug introduced by
It seems like $new_img can also be of type false; however, parameter $dst_image of imagecopyresized() 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

359
            ImageCopyResized(/** @scrutinizer ignore-type */ $new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
Loading history...
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $src_image of imagecopyresized(). ( Ignorable by Annotation )

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

359
            ImageCopyResized($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
Loading history...
360
        }
361
362
        $this->old_image = $this->imageHandle;
363
        $this->imageHandle = $new_img;
364
        $this->resized = true;
365
366
        $this->new_x = $new_x;
367
        $this->new_y = $new_y;
368
        return true;
369
    }
370
371
    /**
372
     * Crop the image
373
     *
374
     * @param int $crop_x left column of the image
375
     * @param int $crop_y top row of the image
376
     * @param int $crop_width new cropped image width
377
     * @param int $crop_height new cropped image height
378
     */
379
    function crop($new_x, $new_y, $new_width, $new_height) 
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
380
    {
381
        if(function_exists('ImageCreateTrueColor')){
382
            $new_img =  $this->newImgPreserveAlpha(ImageCreateTrueColor($new_width,$new_height));
383
        } else {
384
            $new_img =ImageCreate($new_width,$new_height);
385
        }
386
        if(function_exists('ImageCopyResampled')){
387
            ImageCopyResampled($new_img, $this->imageHandle, 0, 0, $new_x, $new_y,$new_width,$new_height,$new_width,$new_height);
0 ignored issues
show
Bug introduced by
It seems like $new_img 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

387
            ImageCopyResampled(/** @scrutinizer ignore-type */ $new_img, $this->imageHandle, 0, 0, $new_x, $new_y,$new_width,$new_height,$new_width,$new_height);
Loading history...
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $src_image of imagecopyresampled(). ( Ignorable by Annotation )

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

387
            ImageCopyResampled($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, $new_x, $new_y,$new_width,$new_height,$new_width,$new_height);
Loading history...
388
        } else {
389
            ImageCopyResized($new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width,$new_height,$new_width,$new_height);
0 ignored issues
show
Bug introduced by
It seems like $new_img can also be of type false; however, parameter $dst_image of imagecopyresized() 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

389
            ImageCopyResized(/** @scrutinizer ignore-type */ $new_img, $this->imageHandle, 0, 0, $new_x, $new_y, $new_width,$new_height,$new_width,$new_height);
Loading history...
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $src_image of imagecopyresized(). ( Ignorable by Annotation )

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

389
            ImageCopyResized($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, $new_x, $new_y, $new_width,$new_height,$new_width,$new_height);
Loading history...
390
        }
391
        $this->old_image = $this->imageHandle;
392
        $this->imageHandle = $new_img;
393
        $this->resized = true;
394
395
        $this->new_x = $new_x;
396
        $this->new_y = $new_y;
397
        return true;
398
    }
399
   
400
    /**
401
     * Flip the image horizontally or vertically
402
     *
403
     * @param boolean $horizontal true if horizontal flip, vertical otherwise
404
     */
405
    function flip($horizontal)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
406
    {
407
        if(!$horizontal) {
408
            $this->rotate(180);
409
        }
410
411
        $width = imagesx($this->imageHandle); 
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagesx(). ( Ignorable by Annotation )

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

411
        $width = imagesx(/** @scrutinizer ignore-type */ $this->imageHandle); 
Loading history...
412
        $height = imagesy($this->imageHandle); 
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagesy(). ( Ignorable by Annotation )

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

412
        $height = imagesy(/** @scrutinizer ignore-type */ $this->imageHandle); 
Loading history...
413
414
        for ($j = 0; $j < $height; $j++) { 
415
                $left = 0; 
416
                $right = $width-1; 
417
418
419
                while ($left < $right) { 
420
                    //echo " j:".$j." l:".$left." r:".$right."\n<br>";
421
                    $t = imagecolorat($this->imageHandle, $left, $j); 
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolorat(). ( Ignorable by Annotation )

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

421
                    $t = imagecolorat(/** @scrutinizer ignore-type */ $this->imageHandle, $left, $j); 
Loading history...
422
                    imagesetpixel($this->imageHandle, $left, $j, imagecolorat($this->imageHandle, $right, $j)); 
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagesetpixel(). ( Ignorable by Annotation )

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

422
                    imagesetpixel(/** @scrutinizer ignore-type */ $this->imageHandle, $left, $j, imagecolorat($this->imageHandle, $right, $j)); 
Loading history...
423
                    imagesetpixel($this->imageHandle, $right, $j, $t); 
424
                    $left++; $right--; 
425
                } 
426
            
427
        }
428
429
        return true;
430
    }
431
432
433
    /**
434
     * Adjust the image gamma
435
     *
436
     * @param float $outputgamma
437
     *
438
     * @return none
439
     */
440
    function gamma($outputgamma=1.0) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
441
        ImageGammaCorrect($this->imageHandle, 1.0, $outputgamma);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagegammacorrect(). ( Ignorable by Annotation )

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

441
        ImageGammaCorrect(/** @scrutinizer ignore-type */ $this->imageHandle, 1.0, $outputgamma);
Loading history...
442
    }
443
	function paletteToTrueColorWithTransparency()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
444
	{
445
		$oldImg = $this->imageHandle;
446
		$newImg = $this->newImgPreserveAlpha( imagecreatetruecolor($this->img_x,$this->img_y) );
447
		imagecopy($newImg,$oldImg,0,0,0,0,$this->img_x,$this->img_y);
0 ignored issues
show
Bug introduced by
It seems like $newImg 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

447
		imagecopy(/** @scrutinizer ignore-type */ $newImg,$oldImg,0,0,0,0,$this->img_x,$this->img_y);
Loading history...
Bug introduced by
$oldImg of type string is incompatible with the type resource expected by parameter $src_im of imagecopy(). ( Ignorable by Annotation )

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

447
		imagecopy($newImg,/** @scrutinizer ignore-type */ $oldImg,0,0,0,0,$this->img_x,$this->img_y);
Loading history...
448
449
		$this->imageHandle = $newImg;
450
	}
451
	
452
	function newImgPreserveAlpha($newImg)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
453
	{
454
		if ( $this->type == 'jpeg') return $newImg;
455
		
456
		// Turn off transparency blending (temporarily)
457
		imagealphablending($newImg, false);
458
		
459
		// Create a new transparent color for image
460
		if ( $transparent = imagecolortransparent($this->imageHandle) >= 0 )
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolortransparent(). ( Ignorable by Annotation )

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

460
		if ( $transparent = imagecolortransparent(/** @scrutinizer ignore-type */ $this->imageHandle) >= 0 )
Loading history...
461
		{
462
			if (imageistruecolor($this->imageHandle))
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imageistruecolor(). ( Ignorable by Annotation )

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

462
			if (imageistruecolor(/** @scrutinizer ignore-type */ $this->imageHandle))
Loading history...
463
			{
464
				$red = ($transparent & 0xFF0000) >> 16;
465
				$green = ($transparent & 0x00FF00) >> 8;
466
				$blue = ($transparent & 0x0000FF);
467
				$color_values = array('red' => $red, 'green' => $green, 'blue' => $blue);
0 ignored issues
show
Unused Code introduced by
The assignment to $color_values is dead and can be removed.
Loading history...
468
			}
469
			else
470
			{
471
				$color_values = imagecolorsforindex($this->imageHandle,$transparent);
0 ignored issues
show
Bug introduced by
$transparent of type true is incompatible with the type integer expected by parameter $index of imagecolorsforindex(). ( Ignorable by Annotation )

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

471
				$color_values = imagecolorsforindex($this->imageHandle,/** @scrutinizer ignore-type */ $transparent);
Loading history...
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolorsforindex(). ( Ignorable by Annotation )

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

471
				$color_values = imagecolorsforindex(/** @scrutinizer ignore-type */ $this->imageHandle,$transparent);
Loading history...
472
473
			}
474
			$color_values = imagecolorsforindex($this->imageHandle,$transparent);
475
			$color = imagecolorallocatealpha($newImg, $color_values['red'],$color_values['green'],$color_values['blue'], 127);
476
			$colort = imagecolorallocate($newImg, $color_values['red'],$color_values['green'],$color_values['blue']);
477
		}
478
		else
479
		{
480
			$color = imagecolorallocatealpha($newImg, 252, 2, 252, 127);
481
			$colort = imagecolorallocate($newImg, 252, 2, 252);
482
		}
483
		imagecolortransparent($newImg,$colort);
484
		
485
		// Completely fill the background of the new image with allocated color.
486
		imagefill($newImg, 0, 0, $color);
487
		
488
		// Restore transparency blending
489
		imagesavealpha($newImg, true);
490
		
491
		return $newImg;
492
	}
493
	
494
	function preserveTransparencyForPalette()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
495
	{
496
		$new_img = imagecreatetruecolor($this->img_x,$this->img_y);
497
		$truecolor = imageistruecolor($this->imageHandle);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imageistruecolor(). ( Ignorable by Annotation )

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

497
		$truecolor = imageistruecolor(/** @scrutinizer ignore-type */ $this->imageHandle);
Loading history...
498
		$transparent = imagecolorallocate($new_img, 252,2,252); // nasty pinkish purple that hopefully doesn't exist in the image
0 ignored issues
show
Bug introduced by
It seems like $new_img can also be of type false; however, parameter $image of imagecolorallocate() 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

498
		$transparent = imagecolorallocate(/** @scrutinizer ignore-type */ $new_img, 252,2,252); // nasty pinkish purple that hopefully doesn't exist in the image
Loading history...
499
500
		imagecolortransparent($new_img, $transparent);
0 ignored issues
show
Bug introduced by
It seems like $new_img can also be of type false; however, parameter $image of imagecolortransparent() 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

500
		imagecolortransparent(/** @scrutinizer ignore-type */ $new_img, $transparent);
Loading history...
501
		for ($i=0;$i<$this->img_y;$i++)
502
		{
503
			for ($j=0;$j<$this->img_x;$j++)
504
			{
505
				$c = imagecolorat($this->imageHandle,$j, $i);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolorat(). ( Ignorable by Annotation )

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

505
				$c = imagecolorat(/** @scrutinizer ignore-type */ $this->imageHandle,$j, $i);
Loading history...
506
				if ($truecolor)
507
				{
508
					$a = ($c >> 24) & 0xFF;
509
					$r = ($c >> 16) & 0xFF;
510
					$g = ($c >> 8) & 0xFF;
511
					$b = $c & 0xFF;
512
					$color_values = array('red' => $r, 'green' => $g, 'blue' => $b, 'alpha' => $a);
513
				}
514
				else
515
				{
516
					$color_values = imagecolorsforindex($this->imageHandle,$c);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagecolorsforindex(). ( Ignorable by Annotation )

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

516
					$color_values = imagecolorsforindex(/** @scrutinizer ignore-type */ $this->imageHandle,$c);
Loading history...
517
				}
518
				if ($color_values['alpha'] >= 126)
519
				{
520
					imagesetpixel($new_img, $j, $i, $transparent);
0 ignored issues
show
Bug introduced by
It seems like $new_img can also be of type false; however, parameter $image of imagesetpixel() 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

520
					imagesetpixel(/** @scrutinizer ignore-type */ $new_img, $j, $i, $transparent);
Loading history...
521
				}
522
				else
523
				{
524
					imagesetpixel($new_img, $j, $i, $c);
525
				}
526
			}
527
		}
528
		$this->imageHandle = $new_img;
529
	}
530
531
    /**
532
     * Save the image file
533
     *
534
     * @param string  $filename the name of the file to write to
535
     * @param int     $quality  output DPI, default is 85
536
     * @param string  $types    define the output format, default
537
     *                          is the current used format
538
     *
539
     * @return none
540
     */
541
    function save($filename, $type = '', $quality = 85)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
542
    {
543
		$type           = $type==''? $this->type : $type;
544
		$functionName   = 'image' . $type;
545
546
		if(function_exists($functionName))
547
		{
548
			$this->old_image = $this->imageHandle;
549
			if($type=='jpeg')
550
				$functionName($this->imageHandle, $filename, $quality);
551
			else
552
				$functionName($this->imageHandle, $filename);
553
			$this->imageHandle = $this->old_image;
554
			$this->resized = false;
555
		}
556
    } // End save
557
558
559
    /**
560
     * Display image without saving and lose changes
561
     *
562
     * @param string type (JPG,PNG...);
563
     * @param int quality 75
0 ignored issues
show
Bug introduced by
The type quality 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...
564
     *
565
     * @return none
566
     */
567
    function display($type = '', $quality = 75)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
568
    {
569
        if ($type != '') {
570
            $this->type = $type;
571
        }
572
        $functionName = 'Image' . $this->type;
573
		if(function_exists($functionName))
574
		{
575
			header('Content-type: image/' . strtolower($this->type));
576
			$functionName($this->imageHandle, '', $quality);
577
			$this->imageHandle = $this->old_image;
578
			$this->resized = false;
579
			ImageDestroy($this->old_image);
0 ignored issues
show
Bug introduced by
$this->old_image of type string is incompatible with the type resource expected by parameter $image of imagedestroy(). ( Ignorable by Annotation )

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

579
			ImageDestroy(/** @scrutinizer ignore-type */ $this->old_image);
Loading history...
580
			$this->free();
581
		}
582
    }
583
584
    /**
585
     * Destroy image handle
586
     *
587
     * @return none
588
     */
589
    function free()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
590
    {
591
        if ($this->imageHandle){
592
            ImageDestroy($this->imageHandle);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type resource expected by parameter $image of imagedestroy(). ( Ignorable by Annotation )

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

592
            ImageDestroy(/** @scrutinizer ignore-type */ $this->imageHandle);
Loading history...
593
        }
594
    }
595
596
} // End class ImageGD
597
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
598