<?
if (!function_exists('imagecreate')) {
	header("Content-type: image/png");
	readfile("gfx/gerror.png");
	exit;
}

$width = $_REQUEST[width];
$height = $_REQUEST[height];
$titel = base64_decode($_REQUEST[titel]);
$daten = base64_decode($_REQUEST[daten]);
$daten2 = base64_decode($_REQUEST[daten2]);

$data = array();
$dat2 = array();
$daten = explode("|", $daten);
for ($i=0; $i<=count($daten)-1; $i++) {
	$war = explode(";", $daten[$i]);
	$data[$war[0]] = $war[1];
}

$daten2 = explode("-", $daten2);
for ($i=0; $i<=count($daten2)-1; $i++) {
	$war2 = explode(";", $daten2[$i]);
	$data2[$war2[0]] = $war2[1];
}

$image = imagecreate($width+4, $height);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$green = imagecolorallocate($image, 0x6b, 0x6b, 0x6b);
$black = imagecolorallocate($image, 0xcc, 0xcc, 0xcc);
$tcolor = imagecolorallocate($image, 0x66, 0x66, 0x66);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$lgray = imagecolorallocate($image, 0xFA, 0xFA, 0xFA);
$maxval = max($data);
$nval = sizeof($data);
$vmargin = 20;
$hmargin = 38;
$base = floor(($width - $hmargin) / $nval);
$ysize = $height - 2 * $vmargin;
$xsize = $nval * $base;
$titlefont = 2;
$title = $titel;
imagerectangle($image, 0, 0, $width+3, $height-1, $black);
$txtsz = imagefontwidth($titlefont) * strlen($title);
$xpos = (int)($hmargin + ($xsize - $txtsz)/2);
$xpos = max(1, $xpos);
$ypos = 3;

//---- Graues Rechteck
imagefilledrectangle($image, $hmargin, $vmargin, $hmargin + $xsize, $vmargin + $ysize, $lgray);

imagefilledrectangle($image, 38, 5, 50, 15, $green);
imagestring($image, $titlefont, 55, $ypos, "Hits" , $green);
imagefilledrectangle($image, 90, 5, 102, 15, $gray);
imagestring($image, $titlefont, 108, $ypos, "Visits" , $gray);

imagestring($image, $titlefont, $xpos, $ypos, $title , $tcolor);

$labelfont = 2;
$ngrid = 10;
$dydat = $maxval / $ngrid;
$dypix = $ysize / ($ngrid + 1);

for ($i = 0; $i <= ($ngrid + 1); $i++) {
	$ydat = (int)($i * $dydat);
	$ypos = $vmargin + $ysize - (int)($i*$dypix);
	$txtsz = imagefontwidth($labelfont) * strlen($ydat);
	$txtht = imagefontheight($labelfont);
	$xpos = (int)(($hmargin - $txtsz) / 2);
	$xpos = max(1, $xpos);
	imagestring($image, $labelfont, $xpos, $ypos - (int)($txtht/2), $ydat, $green);
	if (!($i == 0) && !($i > $ngrid)) {
		imageline($image, $hmargin - 3, $ypos, $hmargin + $xsize, $ypos, $gray);
	}
}

$padding = 3;
@$yscale = $ysize / (($ngrid+1) * $dydat);

for ($i = 0; list($xval, $yval) = each($data); $i++) {
	$ymax = $vmargin + $ysize;
	$ymin = $ymax - (int)($yval*$yscale);
	$xmax = $hmargin + ($i+1)*$base - $padding;
	$xmin = $hmargin + $i*$base + $padding;
	imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $green);
	
	$txtsz = imagefontwidth($labelfont) * strlen($xval);
	$xpos = $xmin + (int)(($base - $txtsz) / 2);
	$xpos = max($xmin, $xpos);
	$ypos = $ymax + 3;
	imagestring($image, $labelfont, $xpos, $ypos, $xval, $green);
}
for ($i = 0; list($xval, $yval) = each($data2); $i++) {
	$ymax = $vmargin + $ysize;
	$ymin = $ymax - (int)($yval*$yscale);
	$xmax = $hmargin + ($i+1)*$base - $padding;
	$xmin = $hmargin + $i*$base + $padding;
	imagefilledrectangle($image, $xmin, $ymin, $xmax, $ymax, $gray);
}
// 	imagefilledrectangle($image, $xmin+20, $ymin, $xmax, $ymax, $gray);
imagerectangle($image, $hmargin, $vmargin, $hmargin + $xsize, $vmargin + $ysize, $black);
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
?>