Passed
Push — master ( 48d769...5ccf6e )
by Michael
07:14
created

eq_pie::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
// eq_pie written by ellardus (C) 2005
4
// for more info look at www.eq-home.com
5
// or email at [email protected]
6
// Feel free to use it, a reference to me would be nice.
7
// Thank you and good luck!
8
9
/**
10
 * Class eq_pie
11
 */
12
class eq_pie
13
{
14
    /**
15
     * eq_pie constructor.
16
     */
17
    public function __construct()
18
    {
19
    }
20
21
    /**
22
     * @param $number
23
     *
24
     * @return mixed
25
     */
26
    public function getColor($number)
27
    {
28
        $color = [
29
            '#ff0000',
30
            '#00ff00',
31
            '#0000ff',
32
            '#ffff00',
33
            '#ff00ff',
34
            '#00ffff',
35
            '#cc0000',
36
            '#00cc00',
37
            '#0000cc',
38
            '#990000',
39
            '#009900',
40
            '#000099',
41
            '#660000',
42
            '#006600',
43
            '#000066',
44
            '#330000',
45
            '#003300',
46
            '#000033'
47
        ];
48
49
        return $color[$number];
50
    }
51
52
    /**
53
     * @param $filename
54
     * @param $pieWidth
55
     * @param $pieHeight
56
     * @param $ShadowDistance
57
     * @param $pieBackgroundColor
58
     * @param $EQpieData
59
     * @param $legend
60
     */
61
    public function MakePie(
62
        $filename,
63
        $pieWidth,
64
        $pieHeight,
65
        $ShadowDistance,
66
        $pieBackgroundColor,
67
        $EQpieData,
68
        $legend
69
    ) {
70
        if (!function_exists('imagecreatetruecolor')) {
71
            die('Error, GD Library 2 needed.');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

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

Loading history...
72
        }
73
74
        //set some limitations
75
        if ($pieWidth < 100 | $pieWidth > 500) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
76
            $pieWidth = 100;
77
        }
78
        if ($pieHeight < 100 | $pieHeight > 500) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
79
            $pieHeight = 100;
80
        }
81
        if ($ShadowDistance < 1 | $ShadowDistance > 50) {
0 ignored issues
show
Bug introduced by
Are you sure you want to use the bitwise | or did you mean ||?
Loading history...
82
            $ShadowDistance = 10;
83
        }
84
85
        $pieWidth           *= 3;
86
        $pieHeight          *= 3;
87
        $ShadowDistance     *= 3;
88
        $pieBackgroundColor = $pieBackgroundColor;
89
90
        $pie = @imagecreatetruecolor($pieWidth, $pieHeight + $ShadowDistance);
91
92
        $colR  = hexdec(substr($pieBackgroundColor, 1, 2));
93
        $colG  = hexdec(substr($pieBackgroundColor, 3, 2));
94
        $colB  = hexdec(substr($pieBackgroundColor, 5, 2));
95
        $pieBG = imagefilledarc($pie, $colR, $colG, $colB);
0 ignored issues
show
Bug introduced by
The call to imagefilledarc() has too few arguments starting with height. ( Ignorable by Annotation )

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

95
        $pieBG = /** @scrutinizer ignore-call */ imagefilledarc($pie, $colR, $colG, $colB);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
It seems like $colB can also be of type double; however, parameter $width of imagefilledarc() 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

95
        $pieBG = imagefilledarc($pie, $colR, $colG, /** @scrutinizer ignore-type */ $colB);
Loading history...
Bug introduced by
It seems like $colG can also be of type double; however, parameter $cy of imagefilledarc() 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

95
        $pieBG = imagefilledarc($pie, $colR, /** @scrutinizer ignore-type */ $colG, $colB);
Loading history...
Bug introduced by
It seems like $colR can also be of type double; however, parameter $cx of imagefilledarc() 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

95
        $pieBG = imagefilledarc($pie, /** @scrutinizer ignore-type */ $colR, $colG, $colB);
Loading history...
96
        imagefill($pie, 0, 0, $pieBG);
0 ignored issues
show
Bug introduced by
$pieBG of type boolean is incompatible with the type integer expected by parameter $color of imagefill(). ( Ignorable by Annotation )

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

96
        imagefill($pie, 0, 0, /** @scrutinizer ignore-type */ $pieBG);
Loading history...
97
98
        // get the total value for percentage calculations
99
        $this->total = 0;
0 ignored issues
show
Bug Best Practice introduced by
The property total does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
100
101
        $maxStringLenght = 0;
102
        foreach ($EQpieData as $i => $value) {
103
            $this->total += $value[1];
104
            if (strlen($value[0]) > $maxStringLenght) {
105
                $maxStringLenght = strlen($value[0]);
106
            }
107
        }
108
109
        $pieParts = $i + 1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $i seems to be defined by a foreach iteration on line 102. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
110
        reset($EQpieData);
111
        $legendWidth = (($legend > 0) ? imagefontwidth(2) * ($maxStringLenght + 6) + 40 : 0);
112
113
        // the first pie-part starts with offset in degrees up from horizantal right, looks better this way
114
        $pieStart = 135;
115
116
        foreach ($EQpieData as $i => $value) {
117
118
            // the name  for each part is $value[0]
119
            // the value for each part is $value[1]
120
            // the color for each part is $value[2]
121
122
            $piePart = $value[1];
123
            if (isset($this->total) && $this->total > 0) {
124
                $piePart100 = round($piePart / $this->total * 100, 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
125
            } else {
126
                $piePart100 = 0;
127
            }
128
129
            $piePart360 = $piePart100 * 3.6;                    // in degrees
130
131
            $colR      = hexdec(substr($value[2], 1, 2));
132
            $colG      = hexdec(substr($value[2], 3, 2));
133
            $colB      = hexdec(substr($value[2], 5, 2));
134
            $PartColor = imagefilledarc($pie, $colR, $colG, $colB);
0 ignored issues
show
Unused Code introduced by
The assignment to $PartColor is dead and can be removed.
Loading history...
135
136
            $ShadowColR = (($colR > 79) ? $colR - 80 : 0);
137
            $ShadowColG = (($colG > 79) ? $colG - 80 : 0);
138
            $ShadowColB = (($colB > 79) ? $colB - 80 : 0);
139
140
            $ShadowColor = imagefilledarc($pie, $ShadowColR, $ShadowColG, $ShadowColB);
141
142
            //Here we create the shadow down-worths
143
            for ($i = 0; $i < $ShadowDistance; ++$i) {
144
                imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $ShadowColor, IMG_ARC_NOFILL);
0 ignored issues
show
Bug introduced by
$ShadowColor of type boolean is incompatible with the type integer expected by parameter $color of imagefilledarc(). ( Ignorable by Annotation )

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

144
                imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), /** @scrutinizer ignore-type */ $ShadowColor, IMG_ARC_NOFILL);
Loading history...
Bug introduced by
round($pieStart + $piePart360) of type double is incompatible with the type integer expected by parameter $end of imagefilledarc(). ( Ignorable by Annotation )

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

144
                imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, round($pieStart), /** @scrutinizer ignore-type */ round($pieStart + $piePart360), $ShadowColor, IMG_ARC_NOFILL);
Loading history...
Bug introduced by
round($pieStart) of type double is incompatible with the type integer expected by parameter $start of imagefilledarc(). ( Ignorable by Annotation )

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

144
                imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2 + $i, $pieWidth - 20, $pieHeight - 20, /** @scrutinizer ignore-type */ round($pieStart), round($pieStart + $piePart360), $ShadowColor, IMG_ARC_NOFILL);
Loading history...
145
            }
146
147
            $pieStart += $piePart360;
148
        }
149
        reset($EQpieData);
150
151
        $pieStart = 135;
152
153
        foreach ($EQpieData as $i => $value) {
154
            $piePart = $value[1];
155
            if (isset($this->total) && $this->total > 0) {
156
                $piePart100 = round($piePart / $this->total * 100, 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
157
            } else {
158
                $piePart100 = 0;
159
            }
160
            $piePart360 = $piePart100 * 3.6;                    // in degrees
161
162
            $colR      = hexdec(substr($value[2], 1, 2));
163
            $colG      = hexdec(substr($value[2], 3, 2));
164
            $colB      = hexdec(substr($value[2], 5, 2));
165
            $PartColor = imagefilledarc($pie, $colR, $colG, $colB);
166
167
            //Here we create the real pie chart
168
            imagefilledarc($pie, $pieWidth / 2, $pieHeight / 2, $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $PartColor, IMG_ARC_PIE);
169
170
            $pieStart += $piePart360;
171
        }
172
        reset($EQpieData);
173
174
        // create final pie picture with proper background color
175
        $finalPie = imagecreatetruecolor($pieWidth / 3 + $legendWidth, ($pieHeight + $ShadowDistance) / 3);
176
        imagefill($finalPie, 0, 0, $pieBG);
177
178
        // resample with pieGraph inside (3x smaller)
179
        imagecopyresampled($finalPie, $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, $pieHeight + $ShadowDistance);
180
181
        // Create the ledgend ...
182
        if ($legendWidth > 0) {
183
            // Legend Box
184
            $leg_width   = $legendWidth - 10;
185
            $leg_height  = $pieParts * (imagefontheight(2) + 2) + 2;
186
            $legendImage = imagecreatetruecolor($leg_width, $leg_height);
187
            //ImageFill($legendImage, 0, 0, $pieBG);
188
189
            $borderColor = imagefilledarc($pie, '155', '155', '155');
190
            $boxColor    = imagefilledarc($pie, '255', '255', '255');
191
            $textColor   = imagefilledarc($pie, '55', '55', '55');
192
193
            imagefilledrectangle($legendImage, 0, 0, $leg_width, $leg_height, $boxColor);
0 ignored issues
show
Bug introduced by
$boxColor of type boolean is incompatible with the type integer expected by parameter $color 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

193
            imagefilledrectangle($legendImage, 0, 0, $leg_width, $leg_height, /** @scrutinizer ignore-type */ $boxColor);
Loading history...
194
            imagerectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, $borderColor);
0 ignored issues
show
Bug introduced by
$borderColor of type boolean is incompatible with the type integer expected by parameter $color 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

194
            imagerectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, /** @scrutinizer ignore-type */ $borderColor);
Loading history...
195
196
            $box_width  = imagefontwidth(2) - 5;
197
            $box_height = imagefontheight(2) - 5;
198
            $yOffset    = 2;
199
200
            foreach ($EQpieData as $i => $value) {
201
                $piePart = $value[1];
202
                if (isset($this->total) && $this->total > 0) {
203
                    $piePart100 = round($piePart / $this->total * 100, 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
204
                } else {
205
                    $piePart100 = 0;
206
                }
207
                $colR      = hexdec(substr($value[2], 1, 2));
208
                $colG      = hexdec(substr($value[2], 3, 2));
209
                $colB      = hexdec(substr($value[2], 5, 2));
210
                $PartColor = imagefilledarc($legendImage, $colR, $colG, $colB);
211
212
                imagefilledrectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $PartColor);
213
                imagerectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $borderColor);
214
215
                $text = $value[0] . ' ' . $piePart100 . '%';
216
                imagestring($legendImage, 2, '20', $yOffset, $text, $textColor);
0 ignored issues
show
Bug introduced by
$textColor of type boolean is incompatible with the type integer expected by parameter $color of imagestring(). ( Ignorable by Annotation )

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

216
                imagestring($legendImage, 2, '20', $yOffset, $text, /** @scrutinizer ignore-type */ $textColor);
Loading history...
217
                $yOffset += 15;
218
            }
219
220
            reset($EQpieData); // reset pointer in array to first
221
222
            imagecopyresampled($finalPie, $legendImage, $pieWidth / 3, 10, 0, 0, $leg_width, $leg_height, $leg_width, $leg_height);
223
            imagedestroy($legendImage);
224
        }
225
        header('Content-type: image/png');
226
        imagepng($finalPie, $filename);
227
        imagedestroy($pie);
228
        imagedestroy($finalPie);
229
230
        return;
231
    }
232
}
233