<?php
require_once "survey_func2.php";
$data=$_GET["data"];
$d=explode(":",$data);
$label=$_GET["label"];
$l=explode(":",$label);
$text=html_entity_decode(urldecode($_GET["text"]));
$line=explode("!@",$text);
$dimension=$_GET["dim"];
$dim=explode(":",$dimension);
$road=explode("@",$_GET["access"]);
if(isset($_GET["intersect"]))$intersect=explode("@",$_GET["intersect"]); else $intersect[0]="";
if(isset($_GET["width"]))$width=$_GET["width"]; else $width=1;
if(isset($_GET["scale"]))$sc=$_GET["scale"]; else $sc="";
$ptype=0; if(isset($_GET["ptype"])){$potype=$_GET["ptype"];if(strchr($potype,"GAL")!==false) $ptype=1;} 
if(!empty($_GET["TN"])) $tn=$_GET["TN"]; else $tn="TN";
if(isset($_GET["peg"])) $pg=explode(":",$_GET["peg"]) ; else $pg=array();

$mid=$d[0];
$mg=20;
$tnlen=90;
$tnwd=40;
$pd=2;
$pdwd=10;
$armg=10;
$arwd=5;
$lf=0;

$lbx=$d[0];
$lby=$d[1];

$np=floor(count($d) /2);
$font = 'arial.ttf';
$height=500;
// create image
$image = imagecreatetruecolor($dim[0]+40, $dim[1]+80);
imageantialias($image, true);

// allocate some solors
$white    = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$color[6]    = imagecolorallocate($image, 102, 102, 102);
$color[5]    = imagecolorallocate($image, 172, 232, 17);
$color[4] = imagecolorallocate($image, 225, 225, 0);
$color[3]     = imagecolorallocate($image, 128, 255, 255);
$color[2] = imagecolorallocate($image, 0x00, 0x00, 200);
$color[1]      = imagecolorallocate($image, 0x00, 0x00, 0x00);
$color[0]  = imagecolorallocate($image, 0xFF, 0x00, 0x00);

imagefilledrectangle($image, 0, 0, $dim[0]+40, $dim[1]+80, $white);

$k=getAngles($d);
$d=modifyPoints($d,$dim[2], $dim[3],$dim[4], $dim[5]);
$mid=$d[0];


$str2=explode("|",$_GET["str"]);
$img = LoadGif("hatch.gif");
$lf=imagesettile($image,$img);
for($i=0;$i<count($str2);$i++)
{
  
 	if(empty($str2[$i])) continue;
  $str[$i]=explode("<",$str2[$i]);
  $str_type=$str[$i][0];
  $str_data[$i]=explode(":",$str[$i][1]);
   $nk=count($str_data[$i])/2;
  if($str_type==5)
  {		
	 
	  $dty=explode("-",$str_data[$i][3]);
	  $dty[1]=str_replace("zq ",":",$dty[1]); 
	  $dty[1]=str_replace("qz ","-",$dty[1]);
		$str_data[$i][3]=$dty[0];
		
	for($j=0;$j<count($str_data[$i]);$j+=2)
	  {
		  $str_data[$i][$j] *=1;
		  $str_data[$i][$j+1] *=1;
	  }
		$textAngle= getDegAngle(	$str_data[$i][0],$str_data[$i][1],$str_data[$i][2],$str_data[$i][3]);
		$midP=getMid(	$str_data[$i][0],$str_data[$i][1],$str_data[$i][2],$str_data[$i][3]);
		$textAngle=normalize($textAngle);
		//die($textAngle);
		imagettftext($image, $dty[2]-4, $textAngle, $str_data[$i][0] , $str_data[$i][1] , $color[1], $font, $dty[1]);
	  
  } else
  {
	  for($j=0;$j<count($str_data[$i]);$j+=2)
	  {
		  $str_data[$i][$j] *=0.985;
		  $str_data[$i][$j+1] *=0.985;
	  }
	  if($str_type==4)
	  {
		  DrawMultiLine($image, $str_data[$i],$nk,$color[6]);
	  } else if($str_type==2)
	  {
		  if($lf) DrawCircle($image, $str_data[$i],$nk,$color[6],1);
		  else DrawCircle($image, $str_data[$i],$nk,$color[6],0);
	  }  else 
	  {
		  if($lf)imagefilledpolygon($image, $str_data[$i],$nk, IMG_COLOR_TILED); 
		  imagepolygon($image, $str_data[$i],$nk,$color[6]);
	  }
  }
}
imagepolygon($image, $d,
    $np,
    $color[0]);


drawPoint($image,$d,5,$color[2],$l,$pg);
labelPoint($image,$d,$l,10,7,$color[1],$pg);
if(empty($ptype))
{
	labelAngle2($image,$d,$l,10,7,$color[1],$k,$pg);
	drawMarker($image,$color[5],$mid,$d[1],0,50,50,50,$dim[1]-20,$dim[0],$lbx,$lby);
}
else 
{
	labelAngle3($image,$d,$l,10,7,$color[1],$k,$pg);
	drawMarker2($image,$color[5],$mid,$d[1],0,50,50,50,$dim[1]-20,$dim[0],$lbx,$lby);
}
if(!empty($road[0]))DrawAccess($image,$d,$road,$intersect,$color[4],$width,$k);
drawOrigin($image,$color[5],$mid,$mg,$tnlen,$tnwd,$pd,$pdwd,$armg,$arwd,$tn);

$scale=drawScale($image,$d,$dim[1]-10,50,$color[1]);
$scale=labelScale($scale,3.77);

LabelDrawing2($image,10,$dim[1],$color[1],$line[0],$line[1],$line[2],$line[3],$line[4]);

imagettftext($image, 8, 0, 60, $dim[1] +2 , $color[1], $font, "SCALE: 1 / $scale");

imagettftext($image, 12, 0, 2, $dim[1]+20 , $color[1], $font, $line[5]);
imagettftext($image, 8, 0, 2, $dim[1]+45 , $color[1], $font, $line[6]);
imagettftext($image, 8, 0, 80, $dim[1]+60 , $color[1], $font, $line[7]);
imagettftext($image, 8, 0, 270, $dim[1]+45 , $color[1], $font, $line[8]);
imagettftext($image, 8, 0, 330, $dim[1]+60 , $color[1], $font, $line[9]);

//die();
// flush image
header('Content-type: image/png');
imagepng($image,null,0);
imagedestroy($image);
?> 
