<?php
// variabelen ophalen
if (isset($_GET['innercaption']) && isset($_GET['outercaption'])){
	$innercaption = $_GET['innercaption'];  // meter caption
	$outercaption = $_GET['outercaption'];	// meter id
	}
else {
	$er="Error 0";
	}
if (strlen($innercaption) >7) {			// lengte van innercaption groter dan 7
	$innercaption = substr ($innercaption, 0, 4)."..."; // afkappen dan!
	}

if (strlen($outercaption) >20) {			// lengte van outercaption groter dan 20
	$outercaption = substr ($outercaption, 0, 17)."..."; // afkappen dan!
	}

$strfill="";							//innercaption lengte op 7 tekens zetten
if (strlen($innercaption)<7){
	$i = (7-strlen($innercaption))/2;
	$strfill = str_repeat (' ', $i);
	}
$innercaption = $strfill. $innercaption . $strfill;

$strfill="";							//outercaption lengte op 20 tekens zetten
if (strlen($outercaption)<20){
	$i = (20-strlen($outercaption))/2;
	$strfill = str_repeat (' ', $i);
	}
$outercaption = $strfill. $outercaption . $strfill;

// ophalen en berekenen van analoge en digitale waardes


if (isset($_GET['max']) && is_numeric($_GET['max'])){
	$max_value = $_GET['max'];			 // maximale waarde
	}
else {
	$er="Error 1";
	}
if (isset($_GET['value']) && is_numeric($_GET['value'])){
	$dig_value = $_GET['value'];		 // deze waarde is de digitale waarde
	if ($dig_value < 0) {
		$er="Error 2";
		}
	}
else {
	$er="Error 3";
}

if ($max_value == 0 && $dig_value== 0) {		 // als de max waarde gelijk is aan de werkelijke waarde
	$er="Error 4";
	$dig_value = 1;				 // dan zetten we ze gelijk aan 1 om errors te voorkomen
	$max_value = 1;
	}

if ($max_value<$dig_value){			 // max waarde kleiner dan werkelijke waarde?
	$er = "Error 5";
	$dummy = $max_value;			 // swap deze dan...
	$max_value = $dig_value;
	$dig_value = $dummy;
	}
$max_value = 540/$max_value;
$ana_value = $dig_value * $max_value;  // deze waarde is nodig voor de wijzer

$dig_value = round($dig_value,2);	   // waarde afronden tot op 2 cijfers na de comma

//ophalen van zones
if (isset($_GET['green']) && is_numeric($_GET['green'])){
	$zone_green = $_GET['green'];
	$total = $zone_green;
	$zone_green = 135+((270 * $zone_green)/100);
	}
if (isset($_GET['red']) && is_numeric($_GET['red'])){
	$zone_red = $_GET['red'];
	$total += $zone_red;
	$zone_red = 405-((270 * $zone_red)/100);
	
	}
	
if ($total >100){
	$er="Error 6";
	}
//Hieronder niets meer veranderen indien je niet  
//vertrouwd bent met GD2
header("Content-type: image/jpg");

// meter image ophalen
$bgImg = imageCreateFromJpeg("images/gauge.jpg");      

// kleuren aanmaken
$black=imagecolorallocatealpha($bgImg, 20,20,20,0);
$black_alpha=imagecolorallocatealpha($bgImg, 0,0,0,30);

$red = imagecolorallocatealpha($bgImg, 255, 0,0,20);
$white = imagecolorallocatealpha($bgImg, 255, 255,255,0);
$grey = imagecolorallocatealpha($bgImg, 63, 63,63,80);
$green = imagecolorallocatealpha($bgImg, 0, 255,0,0);
$blue = imagecolorallocatealpha($bgImg, 0, 0,255,0);

$light_grey = imagecolorallocatealpha($bgImg, 192, 192,192,50);
$dark_green = imagecolorallocatealpha($bgImg, 0, 100,0,30);
$dark_red = imagecolorallocatealpha($bgImg, 100, 0,0,20);
$dark_orange = imagecolorallocatealpha($bgImg, 100,100,0,20);
$dark_blue = imagecolorallocatealpha($bgImg, 0, 0,200,80);
if ($er){
	$box = imageTTFBbox(9, 0, "fonts/digistrip.ttf", $er);
   $width = abs($box[4] - $box[0]);
   ImageTTFText($bgImg, 9, 0, 71-($width/2), 95, $dark_red,
"fonts/digistrip.ttf", $er);
   ImageTTFText($bgImg, 9, 0, 70-($width/2), 94, $red,
"fonts/digistrip.ttf", $er);
	
	//ImageTTFText($bgImg, 20, 15,30,80,$dark_red,"fonts/tahoma.ttf",$er);
	//ImageTTFText($bgImg, 20, 15,28,78,$red,"fonts/tahoma.ttf",$er);
	
	}
	else{
if($zone_red){
	imagefilledarc($bgImg, 70, 62, 90, 90,  $zone_red,405, $dark_red, IMG_ARC_PIE);  // rode zone
	}

if ($zone_red && $zone_green){
	imagefilledarc($bgImg, 70, 62, 90, 90, $zone_green, $zone_red, $dark_orange, IMG_ARC_PIE);  // oranje zone
	}

if($zone_green){
	imagefilledarc($bgImg, 70, 62, 90, 90, 135, $zone_green, $dark_green, IMG_ARC_PIE);// groene zone
	}

imagefilledarc($bgImg, 70, 62, 82, 82, 135, 405, $black_alpha, IMG_ARC_PIE);

//center plaatsen
imageellipse($bgImg, 69, 61, 20, 20, $light_grey); // highlight
imagefilledellipse($bgImg, 70, 62, 20, 20, $black); //center

//meter captions
$box = imageTTFBbox(10, 0, "fonts/tahoma.ttf", $innercaption);
   $width = abs($box[4] - $box[0]);
   ImageTTFText($bgImg, 10, 0, 70-($width/2), 108, $white,
"fonts/tahoma.ttf", $innercaption);
   
//ImageTTFText($bgImg, 10, 0,55,108,$white,"fonts/tahoma.ttf",$innercaption);
$box = imageTTFBbox(9, 0, "fonts/digistrip.ttf", $outercaption);
   $width = abs($box[4] - $box[0]);
   ImageTTFText($bgImg, 9, 0, 70-($width/2), 145, $black,
"fonts/digistrip.ttf", $outercaption);
   ImageTTFText($bgImg, 9, 0, 69-($width/2), 144, $white,
"fonts/digistrip.ttf", $outercaption);
//ImageTTFText($bgImg, 8, 0,20,140,$black,"fonts/digistrip_b.ttf",$outercaption); //schaduw voor outercaption
//ImageTTFText($bgImg, 8, 0,19,138,$white,"fonts/digistrip_b.ttf",$outercaption); //outercaption


// variabelen voor wijzer berekenen
$p = 270+$ana_value;
$x1 = round((cos(($p-20) * 3.14/360)*15)+70,0);  // punt 1 voor polygon
$y1 = round((sin(($p-20) * 3.14/360)*15)+62,0);

$x2 = round((cos(($p+20) * 3.14/360)*15)+70,0);  // punt 2 voor polygon
$y2 = round((sin(($p+20) * 3.14/360)*15)+62,0);

$x3 = round((cos($p * 3.14/360)*40)+70,0);		 // punt 3 voor polygon
$y3 = round((sin($p * 3.14/360)*40)+62,0);

//array maken voor berekende polygon punten 
$values = array(
           $x1,  $y1,  // Point 1 (x, y)  //wijzer coordinaten
           $x2,  $y2, // Point 2 (x, y)
           $x3,  $y3,  // Point 3 (x, y)
           );
$values_shadow = array(
           $x1+1,  $y1+1,  // Point 1 (x, y)  //schaduw voor wijzer
           $x2+1,  $y2+1, // Point 2 (x, y)
           $x3+1,  $y3+1,  // Point 3 (x, y)
           );

// een rode wijzer tekenen
imagefilledpolygon($bgImg, $values_shadow, 3, $black_alpha); // schaduw tekenen
imagefilledpolygon($bgImg, $values, 3, $red); // de wijzer tekenen

// digitale meterstand
   $box = imageTTFBbox(9, 0, "fonts/digistrip.ttf", $dig_value);
   $width = abs($box[4] - $box[0]);
   ImageTTFText($bgImg, 9, 0, 71-($width/2), 95, $dark_green,
"fonts/digistrip.ttf", $dig_value);
   ImageTTFText($bgImg, 9, 0, 70-($width/2), 94, $green,
"fonts/digistrip.ttf", $dig_value);

	}

//laat plaatje zien
Imagejpeg($bgImg,"",100);

//en destroy het weer
imagedestroy($bgImg);
?>

 