Completed
Push — master ( c8eead...62889a )
by Michael
04:13
created

include/class_eq_pie.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
    function eq_pie()
15
    {
16
    }
17
18
    /**
19
     * @param $number
20
     *
21
     * @return mixed
22
     */
23
    function GetColor($number)
24
    {
25
        $color = array(
26
            '#ff0000',
27
            '#00ff00',
28
            '#0000ff',
29
            '#ffff00',
30
            '#ff00ff',
31
            '#00ffff',
32
            '#cc0000',
33
            '#00cc00',
34
            '#0000cc',
35
            '#990000',
36
            '#009900',
37
            '#000099',
38
            '#660000',
39
            '#006600',
40
            '#000066',
41
            '#330000',
42
            '#003300',
43
            '#000033'
44
        );
45
46
        return $color[$number];
47
    }
48
49
    /**
50
     * @param $filename
51
     * @param $pieWidth
52
     * @param $pieHeight
53
     * @param $ShadowDistance
54
     * @param $pieBackgroundColor
55
     * @param $EQpieData
56
     * @param $legend
57
     */
58
    function MakePie($filename, $pieWidth, $pieHeight, $ShadowDistance, $pieBackgroundColor, $EQpieData, $legend)
59
    {
60
        if (!function_exists("imagecreatetruecolor")) {
61
            die("Error, GD Library 2 needed.");
62
        }
63
64
        //set some limitations
65
        if ($pieWidth < 100 | $pieWidth > 500) {
66
            $pieWidth = 100;
67
        }
68
        if ($pieHeight < 100 | $pieHeight > 500) {
69
            $pieHeight = 100;
70
        }
71
        if ($ShadowDistance < 1 | $ShadowDistance > 50) {
72
            $ShadowDistance = 10;
73
        }
74
75
        $pieWidth           = $pieWidth * 3;
76
        $pieHeight          = $pieHeight * 3;
77
        $ShadowDistance     = $ShadowDistance * 3;
78
        $pieBackgroundColor = $pieBackgroundColor;
79
80
        $pie = @ImageCreateTrueColor($pieWidth, $pieHeight + $ShadowDistance);
81
82
        $colR  = hexdec(substr($pieBackgroundColor, 1, 2));
83
        $colG  = hexdec(substr($pieBackgroundColor, 3, 2));
84
        $colB  = hexdec(substr($pieBackgroundColor, 5, 2));
85
        $pieBG = ImageColorAllocate($pie, $colR, $colG, $colB);
86
        ImageFill($pie, 0, 0, $pieBG);
87
88
        // get the total value for percentage calculations
89
        $this->total = 0;
90
91
        $maxStringLenght = 0;
92
        foreach ($EQpieData as $i => $value) {
93
            $this->total += $value[1];
94
            if (strlen($value[0]) > $maxStringLenght) {
95
                $maxStringLenght = strlen($value[0]);
96
            }
97
98
        }
99
100
        $pieParts = $i + 1;
0 ignored issues
show
The variable $i seems to be defined by a foreach iteration on line 92. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
101
        reset($EQpieData);
102
        $legendWidth = (($legend > 0) ? ImageFontWidth(2) * ($maxStringLenght + 6) + 40 : 0);
103
104
        // the first pie-part starts with offset in degrees up from horizantal right, looks better this way
105
        $pieStart = 135;
106
107
        foreach ($EQpieData as $i => $value) {
108
109
            // the name  for each part is $value[0]
110
            // the value for each part is $value[1]
111
            // the color for each part is $value[2]
112
113
            $piePart = $value[1];
114 View Code Duplication
            if (isset($this->total) && $this->total > 0) {
115
                $piePart100 = round(($piePart / $this->total * 100), 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
116
            } else {
117
                $piePart100 = 0;
118
            }
119
120
            $piePart360 = $piePart100 * 3.6;                    // in degrees
121
122
            $colR      = hexdec(substr($value[2], 1, 2));
123
            $colG      = hexdec(substr($value[2], 3, 2));
124
            $colB      = hexdec(substr($value[2], 5, 2));
125
            $PartColor = ImageColorAllocate($pie, $colR, $colG, $colB);
126
127
            $ShadowColR = (($colR > 79) ? $colR - 80 : 0);
128
            $ShadowColG = (($colG > 79) ? $colG - 80 : 0);
129
            $ShadowColB = (($colB > 79) ? $colB - 80 : 0);
130
131
            $ShadowColor = ImageColorAllocate($pie, $ShadowColR, $ShadowColG, $ShadowColB);
132
133
            //Here we create the shadow down-worths
134
            for ($i = 0; $i < $ShadowDistance; ++$i) {
135
                ImageFilledArc($pie, $pieWidth / 2, ($pieHeight / 2 + $i), $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $ShadowColor, IMG_ARC_NOFILL);
136
            }
137
138
            $pieStart = $pieStart + $piePart360;
139
140
        }
141
        reset($EQpieData);
142
143
        $pieStart = 135;
144
145
        foreach ($EQpieData as $i => $value) {
146
147
            $piePart = $value[1];
148 View Code Duplication
            if (isset($this->total) && $this->total > 0) {
149
                $piePart100 = round(($piePart / $this->total * 100), 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
150
            } else {
151
                $piePart100 = 0;
152
            }
153
            $piePart360 = $piePart100 * 3.6;                    // in degrees
154
155
            $colR      = hexdec(substr($value[2], 1, 2));
156
            $colG      = hexdec(substr($value[2], 3, 2));
157
            $colB      = hexdec(substr($value[2], 5, 2));
158
            $PartColor = ImageColorAllocate($pie, $colR, $colG, $colB);
159
160
            //Here we create the real pie chart
161
            ImageFilledArc($pie, $pieWidth / 2, $pieHeight / 2, $pieWidth - 20, $pieHeight - 20, round($pieStart), round($pieStart + $piePart360), $PartColor, IMG_ARC_PIE);
162
163
            $pieStart = $pieStart + $piePart360;
164
165
        }
166
        reset($EQpieData);
167
168
        // create final pie picture with proper background color
169
        $finalPie = ImageCreateTrueColor($pieWidth / 3 + $legendWidth, ($pieHeight + $ShadowDistance) / 3);
170
        ImageFill($finalPie, 0, 0, $pieBG);
171
172
        // resample with pieGraph inside (3x smaller)
173
        ImageCopyResampled($finalPie, $pie, 0, 0, 0, 0, $pieWidth / 3, ($pieHeight + $ShadowDistance) / 3, $pieWidth, ($pieHeight + $ShadowDistance));
174
175
        // Create the ledgend ...
176
        if ($legendWidth > 0) {
177
            // Legend Box
178
            $leg_width   = $legendWidth - 10;
179
            $leg_height  = $pieParts * (ImageFontHeight(2) + 2) + 2;
180
            $legendImage = ImageCreateTrueColor($leg_width, $leg_height);
181
            //ImageFill($legendImage, 0, 0, $pieBG);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
182
183
            $borderColor = ImageColorAllocate($pie, '155', '155', '155');
184
            $boxColor    = ImageColorAllocate($pie, '255', '255', '255');
185
            $textColor   = ImageColorAllocate($pie, '55', '55', '55');
186
187
            ImageFilledRectangle($legendImage, 0, 0, $leg_width, $leg_height, $boxColor);
188
            ImageRectangle($legendImage, 0, 0, $leg_width - 1, $leg_height - 1, $borderColor);
189
190
            $box_width  = ImageFontHeight(2) - 5;
191
            $box_height = ImageFontHeight(2) - 5;
192
            $yOffset    = 2;
193
194
            foreach ($EQpieData as $i => $value) {
195
196
                $piePart = $value[1];
197 View Code Duplication
                if (isset($this->total) && $this->total > 0) {
198
                    $piePart100 = round(($piePart / $this->total * 100), 2);  // value in percentage, the rounding and * 100 for extra accuracy for pie w/o gaps
199
                } else {
200
                    $piePart100 = 0;
201
                }
202
                $colR      = hexdec(substr($value[2], 1, 2));
203
                $colG      = hexdec(substr($value[2], 3, 2));
204
                $colB      = hexdec(substr($value[2], 5, 2));
205
                $PartColor = ImageColorAllocate($legendImage, $colR, $colG, $colB);
206
207
                ImageFilledRectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $PartColor);
208
                ImageRectangle($legendImage, 5, $yOffset + 2, 5 + $box_width, $yOffset + $box_height + 2, $borderColor);
209
210
                $text = $value[0] . " " . $piePart100 . "%";
211
                ImageString($legendImage, 2, '20', $yOffset, $text, $textColor);
212
                $yOffset = $yOffset + 15;
213
214
            }
215
216
            reset($EQpieData); // reset pointer in array to first
217
218
            ImageCopyResampled($finalPie, $legendImage, $pieWidth / 3, 10, 0, 0, $leg_width, $leg_height, $leg_width, $leg_height);
219
            ImageDestroy($legendImage);
220
221
        }
222
        header('Content-type: image/png');
223
        imagepng($finalPie, $filename);
224
        ImageDestroy($pie);
225
        ImageDestroy($finalPie);
226
227
        return;
228
    }
229
230
}
231