Wie gewünscht (Mann war das jetzt ein krampf, aber es geht ^^"
:
function color2rgb($color) {
if(strlen($color) > 7) {
return false;
}
else {
if(substr($color,0,1) === '#' {
$color = substr($color,1);
}
if(strlen($color) === 3) {
$red = substr($color,0,1);
$green = substr($color,1,1);
$blue = substr($color,2,1);
$red .= $red;
$green .= $green;
$blue .= $blue;
}
elseif(strlen($color) === 6) {
$red = substr($color,0,2);
$green = substr($color,2,2);
$blue = substr($color,4,2);
}
else {
return false;
}
return array(
0 => hexdec($red),
1 => hexdec($green),
2 => hexdec($blue),
);
}
}
function grafbar2($max,$val,$height = 5, $width = 30,$bgcolor = '#000000',$color1 = '#009900', $color2 = '#00FF00', $color3 = '#FFFF00', $color4 = '#FF0000' {
$percent = round((100/$max)*$val);
if($val >= $max) {
$color = color2rgb($color1);
}
elseif($percent >= 50) {
$color = color2rgb($color2);
}
elseif($percent >= 10) {
$color = color2rgb($color3);
}
else {
$color = color2rgb($color4);
}
// Bild erstellen
$im = ImageCreate($width,$height);
// Farben
$bgcolor = color2rgb($bgcolor);
$color = ImageColorAllocate($im,$color[0],$color[1],$color[2]);
$bgcolor = ImageColorAllocate($im,$bgcolor[0],$bgcolor[1],$bgcolor[2]);
// Balken erstellen
$bar1_start = 0;
$bar1_end = round($width/$max*$val);
$bar2_start = $bar1_end+1;
$bar2_end = $width;
ImageFilledRectangle ($im, $bar1_start, 0, $bar1_end, $height, $color); // Gefülllt
ImageFilledRectangle ($im, $bar2_start, 0, $bar2_end, $height, $bgcolor); // Leer
return $im;
}
Anwendung:
header('content-type: image/gif';
ImageGIF (grafbar2(100,40,20,200));
Bild hängt an ^^"