Image_Transform_Driver_GD::rotate()   F
last analyzed

Complexity

Conditions 32
Paths 2305

Size

Total Lines 164
Code Lines 115

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 32
eloc 115
c 1
b 0
f 0
nc 2305
nop 2
dl 0
loc 164
rs 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
 ** 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
// +----------------------------------------------------------------------+
12
// | PHP Version 4                                                        |
13
// +----------------------------------------------------------------------+
14
// | Copyright (c) 1997-2002 The PHP Group                                |
15
// +----------------------------------------------------------------------+
16
// | This source file is subject to version 2.02 of the PHP license,      |
17
// | that is bundled with this package in the file LICENSE, and is        |
18
// | available at through the world-wide-web at                           |
19
// | http://www.php.net/license/2_02.txt.                                 |
20
// | If you did not receive a copy of the PHP license and are unable to   |
21
// | obtain it through the world-wide-web, please send a note to          |
22
// | [email protected] so we can mail you a copy immediately.               |
23
// +----------------------------------------------------------------------+
24
// | Authors: Peter Bowyer <[email protected]>                      |
25
// |          Alan Knowles <[email protected]>                            |
26
// +----------------------------------------------------------------------+
27
//
28
//    Usage :
29
//    $img    = new Image_Transform_GD();
30
//    $angle  = -78;
31
//    $img->load('magick.png');
32
//
33
//    if($img->rotate($angle,array('autoresize'=>true,'color_mask'=>array(255,0,0)))){
34
//        $img->addText(array('text'=>"Rotation $angle",'x'=>0,'y'=>100,'font'=>'/usr/share/fonts/default/TrueType/cogb____.ttf'));
35
//        $img->display();
36
//    } else {
37
//        echo "Error";
38
//    }
39
//
40
//
41
// $Id:GD.php 938 2008-01-22 20:13:47Z ray $
42
//
43
// Image Transformation interface using the GD library
44
//
45
46
require_once '../ImageManager/Classes/Transform.php';
47
48
Class Image_Transform_Driver_GD extends Image_Transform
49
{
50
    /**
51
     * Holds the image file for manipulation
52
     */
53
    public $imageHandle = '';
54
55
    /**
56
     * Holds the original image file
57
     */
58
    public $old_image = '';
59
60
    /**
61
     * Check settings
62
     *
63
     * @return mixed true or  or a PEAR error object on error
64
     *
65
     * @see PEAR::isError()
66
     */
67
    public function Image_Transform_GD()
68
    {
69
        return;
70
    } // End function Image
71
72
    /**
73
     * Load image
74
     *
75
     * @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...
76
     *
77
     * @return mixed none or a PEAR error object on error
78
     * @see PEAR::isError()
79
     */
80
    public function load($image)
81
    {
82
        $this->uid   = md5($_SERVER['REMOTE_ADDR']);
83
        $this->image = $image;
84
        $this->_get_image_details($image);
85
        $functionName = 'ImageCreateFrom' . $this->type;
86
87
        if (function_exists($functionName)) {
88
            $this->imageHandle = $functionName($this->image);
89
            if ('png' == $this->type) {
90
                imageAlphaBlending($this->imageHandle, false);
91
                imageSaveAlpha($this->imageHandle, true);
92
            }
93
        }
94
    } // End load
95
96
    /**
97
     * addText
98
     *
99
     * @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...
100
     *                                                  array(
101
     *                                                  'text'  The string to draw
102
     *                                                  'x'     Horizontal position
103
     *                                                  'y'     Vertical Position
104
     *                                                  'Color' Font color
105
     *                                                  'font'  Font to be used
106
     *                                                  'size'  Size of the fonts in pixel
107
     *                                                  'resize_first'  Tell if the image has to be resized
108
     *                                                  before drawing the text
109
     *                                                  )
110
     *
111
     * @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...
112
     * @see PEAR::isError()
113
     */
114
    public function addText($params)
115
    {
116
        $default_params = [
117
            'text'         => 'This is Text',
118
            'x'            => 10,
119
            'y'            => 20,
120
            'color'        => [255, 0, 0],
121
            'font'         => 'Arial.ttf',
122
            'size'         => '12',
123
            'angle'        => 0,
124
            'resize_first' => false // Carry out the scaling of the image before annotation?  Not used for GD
125
        ];
126
        $params         = array_merge($default_params, $params);
127
        extract($params);
128
129
        if (!is_array($color)) {
130
            if ('#' == $color[0]) {
131
                $this->colorhex2colorarray($color);
132
            } else {
133
                include_once('Image/Transform/Driver/ColorsDefs.php');
134
                $color = isset($colornames[$color]) ? $colornames[$color] : false;
135
            }
136
        }
137
138
        $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 GdImage|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

138
        $c = imagecolorresolve(/** @scrutinizer ignore-type */ $this->imageHandle, $color[0], $color[1], $color[2]);
Loading history...
139
140
        if ('ttf' == substr($font, -3)) {
141
            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 GdImage|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

141
            ImageTTFText(/** @scrutinizer ignore-type */ $this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
Loading history...
142
        } else {
143
            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 GdImage|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

143
            ImagePSText(/** @scrutinizer ignore-type */ $this->imageHandle, $size, $angle, $x, $y, $c, $font, $text);
Loading history...
144
        }
145
        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...
146
    } // End addText
147
148
    /**
149
     * Rotate image by the given angle
150
     * Uses a fast rotation algorythm for custom angles
151
     * or lines copy for multiple of 90 degrees
152
     *
153
     * @param int   $angle                  Rotation angle
154
     * @param array $options                array(  'autoresize'=>true|false,
155
     *                                      'color_mask'=>array(r,g,b), named color or #rrggbb
156
     *                                      )
157
     * @return mixed none or a PEAR error object on error
158
     * @author Pierre-Alain Joye
159
     * @see    PEAR::isError()
160
     */
161
    public function rotate($angle, $options = null)
162
    {
163
        if (function_exists('imagerotate')) {
164
            $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 GdImage|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

164
            $white             = imagecolorallocatealpha(/** @scrutinizer ignore-type */ $this->imageHandle, 255, 255, 255, 127);
Loading history...
165
            $this->imageHandle = imagerotate($this->imageHandle, $angle, $white);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

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

256
        imagepalettecopy($img2, /** @scrutinizer ignore-type */ $img);
Loading history...
257
258
        $mask = imagecolorallocatealpha($img2, $color_mask[0], $color_mask[1], $color_mask[2], 127);
259
        // use simple lines copy for axes angles
260
        switch ((int)($angle)) {
261
            case 0:
262
                imagefill($img2, 0, 0, $mask);
263
                for ($y = 0; $y < $max_y; $y++) {
264
                    for ($x = $min_x; $x < $max_x; $x++) {
265
                        $c = @imagecolorat($img, $x, $y);
0 ignored issues
show
Bug introduced by
$img of type string is incompatible with the type GdImage|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

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

309
                            $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

309
                            $c = imagecolorat($img, /** @scrutinizer ignore-type */ $x2, $y2);
Loading history...
310
                        } else {
311
                            $c = $mask;
312
                        }
313
                        imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c);
314
315
                        // round verboten!
316
                        $x2 += $cosT;
317
                        $y2 -= $sinT;
318
                    }
319
                }
320
                break;
321
        }
322
        $this->old_image   = $this->imageHandle;
323
        $this->imageHandle = $img2;
324
        return true;
325
    }
326
327
    /**
328
     * Resize Action
329
     *
330
     * For GD 2.01+ the new copyresampled function is used
331
     * It uses a bicubic interpolation algorithm to get far
332
     * better result.
333
     *
334
     * @param int $new_x new width
335
     * @param int $new_y new height
336
     *
337
     * @return true on success or pear error
338
     * @see PEAR::isError()
339
     */
340
    public function _resize($new_x, $new_y)
341
    {
342
        if (true === $this->resized) {
343
            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...
344
        }
345
        if (function_exists('ImageCreateTrueColor')) {
346
            $new_img = $this->newImgPreserveAlpha(ImageCreateTrueColor($new_x, $new_y));
347
        } else {
348
            $new_img = ImageCreate($new_x, $new_y);
349
        }
350
351
        if (function_exists('ImageCopyResampled')) {
352
            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
$this->imageHandle of type string is incompatible with the type GdImage|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

352
            ImageCopyResampled($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
Loading history...
353
        } else {
354
            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
$this->imageHandle of type string is incompatible with the type GdImage|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

354
            ImageCopyResized($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
Loading history...
355
        }
356
357
        $this->old_image   = $this->imageHandle;
358
        $this->imageHandle = $new_img;
359
        $this->resized     = true;
360
361
        $this->new_x = $new_x;
362
        $this->new_y = $new_y;
363
        return true;
364
    }
365
366
    /**
367
     * Crop the image
368
     *
369
     * @param int $crop_x      left column of the image
370
     * @param int $crop_y      top row of the image
371
     * @param int $crop_width  new cropped image width
372
     * @param int $crop_height new cropped image height
373
     */
374
    public function crop($new_x, $new_y, $new_width, $new_height)
375
    {
376
        if (function_exists('ImageCreateTrueColor')) {
377
            $new_img = $this->newImgPreserveAlpha(ImageCreateTrueColor($new_width, $new_height));
378
        } else {
379
            $new_img = ImageCreate($new_width, $new_height);
380
        }
381
        if (function_exists('ImageCopyResampled')) {
382
            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
$this->imageHandle of type string is incompatible with the type GdImage|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

382
            ImageCopyResampled($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, $new_x, $new_y, $new_width, $new_height, $new_width, $new_height);
Loading history...
383
        } else {
384
            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
$this->imageHandle of type string is incompatible with the type GdImage|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

384
            ImageCopyResized($new_img, /** @scrutinizer ignore-type */ $this->imageHandle, 0, 0, $new_x, $new_y, $new_width, $new_height, $new_width, $new_height);
Loading history...
385
        }
386
        $this->old_image   = $this->imageHandle;
387
        $this->imageHandle = $new_img;
388
        $this->resized     = true;
389
390
        $this->new_x = $new_x;
391
        $this->new_y = $new_y;
392
        return true;
393
    }
394
395
    /**
396
     * Flip the image horizontally or vertically
397
     *
398
     * @param bool $horizontal true if horizontal flip, vertical otherwise
399
     */
400
    public function flip($horizontal)
401
    {
402
        if (!$horizontal) {
403
            $this->rotate(180);
404
        }
405
406
        $width  = imagesx($this->imageHandle);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

406
        $width  = imagesx(/** @scrutinizer ignore-type */ $this->imageHandle);
Loading history...
407
        $height = imagesy($this->imageHandle);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

407
        $height = imagesy(/** @scrutinizer ignore-type */ $this->imageHandle);
Loading history...
408
409
        for ($j = 0; $j < $height; $j++) {
410
            $left  = 0;
411
            $right = $width - 1;
412
413
            while ($left < $right) {
414
                //echo " j:".$j." l:".$left." r:".$right."\n<br>";
415
                $t = imagecolorat($this->imageHandle, $left, $j);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

415
                $t = imagecolorat(/** @scrutinizer ignore-type */ $this->imageHandle, $left, $j);
Loading history...
416
                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 GdImage|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

416
                imagesetpixel(/** @scrutinizer ignore-type */ $this->imageHandle, $left, $j, imagecolorat($this->imageHandle, $right, $j));
Loading history...
417
                imagesetpixel($this->imageHandle, $right, $j, $t);
418
                $left++;
419
                $right--;
420
            }
421
        }
422
423
        return true;
424
    }
425
426
    /**
427
     * Adjust the image gamma
428
     *
429
     * @param float $outputgamma
430
     *
431
     * @return none
432
     */
433
    public function gamma($outputgamma = 1.0)
434
    {
435
        ImageGammaCorrect($this->imageHandle, 1.0, $outputgamma);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

435
        ImageGammaCorrect(/** @scrutinizer ignore-type */ $this->imageHandle, 1.0, $outputgamma);
Loading history...
436
    }
437
438
    public function paletteToTrueColorWithTransparency()
439
    {
440
        $oldImg = $this->imageHandle;
441
        $newImg = $this->newImgPreserveAlpha(imagecreatetruecolor($this->img_x, $this->img_y));
442
        imagecopy($newImg, $oldImg, 0, 0, 0, 0, $this->img_x, $this->img_y);
0 ignored issues
show
Bug introduced by
$oldImg of type string is incompatible with the type GdImage|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

442
        imagecopy($newImg, /** @scrutinizer ignore-type */ $oldImg, 0, 0, 0, 0, $this->img_x, $this->img_y);
Loading history...
443
444
        $this->imageHandle = $newImg;
445
    }
446
447
    public function newImgPreserveAlpha($newImg)
448
    {
449
        if ('jpeg' == $this->type) {
450
            return $newImg;
451
        }
452
453
        // Turn off transparency blending (temporarily)
454
        imagealphablending($newImg, false);
455
456
        // Create a new transparent color for image
457
        if ($transparent = imagecolortransparent($this->imageHandle) >= 0) {
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

457
        if ($transparent = imagecolortransparent(/** @scrutinizer ignore-type */ $this->imageHandle) >= 0) {
Loading history...
458
            if (imageistruecolor($this->imageHandle)) {
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

458
            if (imageistruecolor(/** @scrutinizer ignore-type */ $this->imageHandle)) {
Loading history...
459
                $red          = ($transparent & 0xFF0000) >> 16;
460
                $green        = ($transparent & 0x00FF00) >> 8;
461
                $blue         = ($transparent & 0x0000FF);
462
                $color_values = ['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...
463
            } else {
464
                $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

464
                $color_values = imagecolorsforindex($this->imageHandle, /** @scrutinizer ignore-type */ $transparent);
Loading history...
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

464
                $color_values = imagecolorsforindex(/** @scrutinizer ignore-type */ $this->imageHandle, $transparent);
Loading history...
465
            }
466
            $color_values = imagecolorsforindex($this->imageHandle, $transparent);
467
            $color        = imagecolorallocatealpha($newImg, $color_values['red'], $color_values['green'], $color_values['blue'], 127);
468
            $colort       = imagecolorallocate($newImg, $color_values['red'], $color_values['green'], $color_values['blue']);
469
        } else {
470
            $color  = imagecolorallocatealpha($newImg, 252, 2, 252, 127);
471
            $colort = imagecolorallocate($newImg, 252, 2, 252);
472
        }
473
        imagecolortransparent($newImg, $colort);
474
475
        // Completely fill the background of the new image with allocated color.
476
        imagefill($newImg, 0, 0, $color);
477
478
        // Restore transparency blending
479
        imagesavealpha($newImg, true);
480
481
        return $newImg;
482
    }
483
484
    public function preserveTransparencyForPalette()
485
    {
486
        $new_img     = imagecreatetruecolor($this->img_x, $this->img_y);
487
        $truecolor   = imageistruecolor($this->imageHandle);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

487
        $truecolor   = imageistruecolor(/** @scrutinizer ignore-type */ $this->imageHandle);
Loading history...
488
        $transparent = imagecolorallocate($new_img, 252, 2, 252); // nasty pinkish purple that hopefully doesn't exist in the image
489
490
        imagecolortransparent($new_img, $transparent);
491
        for ($i = 0; $i < $this->img_y; $i++) {
492
            for ($j = 0; $j < $this->img_x; $j++) {
493
                $c = imagecolorat($this->imageHandle, $j, $i);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

493
                $c = imagecolorat(/** @scrutinizer ignore-type */ $this->imageHandle, $j, $i);
Loading history...
494
                if ($truecolor) {
495
                    $a            = ($c >> 24) & 0xFF;
496
                    $r            = ($c >> 16) & 0xFF;
497
                    $g            = ($c >> 8) & 0xFF;
498
                    $b            = $c & 0xFF;
499
                    $color_values = ['red' => $r, 'green' => $g, 'blue' => $b, 'alpha' => $a];
500
                } else {
501
                    $color_values = imagecolorsforindex($this->imageHandle, $c);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

501
                    $color_values = imagecolorsforindex(/** @scrutinizer ignore-type */ $this->imageHandle, $c);
Loading history...
502
                }
503
                if ($color_values['alpha'] >= 126) {
504
                    imagesetpixel($new_img, $j, $i, $transparent);
505
                } else {
506
                    imagesetpixel($new_img, $j, $i, $c);
507
                }
508
            }
509
        }
510
        $this->imageHandle = $new_img;
511
    }
512
513
    /**
514
     * Save the image file
515
     *
516
     * @param string $filename  the name of the file to write to
517
     * @param int    $quality   output DPI, default is 85
518
     * @param string $types     define the output format, default
519
     *                          is the current used format
520
     *
521
     * @return none
522
     */
523
    public function save($filename, $type = '', $quality = 85)
524
    {
525
        $type         = '' == $type ? $this->type : $type;
526
        $functionName = 'image' . $type;
527
528
        if (function_exists($functionName)) {
529
            $this->old_image = $this->imageHandle;
530
            if ('jpeg' == $type) {
531
                $functionName($this->imageHandle, $filename, $quality);
532
            } else {
533
                $functionName($this->imageHandle, $filename);
534
            }
535
            $this->imageHandle = $this->old_image;
536
            $this->resized     = false;
537
        }
538
    } // End save
539
540
    /**
541
     * Display image without saving and lose changes
542
     *
543
     * @param string type (JPG,PNG...);
544
     * @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...
545
     *
546
     * @return none
547
     */
548
    public function display($type = '', $quality = 75)
549
    {
550
        if ('' != $type) {
551
            $this->type = $type;
552
        }
553
        $functionName = 'Image' . $this->type;
554
        if (function_exists($functionName)) {
555
            header('Content-type: image/' . strtolower($this->type));
556
            $functionName($this->imageHandle, '', $quality);
557
            $this->imageHandle = $this->old_image;
558
            $this->resized     = false;
559
            ImageDestroy($this->old_image);
0 ignored issues
show
Bug introduced by
$this->old_image of type string is incompatible with the type GdImage|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

559
            ImageDestroy(/** @scrutinizer ignore-type */ $this->old_image);
Loading history...
560
            $this->free();
561
        }
562
    }
563
564
    /**
565
     * Destroy image handle
566
     *
567
     * @return none
568
     */
569
    public function free()
570
    {
571
        if ($this->imageHandle) {
572
            ImageDestroy($this->imageHandle);
0 ignored issues
show
Bug introduced by
$this->imageHandle of type string is incompatible with the type GdImage|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

572
            ImageDestroy(/** @scrutinizer ignore-type */ $this->imageHandle);
Loading history...
573
        }
574
    }
575
576
} // End class ImageGD
577
578