Passed
Push — master ( 8d8e58...047d50 )
by Michael
02:21
created

Image_Transform_Driver_GD1   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 219
rs 9
c 0
b 0
f 0
wmc 35

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
F rotate() 0 162 31
A _resize() 0 15 2
A Image_Transform_Driver_GD1() 0 3 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
// +----------------------------------------------------------------------+
3
// | PHP Version 4                                                        |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1997-2003 The PHP Group                                |
6
// +----------------------------------------------------------------------+
7
// | This source file is subject to version 2.02 of the PHP license,      |
8
// | that is bundled with this package in the file LICENSE, and is        |
9
// | available at through the world-wide-web at                           |
10
// | http://www.php.net/license/2_02.txt.                                 |
11
// | If you did not receive a copy of the PHP license and are unable to   |
12
// | obtain it through the world-wide-web, please send a note to          |
13
// | [email protected] so we can mail you a copy immediately.               |
14
// +----------------------------------------------------------------------+
15
// | Authors: Peter Bowyer <[email protected]>                      |
16
// |          Alan Knowles <[email protected]>                            |
17
// +----------------------------------------------------------------------+
18
19
//require_once 'Image/Transform/Driver/GD.php';
20
require_once XOOPS_ROOT_PATH . '/modules/extgallery/class/pear/Image/Transform/Driver/GD.php';
0 ignored issues
show
Bug introduced by
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
21
22
/**
23
 * This driver is for GD1 or the non-bundled version of GD2
24
 *
25
 * @package
26
 * @author    NAWAL ASWAN
27
 * @copyright Copyright (c) 2003
28
 * @version   $Id: GD1.php 234123 2007-04-19 16:36:09Z dufuz $
29
 * @access    public
30
 **/
31
class Image_Transform_Driver_GD1 extends Image_Transform_Driver_GD
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    /**
34
     * Check settings
35
     *
36
     * @return mixed true or  or a PEAR error object on error
37
     *
38
     * @see PEAR::isError()
39
     */
40
    public function Image_Transform_Driver_GD1()
41
    {
42
        $this->__construct();
43
    } // End function Image
44
45
    /**
46
     * Check settings
47
     *
48
     * @return mixed true or  or a PEAR error object on error
49
     *
50
     * @see PEAR::isError()
51
     */
52
    public function __construct()
53
    {
54
        parent::__construct();
55
    } // End function Image
56
57
    /**
58
     * Resize Action
59
     *
60
     * For GD 2.01+ the new copyresampled function is used
61
     * It uses a bicubic interpolation algorithm to get far
62
     * better result.
63
     *
64
     * @param       $new_x   int  new width
65
     * @param       $new_y   int  new height
66
     * @param mixed $options Optional parameters
67
     *
68
     * @return true on success or PEAR Error object on error
69
     * @see PEAR::isError()
70
     */
71
    public function _resize($new_x, $new_y, $options = null)
72
    {
73
        if (true === $this->resized) {
74
            return 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 introduced by
The method raiseError() does not exist on PEAR. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

74
            return PEAR::/** @scrutinizer ignore-call */ raiseError('You have already resized the image without saving it.  Your previous resizing will be overwritten', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);
Loading history...
75
        }
76
        $new_img = ImageCreate($new_x, $new_y);
77
        ImageCopyResized($new_img, $this->imageHandle, 0, 0, 0, 0, $new_x, $new_y, $this->img_x, $this->img_y);
78
        $this->old_image   = $this->imageHandle;
0 ignored issues
show
Bug Best Practice introduced by
The property old_image does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
79
        $this->imageHandle = $new_img;
80
        $this->resized     = true;
81
82
        $this->new_x = $new_x;
83
        $this->new_y = $new_y;
84
85
        return true;
86
    }
87
88
    public function rotate($angle, $options = null)
89
    {
90
        if (null == $options) {
91
            $autoresize = true;
92
            $color_mask = array(255, 255, 0);
93
        } else {
94
            extract($options);
95
        }
96
97
        while ($angle <= -45) {
98
            $angle += 360;
99
        }
100
        while ($angle > 270) {
101
            $angle -= 360;
102
        }
103
104
        $t = deg2rad($angle);
105
106
        if (!is_array($color_mask)) {
107
            // Not already in numberical format, so we convert it.
108
            if ('#' == $color_mask{0}) {
109
                $color_mask = $this->colorhex2colorarray($color_mask);
110
            } else {
111
                require_once 'Image/Transform/Driver/ColorsDefs.php';
112
                $color_mask = isset($colornames[$color_mask]) ? $colornames[$color_mask] : false;
113
            }
114
        }
115
116
        // Do not round it, too much lost of quality
117
        $cosT = cos($t);
118
        $sinT = sin($t);
119
120
        $img =& $this->imageHandle;
121
122
        $width  = $max_x = $this->img_x;
123
        $height = $max_y = $this->img_y;
124
        $min_y  = 0;
125
        $min_x  = 0;
126
127
        $x1 = round($max_x / 2, 0);
128
        $y1 = round($max_y / 2, 0);
129
130
        if ($autoresize) {
131
            $t = abs($t);
132
            $a = round($angle, 0);
0 ignored issues
show
Unused Code introduced by
The assignment to $a is dead and can be removed.
Loading history...
133
            switch ((int)($angle)) {
134
                case 0:
135
                    $width2  = $width;
136
                    $height2 = $height;
137
                    break;
138
                case 90:
139
                    $width2  = $height;
140
                    $height2 = $width;
141
                    break;
142
                case 180:
143
                    $width2  = $width;
144
                    $height2 = $height;
145
                    break;
146
                case 270:
147
                    $width2  = $height;
148
                    $height2 = $width;
149
                    break;
150
                default:
151
                    $width2  = (int)(abs(sin($t) * $height + cos($t) * $width));
152
                    $height2 = (int)(abs(cos($t) * $height + sin($t) * $width));
153
            }
154
155
            $width2  -= $width2 % 2;
156
            $height2 -= $height2 % 2;
157
158
            $d_width  = abs($width - $width2);
159
            $d_height = abs($height - $height2);
160
            $x_offset = $d_width / 2;
161
            $y_offset = $d_height / 2;
162
            $min_x2   = -abs($x_offset);
163
            $min_y2   = -abs($y_offset);
164
            $max_x2   = $width2;
165
            $max_y2   = $height2;
166
        }
167
168
        $img2 = @imagecreateTrueColor($width2, $height2);
169
170
        if (!is_resource($img2)) {
171
            return PEAR::raiseError('Cannot create buffer for the rotataion.', null, PEAR_ERROR_TRIGGER, E_USER_NOTICE);
172
        }
173
174
        $this->img_x = $width2;
175
        $this->img_y = $height2;
176
177
        imagepalettecopy($img2, $img);
178
179
        $mask = imagecolorresolve($img2, $color_mask[0], $color_mask[1], $color_mask[2]);
180
181
        // use simple lines copy for axes angles
182
        switch ((int)($angle)) {
183
            case 0:
184
                imagefill($img2, 0, 0, $mask);
185
                for ($y = 0; $y < $max_y; $y++) {
186
                    for ($x = $min_x; $x < $max_x; $x++) {
187
                        $c = @imagecolorat($img, $x, $y);
188
                        imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c);
189
                    }
190
                }
191
                break;
192
            case 90:
193
                imagefill($img2, 0, 0, $mask);
194
                for ($x = $min_x; $x < $max_x; $x++) {
195
                    for ($y = $min_y; $y < $max_y; $y++) {
196
                        $c = imagecolorat($img, $x, $y);
197
                        imagesetpixel($img2, $max_y - $y - 1, $x, $c);
198
                    }
199
                }
200
                break;
201
            case 180:
202
                imagefill($img2, 0, 0, $mask);
203
                for ($y = 0; $y < $max_y; $y++) {
204
                    for ($x = $min_x; $x < $max_x; $x++) {
205
                        $c = @imagecolorat($img, $x, $y);
206
                        imagesetpixel($img2, $max_x2 - $x - 1, $max_y2 - $y - 1, $c);
207
                    }
208
                }
209
                break;
210
            case 270:
211
                imagefill($img2, 0, 0, $mask);
212
                for ($y = 0; $y < $max_y; $y++) {
213
                    for ($x = $max_x; $x >= $min_x; $x--) {
214
                        $c = @imagecolorat($img, $x, $y);
215
                        imagesetpixel($img2, $y, $max_x - $x - 1, $c);
216
                    }
217
                }
218
                break;
219
            // simple reverse rotation algo
220
            default:
221
                $i = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
222
                for ($y = $min_y2; $y < $max_y2; $y++) {
223
224
                    // Algebra :)
225
                    $x2 = round((($min_x2 - $x1) * $cosT) + (($y - $y1) * $sinT + $x1), 0);
226
                    $y2 = round((($y - $y1) * $cosT - ($min_x2 - $x1) * $sinT + $y1), 0);
227
228
                    for ($x = $min_x2; $x < $max_x2; $x++) {
229
230
                        // Check if we are out of original bounces, if we are
231
                        // use the default color mask
232
                        if ($x2 >= 0 && $x2 < $max_x && $y2 >= 0 && $y2 < $max_y) {
233
                            $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

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

233
                            $c = imagecolorat($img, /** @scrutinizer ignore-type */ $x2, $y2);
Loading history...
234
                        } else {
235
                            $c = $mask;
236
                        }
237
                        imagesetpixel($img2, $x + $x_offset, $y + $y_offset, $c);
238
239
                        // round verboten!
240
                        $x2 += $cosT;
241
                        $y2 -= $sinT;
242
                    }
243
                }
244
                break;
245
        }
246
247
        $this->imageHandle = $img2;
248
249
        return true;
250
    }
251
} // End class ImageGD
252