Merge branch 'maint/7.0'
[ninja.git] / application / vendor / mfchart / Utilities.php
blobf944565276db8ed2a5c34975710a0cbe6a21f834
1 <?php
3 require('imageSmoothArc.php');
6 // for text aligning -- imagestringbox
7 define("ALIGN_LEFT", "left");
8 define("ALIGN_CENTER", "center");
9 define("ALIGN_RIGHT", "right");
10 define("VALIGN_TOP", "top");
11 define("VALIGN_MIDDLE", "middle");
12 define("VALIGN_BOTTOM", "bottom");
15 class Utilities {
17 public static function hex2rgb($color)
19 if ($color[0] == '#')
20 $color = substr($color, 1);
22 if (strlen($color) == 6)
23 list($r, $g, $b) = array($color[0].$color[1], $color[2].$color[3], $color[4].$color[5]);
24 elseif (strlen($color) == 3)
25 list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
26 else
27 return false;
29 $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
31 return array($r, $g, $b);
34 public static function rgb2hex($r, $g=-1, $b=-1)
36 if (is_array($r) && sizeof($r) == 3)
37 list($r, $g, $b) = $r;
39 $r = intval($r); $g = intval($g);
40 $b = intval($b);
42 $r = dechex($r<0?0:($r>255?255:$r));
43 $g = dechex($g<0?0:($g>255?255:$g));
44 $b = dechex($b<0?0:($b>255?255:$b));
46 $color = (strlen($r) < 2?'0':'').$r;
47 $color .= (strlen($g) < 2?'0':'').$g;
48 $color .= (strlen($b) < 2?'0':'').$b;
50 return '#'.$color;
53 // for true type fonts! not for imagestring
54 // supports only one line text and vertical direction
55 public static function imagestringbox(&$image, $font, $font_size, $left, $top, $right, $bottom, $align, $valign, $leading, $text, $color, $vertical=FALSE)
57 $box_points = imagettfbbox($font_size, 0, $font, $text);
58 // $box_points[3] = $box_points[3]==-1 ? 0 : $box_points[3];
59 // $box_points[4] = $box_points[4]==-1 ? 0 : $box_points[4];
60 // $box_points[5] = $box_points[5]==-1 ? 0 : $box_points[5];
61 $box_points[6] = $box_points[6]==-1 ? 0 : $box_points[6];
62 $textwidth = $box_points[4]-$box_points[6];
63 $textheight = $font_size;
65 if ($vertical)
66 list($textwidth, $textheight) = array($textheight, $textwidth);
68 // Get size of box
69 $height = $bottom - $top;
70 $width = $right - $left;
72 // // Break the text into lines, and into an array
73 // if (!$vertical)
74 // {
75 // $lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
76 // $lines = explode("\n", $lines);
77 // }
78 // else
79 // {
80 // $lines = wordwrap($text, floor($height / imagefontwidth($font)), "\n", true);
81 // $lines = explode("\n", $lines);
82 // }
84 $lines = array($text);
86 // Other important numbers
87 if (!$vertical)
89 $line_height = $textheight + $leading;
90 $line_count = floor($height / $line_height);
92 else
94 $line_height = $textwidth + $leading;
95 $line_count = floor($width / $line_height);
97 $line_count = ($line_count > count($lines)) ? (count($lines)) : ($line_count);
99 // Loop through lines
100 for ($i = 0; $i < $line_count; $i++)
102 // Vertical Align
103 switch($valign)
105 case VALIGN_TOP: // Top
106 $y = $top + (($i+1) * $line_height);
107 break;
108 case VALIGN_MIDDLE: // Middle
109 $y = $top + (($height - ($line_count * $line_height)) / 2) + (($i+1) * $line_height);
110 break;
111 case VALIGN_BOTTOM: // Bottom
112 $y = ($top + $height) - ($line_count * $line_height) + (($i+1) * $line_height);
113 break;
114 default:
115 return false;
118 // Horizontal Align
119 $line_width = $textwidth;
120 switch($align)
122 case ALIGN_LEFT: // Left
123 $x = ($vertical) ? $left + $textwidth : $left;
124 break;
125 case ALIGN_CENTER: // Center
126 $x = ($vertical) ? $left + $width/2 + $textwidth/2 : $left + (($width - $line_width) / 2);
127 break;
128 case ALIGN_RIGHT: // Right
129 $x = ($vertical) ? $right : $left + ($width - $line_width);
130 break;
131 default:
132 return false;
135 // Draw
136 if ($vertical)
137 imagettftext($image, $font_size, 90, $x, $y, $color, $font, $lines[$i]);
138 else
139 imagettftext($image, $font_size, 0, $x, $y, $color, $font, $lines[$i]);
142 return true;
145 // draw rounded rectangle
146 public static function imagefillroundedrect($im, $x, $y, $cx, $cy, $rad, $col)
148 // Draw the middle cross shape of the rectangle
149 imagefilledrectangle($im,$x,$y+$rad,$cx,$cy-$rad,$col);
150 imagefilledrectangle($im,$x+$rad,$y,$cx-$rad,$cy,$col);
152 $dia = $rad*2;
154 // Now fill in the rounded corners
155 imagefilledellipse($im, $x+$rad, $y+$rad, $rad*2, $dia, $col);
156 imagefilledellipse($im, $x+$rad, $cy-$rad, $rad*2, $dia, $col);
157 imagefilledellipse($im, $cx-$rad, $cy-$rad, $rad*2, $dia, $col);
158 imagefilledellipse($im, $cx-$rad, $y+$rad, $rad*2, $dia, $col);
161 // draw the background image
162 public static function imagefilledrectanglestyled($image, $x1, $y1, $x2, $y2, $bg_style, $color)
164 $width = abs($x2-$x1);
165 $height = abs($y2-$y1);
167 switch ($bg_style)
169 case 'dotted':
170 for ($i=0; $i<$width; $i=$i+5)
171 for ($j=0; $j<$height; $j=$j+5)
172 imagefilledrectangle($image, $x1+$i, $y1+$j, $x1+$i, $y1+$j, $color); // draw shade of bar
173 break;
174 case 'lines':
175 for ($j=0; $j<$height; $j=$j+1)
177 for ($i=$width-($j%5); $i>0; $i=$i-5)
178 imagefilledrectangle($image, $x1+$i, $y1+$j, $x1+$i, $y1+$j, $color); // draw shade of bar
180 break;
185 * function imageSmoothAlphaLine() - version 1.0
186 * Draws a smooth line with alpha-functionality
188 * @param ident the image to draw on
189 * @param integer x1
190 * @param integer y1
191 * @param integer x2
192 * @param integer y2
193 * @param integer red (0 to 255)
194 * @param integer green (0 to 255)
195 * @param integer blue (0 to 255)
196 * @param integer alpha (0 to 127)
198 * @access public
200 * @author DASPRiD <d@sprid.de>
203 public static function imagesmoothalphaline($image, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha=0) {
204 $icr = $r;
205 $icg = $g;
206 $icb = $b;
207 $dcol = imagecolorallocatealpha($image, $icr, $icg, $icb, $alpha);
209 if ($y1 == $y2 || $x1 == $x2)
211 imageline($image, $x1, $y1, $x2, $y2, $dcol);
213 else
215 $m = ($y2 - $y1) / ($x2 - $x1);
216 $b = $y1 - $m * $x1;
218 if (abs ($m) <2) {
219 $x = min($x1, $x2);
220 $endx = max($x1, $x2) + 1;
222 while ($x < $endx) {
223 $y = $m * $x + $b;
224 $ya = ($y == floor($y) ? 1: $y - floor($y));
225 $yb = ceil($y) - $y;
227 $trgb = ImageColorAt($image, $x, floor($y));
228 $tcr = ($trgb >> 16) & 0xFF;
229 $tcg = ($trgb >> 8) & 0xFF;
230 $tcb = $trgb & 0xFF;
231 imagesetpixel($image, $x, floor($y), imagecolorallocatealpha($image, ($tcr * $ya + $icr * $yb), ($tcg * $ya + $icg * $yb), ($tcb * $ya + $icb * $yb), $alpha));
233 $trgb = ImageColorAt($image, $x, ceil($y));
234 $tcr = ($trgb >> 16) & 0xFF;
235 $tcg = ($trgb >> 8) & 0xFF;
236 $tcb = $trgb & 0xFF;
237 imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb + $icr * $ya), ($tcg * $yb + $icg * $ya), ($tcb * $yb + $icb * $ya), $alpha));
239 $x++;
241 } else {
242 $y = min($y1, $y2);
243 $endy = max($y1, $y2) + 1;
245 while ($y < $endy) {
246 $x = ($y - $b) / $m;
247 $xa = ($x == floor($x) ? 1: $x - floor($x));
248 $xb = ceil($x) - $x;
250 $trgb = ImageColorAt($image, floor($x), $y);
251 $tcr = ($trgb >> 16) & 0xFF;
252 $tcg = ($trgb >> 8) & 0xFF;
253 $tcb = $trgb & 0xFF;
254 imagesetpixel($image, floor($x), $y, imagecolorallocatealpha($image, ($tcr * $xa + $icr * $xb), ($tcg * $xa + $icg * $xb), ($tcb * $xa + $icb * $xb), $alpha));
256 $trgb = ImageColorAt($image, ceil($x), $y);
257 $tcr = ($trgb >> 16) & 0xFF;
258 $tcg = ($trgb >> 8) & 0xFF;
259 $tcb = $trgb & 0xFF;
260 imagesetpixel ($image, ceil($x), $y, imagecolorallocatealpha($image, ($tcr * $xb + $icr * $xa), ($tcg * $xb + $icg * $xa), ($tcb * $xb + $icb * $xa), $alpha));
262 $y ++;
266 } // end of 'imagesmoothalphaLine()' function
269 * almost same as imageSmoothAlphaLine() but supports THICKNESS and DON'T support ALPHA
271 public static function imagesmoothline($image, $x1, $base_y1, $x2, $base_y2, $r, $g, $b, $thickness=1) {
272 $icr = $r;
273 $icg = $g;
274 $icb = $b;
275 $dcol = imagecolorallocate($image, $icr, $icg, $icb);
276 $alpha = 0;
278 if ($base_y1 == $base_y2 || $x1 == $x2) // 90° line
280 imagesetthickness($image, $thickness);
281 imageline($image, $x1, $base_y1, $x2, $base_y2, $dcol);
282 imagesetthickness($image, 1);
284 else
286 for ($i=0; $i<$thickness; $i++)
288 $y1 = $base_y1+$i;
289 $y2 = $base_y2+$i;
291 if ($thickness > 2)
293 $y1 = $base_y1-$thickness/2+$i;
294 $y2 = $base_y2-$thickness/2+$i;
296 if ($i!=0 AND $i!=$thickness-1) // inner lines
297 imageline($image, $x1, $y1, $x2, $y2, $dcol);
301 $m = ($y2 - $y1) / ($x2 - $x1);
302 $b = $y1 - $m * $x1;
304 if (abs ($m) <2) {
305 $x = min($x1, $x2);
306 $endx = max($x1, $x2) + 1;
308 while ($x < $endx) {
309 $y = $m * $x + $b;
310 $ya = ($y == floor($y) ? 1: $y - floor($y));
311 $yb = ceil($y) - $y;
313 $trgb = ImageColorAt($image, $x, floor($y));
314 $tcr = ($trgb >> 16) & 0xFF;
315 $tcg = ($trgb >> 8) & 0xFF;
316 $tcb = $trgb & 0xFF;
317 imagesetpixel($image, $x, floor($y), imagecolorallocatealpha($image, ($tcr * $ya + $icr * $yb), ($tcg * $ya + $icg * $yb), ($tcb * $ya + $icb * $yb), $alpha));
319 $trgb = ImageColorAt($image, $x, ceil($y));
320 $tcr = ($trgb >> 16) & 0xFF;
321 $tcg = ($trgb >> 8) & 0xFF;
322 $tcb = $trgb & 0xFF;
323 imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb + $icr * $ya), ($tcg * $yb + $icg * $ya), ($tcb * $yb + $icb * $ya), $alpha));
325 $x++;
327 } else {
328 $y = min($y1, $y2);
329 $endy = max($y1, $y2) + 1;
331 while ($y < $endy) {
332 $x = ($y - $b) / $m;
333 $xa = ($x == floor($x) ? 1: $x - floor($x));
334 $xb = ceil($x) - $x;
336 $trgb = ImageColorAt($image, floor($x), $y);
337 $tcr = ($trgb >> 16) & 0xFF;
338 $tcg = ($trgb >> 8) & 0xFF;
339 $tcb = $trgb & 0xFF;
340 imagesetpixel($image, floor($x), $y, imagecolorallocatealpha($image, ($tcr * $xa + $icr * $xb), ($tcg * $xa + $icg * $xb), ($tcb * $xa + $icb * $xb), $alpha));
342 $trgb = ImageColorAt($image, ceil($x), $y);
343 $tcr = ($trgb >> 16) & 0xFF;
344 $tcg = ($trgb >> 8) & 0xFF;
345 $tcb = $trgb & 0xFF;
346 imagesetpixel ($image, ceil($x), $y, imagecolorallocatealpha($image, ($tcr * $xb + $icr * $xa), ($tcg * $xb + $icg * $xa), ($tcb * $xb + $icb * $xa), $alpha));
348 $y ++;
352 } // end for
354 } // end of 'imagesmoothalphaLine()' function
356 // works on multidimensional array
357 public static function max($array)
359 $return = NULL;
361 // use foreach to iterate over our input array.
362 foreach( $array as $value ) {
363 // check if $value is an array...
364 if( is_array($value) ) {
365 // ... $value is an array so recursively pass it into multimax() to
366 // determine it's highest value.
367 $subvalue = utilities::max($value);
368 // if the returned $subvalue is greater than our current highest value,
369 // set it as our $return value.
370 if($return === NULL OR $subvalue > $return) {
371 $return = $subvalue;
373 } elseif($return === NULL OR $value > $return) {
374 // ... $value is not an array so set the return variable if it's greater
375 // than our highest value so far.
376 $return = $value;
380 return $return; // return (what should be) the highest value from any dimension.
383 // works on multidimensional array
384 public static function min($array)
386 $return = NULL;
388 // use foreach to iterate over our input array.
389 foreach( $array as $value ) {
390 // check if $value is an array...
391 if( is_array($value) ) {
392 // ... $value is an array so recursively pass it into multimax() to
393 // determine it's highest value.
394 $subvalue = utilities::min($value);
395 // if the returned $subvalue is greater than our current highest value,
396 // set it as our $return value.
397 if($return === NULL OR $subvalue < $return) {
398 $return = $subvalue;
400 } elseif($return === NULL OR $value < $return) {
401 // ... $value is not an array so set the return variable if it's greater
402 // than our highest value so far.
403 $return = $value;
407 return $return; // return (what should be) the highest value from any dimension.
410 public static function deg2rad($degrees)
412 return $degrees / 180.0 * M_PI;