ImgTrans::_Skew3D()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 8
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 12
rs 10
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * JPGraph v4.0.3
5
 */
6
7
namespace Amenadiel\JpGraph\Image;
8
9
use Amenadiel\JpGraph\Util;
10
11
/**
12
 * File:        JPGRAPH_IMGTRANS.PHP
13
 * // Description: Extension for JpGraph to do some simple img transformations
14
 * // Created:     2003-09-06
15
 * // Ver:         $Id: jpgraph_imgtrans.php 1106 2009-02-22 20:16:35Z ljp $
16
 * //
17
 * // Copyright (c) Asial Corporation. All rights reserved.
18
 */
19
20
/**
21
 * @class ImgTrans
22
 * // Perform some simple image transformations.
23
 */
24
class ImgTrans
25
{
26
    private $gdImg;
27
28
    public function __construct($aGdImg)
29
    {
30
        // Constructor
31
        $this->gdImg = $aGdImg;
32
    }
33
34
    // --------------------------------------------------------------------
35
    // _TransVert3D() and _TransHor3D() are helper methods to
36
    // Skew3D().
37
    // --------------------------------------------------------------------
38
    public function _TransVert3D($aGdImg, $aHorizon = 100, $aSkewDist = 120, $aDir = SKEW3D_DOWN, $aMinSize = true, $aFillColor = '#FFFFFF', $aQuality = false, $aBorder = false, $aHorizonPos = 0.5)
39
    {
40
        // Parameter check
41
        if ($aHorizonPos < 0 || $aHorizonPos > 1.0) {
42
            Util\JpGraphError::RaiseL(9001);
43
            //("Value for image transformation out of bounds.\nVanishing point on horizon must be specified as a value between 0 and 1.");
44
        }
45
46
        $w = imagesx($aGdImg);
47
        $h = imagesy($aGdImg);
48
49
        // Create new image
50
        $ww = $w;
51
        if ($aMinSize) {
52
            $hh = ceil($h * $aHorizon / ($aSkewDist + $h));
53
        } else {
54
            $hh = $h;
55
        }
56
57
        $newgdh    = imagecreatetruecolor($ww, $hh);
0 ignored issues
show
Bug introduced by
It seems like $hh can also be of type double; however, parameter $height of imagecreatetruecolor() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

57
        $newgdh    = imagecreatetruecolor($ww, /** @scrutinizer ignore-type */ $hh);
Loading history...
58
        $crgb      = new RGB($newgdh);
59
        $fillColor = $crgb->Allocate($aFillColor);
60
        imagefilledrectangle($newgdh, 0, 0, $ww - 1, $hh - 1, $fillColor);
0 ignored issues
show
Bug introduced by
$hh - 1 of type double is incompatible with the type integer expected by parameter $y2 of imagefilledrectangle(). ( Ignorable by Annotation )

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

60
        imagefilledrectangle($newgdh, 0, 0, $ww - 1, /** @scrutinizer ignore-type */ $hh - 1, $fillColor);
Loading history...
61
62
        if ($aBorder) {
63
            $colidx = $crgb->Allocate($aBorder);
64
            imagerectangle($newgdh, 0, 0, $ww - 1, $hh - 1, $colidx);
0 ignored issues
show
Bug introduced by
$hh - 1 of type double is incompatible with the type integer expected by parameter $y2 of imagerectangle(). ( Ignorable by Annotation )

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

64
            imagerectangle($newgdh, 0, 0, $ww - 1, /** @scrutinizer ignore-type */ $hh - 1, $colidx);
Loading history...
65
        }
66
67
        $mid = round($w * $aHorizonPos);
68
69
        $last = $h;
70
        for ($y = 0; $y < $h; ++$y) {
71
            $yp = $h - $y - 1;
72
            $yt = floor($yp * $aHorizon / ($aSkewDist + $yp));
73
74
            if (!$aQuality) {
75
                if ($last <= $yt) {
76
                    continue;
77
                }
78
79
                $last = $yt;
80
            }
81
82
            for ($x = 0; $x < $w; ++$x) {
83
                $xt = ($x - $mid) * $aSkewDist / ($aSkewDist + $yp);
84
                if ($aDir == SKEW3D_UP) {
85
                    $rgb = imagecolorat($aGdImg, $x, $h - $y - 1);
86
                } else {
87
                    $rgb = imagecolorat($aGdImg, $x, $y);
88
                }
89
90
                $r      = ($rgb >> 16) & 0xFF;
91
                $g      = ($rgb >> 8) & 0xFF;
92
                $b      = $rgb & 0xFF;
93
                $colidx = imagecolorallocate($newgdh, $r, $g, $b);
94
                $xt     = round($xt + $mid);
95
                if ($aDir == SKEW3D_UP) {
96
                    $syt = $yt;
97
                } else {
98
                    $syt = $hh - $yt - 1;
99
                }
100
101
                if (!empty($set[$yt])) {
102
                    $nrgb   = imagecolorat($newgdh, $xt, $syt);
0 ignored issues
show
Bug introduced by
$syt 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

102
                    $nrgb   = imagecolorat($newgdh, $xt, /** @scrutinizer ignore-type */ $syt);
Loading history...
Bug introduced by
$xt 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

102
                    $nrgb   = imagecolorat($newgdh, /** @scrutinizer ignore-type */ $xt, $syt);
Loading history...
103
                    $nr     = ($nrgb >> 16) & 0xFF;
104
                    $ng     = ($nrgb >> 8) & 0xFF;
105
                    $nb     = $nrgb & 0xFF;
106
                    $colidx = imagecolorallocate(
107
                        $newgdh,
108
                        floor(($r + $nr) / 2),
0 ignored issues
show
Bug introduced by
floor($r + $nr / 2) of type double is incompatible with the type integer expected by parameter $red of imagecolorallocate(). ( Ignorable by Annotation )

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

108
                        /** @scrutinizer ignore-type */ floor(($r + $nr) / 2),
Loading history...
109
                        floor(($g + $ng) / 2),
0 ignored issues
show
Bug introduced by
floor($g + $ng / 2) of type double is incompatible with the type integer expected by parameter $green of imagecolorallocate(). ( Ignorable by Annotation )

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

109
                        /** @scrutinizer ignore-type */ floor(($g + $ng) / 2),
Loading history...
110
                        floor(($b + $nb) / 2)
0 ignored issues
show
Bug introduced by
floor($b + $nb / 2) of type double is incompatible with the type integer expected by parameter $blue of imagecolorallocate(). ( Ignorable by Annotation )

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

110
                        /** @scrutinizer ignore-type */ floor(($b + $nb) / 2)
Loading history...
111
                    );
112
                }
113
114
                imagesetpixel($newgdh, $xt, $syt, $colidx);
0 ignored issues
show
Bug introduced by
$xt of type double is incompatible with the type integer expected by parameter $x 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

114
                imagesetpixel($newgdh, /** @scrutinizer ignore-type */ $xt, $syt, $colidx);
Loading history...
Bug introduced by
$syt of type double is incompatible with the type integer expected by parameter $y 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

114
                imagesetpixel($newgdh, $xt, /** @scrutinizer ignore-type */ $syt, $colidx);
Loading history...
115
            }
116
117
            $set[$yt] = true;
118
        }
119
120
        return $newgdh;
121
    }
122
123
    // --------------------------------------------------------------------
124
    // _TransVert3D() and _TransHor3D() are helper methods to
125
    // Skew3D().
126
    // --------------------------------------------------------------------
127
    public function _TransHor3D($aGdImg, $aHorizon = 100, $aSkewDist = 120, $aDir = SKEW3D_LEFT, $aMinSize = true, $aFillColor = '#FFFFFF', $aQuality = false, $aBorder = false, $aHorizonPos = 0.5)
128
    {
129
        $w = imagesx($aGdImg);
130
        $h = imagesy($aGdImg);
131
132
        // Create new image
133
        $hh = $h;
134
        if ($aMinSize) {
135
            $ww = ceil($w * $aHorizon / ($aSkewDist + $w));
136
        } else {
137
            $ww = $w;
138
        }
139
140
        $newgdh    = imagecreatetruecolor($ww, $hh);
0 ignored issues
show
Bug introduced by
It seems like $ww can also be of type double; however, parameter $width of imagecreatetruecolor() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

140
        $newgdh    = imagecreatetruecolor(/** @scrutinizer ignore-type */ $ww, $hh);
Loading history...
141
        $crgb      = new RGB($newgdh);
142
        $fillColor = $crgb->Allocate($aFillColor);
143
        imagefilledrectangle($newgdh, 0, 0, $ww - 1, $hh - 1, $fillColor);
0 ignored issues
show
Bug introduced by
$ww - 1 of type double is incompatible with the type integer expected by parameter $x2 of imagefilledrectangle(). ( Ignorable by Annotation )

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

143
        imagefilledrectangle($newgdh, 0, 0, /** @scrutinizer ignore-type */ $ww - 1, $hh - 1, $fillColor);
Loading history...
144
145
        if ($aBorder) {
146
            $colidx = $crgb->Allocate($aBorder);
147
            imagerectangle($newgdh, 0, 0, $ww - 1, $hh - 1, $colidx);
0 ignored issues
show
Bug introduced by
$ww - 1 of type double is incompatible with the type integer expected by parameter $x2 of imagerectangle(). ( Ignorable by Annotation )

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

147
            imagerectangle($newgdh, 0, 0, /** @scrutinizer ignore-type */ $ww - 1, $hh - 1, $colidx);
Loading history...
148
        }
149
150
        $mid = round($h * $aHorizonPos);
151
152
        $last = -1;
153
        for ($x = 0; $x < $w - 1; ++$x) {
154
            $xt = floor($x * $aHorizon / ($aSkewDist + $x));
155
            if (!$aQuality) {
156
                if ($last >= $xt) {
157
                    continue;
158
                }
159
160
                $last = $xt;
161
            }
162
163
            for ($y = 0; $y < $h; ++$y) {
164
                $yp = $h - $y - 1;
165
                $yt = ($yp - $mid) * $aSkewDist / ($aSkewDist + $x);
166
167
                if ($aDir == SKEW3D_RIGHT) {
168
                    $rgb = imagecolorat($aGdImg, $w - $x - 1, $y);
169
                } else {
170
                    $rgb = imagecolorat($aGdImg, $x, $y);
171
                }
172
173
                $r      = ($rgb >> 16) & 0xFF;
174
                $g      = ($rgb >> 8) & 0xFF;
175
                $b      = $rgb & 0xFF;
176
                $colidx = imagecolorallocate($newgdh, $r, $g, $b);
177
                $yt     = floor($hh - $yt - $mid - 1);
178
                if ($aDir == SKEW3D_RIGHT) {
179
                    $sxt = $ww - $xt - 1;
180
                } else {
181
                    $sxt = $xt;
182
                }
183
184
                if (!empty($set[$xt])) {
185
                    $nrgb   = imagecolorat($newgdh, $sxt, $yt);
0 ignored issues
show
Bug introduced by
$yt 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

185
                    $nrgb   = imagecolorat($newgdh, $sxt, /** @scrutinizer ignore-type */ $yt);
Loading history...
Bug introduced by
$sxt 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

185
                    $nrgb   = imagecolorat($newgdh, /** @scrutinizer ignore-type */ $sxt, $yt);
Loading history...
186
                    $nr     = ($nrgb >> 16) & 0xFF;
187
                    $ng     = ($nrgb >> 8) & 0xFF;
188
                    $nb     = $nrgb & 0xFF;
189
                    $colidx = imagecolorallocate(
190
                        $newgdh,
191
                        floor(($r + $nr) / 2),
0 ignored issues
show
Bug introduced by
floor($r + $nr / 2) of type double is incompatible with the type integer expected by parameter $red of imagecolorallocate(). ( Ignorable by Annotation )

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

191
                        /** @scrutinizer ignore-type */ floor(($r + $nr) / 2),
Loading history...
192
                        floor(($g + $ng) / 2),
0 ignored issues
show
Bug introduced by
floor($g + $ng / 2) of type double is incompatible with the type integer expected by parameter $green of imagecolorallocate(). ( Ignorable by Annotation )

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

192
                        /** @scrutinizer ignore-type */ floor(($g + $ng) / 2),
Loading history...
193
                        floor(($b + $nb) / 2)
0 ignored issues
show
Bug introduced by
floor($b + $nb / 2) of type double is incompatible with the type integer expected by parameter $blue of imagecolorallocate(). ( Ignorable by Annotation )

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

193
                        /** @scrutinizer ignore-type */ floor(($b + $nb) / 2)
Loading history...
194
                    );
195
                }
196
                imagesetpixel($newgdh, $sxt, $yt, $colidx);
0 ignored issues
show
Bug introduced by
$sxt of type double is incompatible with the type integer expected by parameter $x 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

196
                imagesetpixel($newgdh, /** @scrutinizer ignore-type */ $sxt, $yt, $colidx);
Loading history...
Bug introduced by
$yt of type double is incompatible with the type integer expected by parameter $y 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

196
                imagesetpixel($newgdh, $sxt, /** @scrutinizer ignore-type */ $yt, $colidx);
Loading history...
197
            }
198
199
            $set[$xt] = true;
200
        }
201
202
        return $newgdh;
203
    }
204
205
    // --------------------------------------------------------------------
206
    // Skew image for the apperance of a 3D effect
207
    // This transforms an image into a 3D-skewed version
208
    // of the image. The transformation is specified by giving the height
209
    // of the artificial horizon and specifying a "skew" factor which
210
    // is the distance on the horizon line between the point of
211
    // convergence and perspective line.
212
    //
213
    // The function returns the GD handle of the transformed image
214
    // leaving the original image untouched.
215
    //
216
    // Parameters:
217
    // * $aGdImg, GD handle to the image to be transformed
218
    // * $aHorizon, Distance to the horizon
219
    // * $aSkewDist, Distance from the horizon point of convergence
220
    //   on the horizon line to the perspective points. A larger
221
    //   value will fore-shorten the image more
222
    // * $aDir, parameter specifies type of convergence. This of this
223
    //   as the walls in a room you are looking at. This specifies if the
224
    //   image should be applied on the left,right,top or bottom walls.
225
    // * $aMinSize, true=make the new image just as big as needed,
226
    //   false = keep the image the same size as the original image
227
    // * $aFillColor, Background fill color in the image
228
    // * $aHiQuality, true=performa some interpolation that improves
229
    //   the image quality but at the expense of performace. Enabling
230
    //   high quality will have a dramatic effect on the time it takes
231
    //   to transform an image.
232
    // * $aBorder, if set to anything besides false this will draw a
233
    //   a border of the speciied color around the image
234
    // --------------------------------------------------------------------
235
    public function Skew3D($aHorizon = 120, $aSkewDist = 150, $aDir = SKEW3D_DOWN, $aHiQuality = false, $aMinSize = true, $aFillColor = '#FFFFFF', $aBorder = false)
236
    {
237
        return $this->_Skew3D(
238
            $this->gdImg,
239
            $aHorizon,
240
            $aSkewDist,
241
            $aDir,
242
            $aHiQuality,
243
            $aMinSize,
244
            $aFillColor,
245
            $aBorder
246
        );
247
    }
248
249
    public function _Skew3D($aGdImg, $aHorizon = 120, $aSkewDist = 150, $aDir = SKEW3D_DOWN, $aHiQuality = false, $aMinSize = true, $aFillColor = '#FFFFFF', $aBorder = false)
250
    {
251
        if ($aDir == SKEW3D_DOWN || $aDir == SKEW3D_UP) {
252
            return $this->_TransVert3D($aGdImg, $aHorizon, $aSkewDist, $aDir, $aMinSize, $aFillColor, $aHiQuality, $aBorder);
253
        }
254
255
        return $this->_TransHor3D($aGdImg, $aHorizon, $aSkewDist, $aDir, $aMinSize, $aFillColor, $aHiQuality, $aBorder);
256
    }
257
}
258