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");
17 public static function hex2rgb($color)
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]);
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);
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;
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;
66 list($textwidth, $textheight) = array($textheight, $textwidth);
69 $height = $bottom - $top;
70 $width = $right - $left;
72 // // Break the text into lines, and into an array
75 // $lines = wordwrap($text, floor($width / imagefontwidth($font)), "\n", true);
76 // $lines = explode("\n", $lines);
80 // $lines = wordwrap($text, floor($height / imagefontwidth($font)), "\n", true);
81 // $lines = explode("\n", $lines);
84 $lines = array($text);
86 // Other important numbers
89 $line_height = $textheight +
$leading;
90 $line_count = floor($height / $line_height);
94 $line_height = $textwidth +
$leading;
95 $line_count = floor($width / $line_height);
97 $line_count = ($line_count > count($lines)) ?
(count($lines)) : ($line_count);
100 for ($i = 0; $i < $line_count; $i++
)
105 case VALIGN_TOP
: // Top
106 $y = $top +
(($i+
1) * $line_height);
108 case VALIGN_MIDDLE
: // Middle
109 $y = $top +
(($height - ($line_count * $line_height)) / 2) +
(($i+
1) * $line_height);
111 case VALIGN_BOTTOM
: // Bottom
112 $y = ($top +
$height) - ($line_count * $line_height) +
(($i+
1) * $line_height);
119 $line_width = $textwidth;
122 case ALIGN_LEFT
: // Left
123 $x = ($vertical) ?
$left +
$textwidth : $left;
125 case ALIGN_CENTER
: // Center
126 $x = ($vertical) ?
$left +
$width/2 +
$textwidth/2 : $left +
(($width - $line_width) / 2);
128 case ALIGN_RIGHT
: // Right
129 $x = ($vertical) ?
$right : $left +
($width - $line_width);
137 imagettftext($image, $font_size, 90, $x, $y, $color, $font, $lines[$i]);
139 imagettftext($image, $font_size, 0, $x, $y, $color, $font, $lines[$i]);
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);
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);
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
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
185 * function imageSmoothAlphaLine() - version 1.0
186 * Draws a smooth line with alpha-functionality
188 * @param ident the image to draw on
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)
200 * @author DASPRiD <d@sprid.de>
203 public static function imagesmoothalphaline($image, $x1, $y1, $x2, $y2, $r, $g, $b, $alpha=0) {
207 $dcol = imagecolorallocatealpha($image, $icr, $icg, $icb, $alpha);
209 if ($y1 == $y2 ||
$x1 == $x2)
211 imageline($image, $x1, $y1, $x2, $y2, $dcol);
215 $m = ($y2 - $y1) / ($x2 - $x1);
220 $endx = max($x1, $x2) +
1;
224 $ya = ($y == floor($y) ?
1: $y - floor($y));
227 $trgb = ImageColorAt($image, $x, floor($y));
228 $tcr = ($trgb >> 16) & 0xFF;
229 $tcg = ($trgb >> 8) & 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;
237 imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb +
$icr * $ya), ($tcg * $yb +
$icg * $ya), ($tcb * $yb +
$icb * $ya), $alpha));
243 $endy = max($y1, $y2) +
1;
247 $xa = ($x == floor($x) ?
1: $x - floor($x));
250 $trgb = ImageColorAt($image, floor($x), $y);
251 $tcr = ($trgb >> 16) & 0xFF;
252 $tcg = ($trgb >> 8) & 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;
260 imagesetpixel ($image, ceil($x), $y, imagecolorallocatealpha($image, ($tcr * $xb +
$icr * $xa), ($tcg * $xb +
$icg * $xa), ($tcb * $xb +
$icb * $xa), $alpha));
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) {
275 $dcol = imagecolorallocate($image, $icr, $icg, $icb);
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);
286 for ($i=0; $i<$thickness; $i++
)
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);
306 $endx = max($x1, $x2) +
1;
310 $ya = ($y == floor($y) ?
1: $y - floor($y));
313 $trgb = ImageColorAt($image, $x, floor($y));
314 $tcr = ($trgb >> 16) & 0xFF;
315 $tcg = ($trgb >> 8) & 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;
323 imagesetpixel($image, $x, ceil($y), imagecolorallocatealpha($image, ($tcr * $yb +
$icr * $ya), ($tcg * $yb +
$icg * $ya), ($tcb * $yb +
$icb * $ya), $alpha));
329 $endy = max($y1, $y2) +
1;
333 $xa = ($x == floor($x) ?
1: $x - floor($x));
336 $trgb = ImageColorAt($image, floor($x), $y);
337 $tcr = ($trgb >> 16) & 0xFF;
338 $tcg = ($trgb >> 8) & 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;
346 imagesetpixel ($image, ceil($x), $y, imagecolorallocatealpha($image, ($tcr * $xb +
$icr * $xa), ($tcg * $xb +
$icg * $xa), ($tcb * $xb +
$icb * $xa), $alpha));
354 } // end of 'imagesmoothalphaLine()' function
356 // works on multidimensional array
357 public static function max($array)
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) {
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.
380 return $return; // return (what should be) the highest value from any dimension.
383 // works on multidimensional array
384 public static function min($array)
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) {
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.
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
;