<?php
	set_time_limit(0);
	header ("Content-type: image/png");  
    $largeurImage = 1000;
    $hauteurImage = 1000;
    $im = ImageCreate ($largeurImage, $hauteurImage)
            or die ("Ошибка при создании изображения");     
    
	$blanc = ImageColorAllocate ($im, 255, 255, 255);
    $noir = ImageColorAllocate ($im, 0, 0, 0);
    $bleu = ImageColorAllocate ($im, 0, 0, 255);
	
	if(isset($_POST['upload'])){
    //Список разрешенных файлов
    $whitelist = array(".xml");         
	$data = array();
	$error = true;
	
	//Проверяем разрешение файла
    foreach  ($whitelist as  $item) {
		if(preg_match("/$item\$/i",$_FILES['userfile']['name'])) $error = false;
    }

    //если нет ошибок, грузим файл
    if(!$error) { 
    	   	  
		$folder =  'KV/';//директория в которую будет загружен файл
		
		$uploadedFile =  $folder.basename($_FILES['userfile']['name']);
				
		if(is_uploaded_file($_FILES['userfile']['tmp_name'])){
		
			if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadedFile)){
		
		        $data = $_FILES['userfile'];
			}
			else {	
				echo "Во время загрузки файла произошла ошибка";
			}
		}
		else {	
			echo "Файл не  загружен";
		}
    }
    else{
    	
		echo 'Вы загружаете запрещенный тип файла';
    }
	$f = fopen('KV/'.$data['name'], 'r');
	$fout = fopen('KV/Reprocessed_'.$data['name'], "w");
	$out = "";
	while(!feof($f)){
		$str = fgetc($f);
		if($str == "<")
			$out .= "\n<";
		if($str == ">")
			$out .= ">\n";
		if($str != ">" && $str != "<")
			$out .= $str;
	}
	fwrite($fout, $out);
	fclose($fout);
	fclose($f);
	$f = fopen('KV/Reprocessed_'.$data['name'], 'r');
	$flag = 0;
	$i = 0;
	$k = 0;
	while(!feof($f)){
		$str = fgets($f);
		if(strpos($str, "Spatial_Element") && !strpos($str, "/Spatial_Element")){	
			$flag = 0;			
			$i++;		
		}
		if(strpos($str, "/Spatial_Element")){
			$nump[$i] = $k;
			$k = 0;
		}
		if(strpos($str, "Ordinate") && !strpos($str, "/Ordinate")){
			$startpos = strpos($str, "X=");
			
			for($j = $startpos + 3;;$j++){
				if($str[$j] == "\"" || $str[$j] == "'")
					break;
				$X .= $str[$j];	
			}
			for($j = $j + 1;;$j++){
				if($str[$j] == "\"" || $str[$j] == "'")
					break;
			}
			for($j = $j + 1;;$j++){
				if($str[$j] == "\"" || $str[$j] == "'")
					break;
				$Y .= $str[$j];				
			}
			if($flag == 0){
				$k++;
				$px[$i][$k] = $X;
				$py[$i][$k] = $Y;
				$lastX = $X;
				$lastY = $Y;
				$firstpointX = $X;
				$firstpointY = $Y;
				$flag = 1;
			}else{
				$k++;
				$firstX = $lastX;
				$firstY = $lastY;
				$lastX = $X;
				$lastY = $Y;
				$px[$i][$k] = $X;
				$py[$i][$k] = $Y;
			}
			$X = "";
			$Y = "";
		}
	}
	$cont = $i;
	$maxX = 0;
	$maxY = 0;
	$minX = 1000000000000;
	$minY = 1000000000000;
	for($i = 1; $i <= $cont; $i++){
		for($j = 1; $j <= $nump[$i]; $j++){
			if($py[$i][$j] > $maxY)
				$maxY = $py[$i][$j];
			if($px[$i][$j] > $maxX)
				$maxX = $px[$i][$j];
			if($py[$i][$j] < $minY)
				$minY = $py[$i][$j];
			if($px[$i][$j] < $minX)
				$minX = $px[$i][$j];
		}
	}
	if($maxX - $minX >= $maxY - $minY){
		$stretchX = 900/($maxX - $minX);
		$stretchY = $stretchX;
	}else{
		$stretchY = 900/($maxY - $minY);
		$stretchX = $stretchY;
	}
	$shiftX = $maxX - 1000;
	$shiftY = $maxY - 1000;
	
	for($i = 1; $i <= $cont; $i++){
		for($j = 1; $j <= $nump[$i]; $j++){
			if($j != $nump[$i]){
				ImageLine($im, (50 + (($py[$i][$j] - $shiftY) - ($minY - $shiftY))*$stretchX), (50 + (1000 - ($px[$i][$j] - $shiftX))*$stretchX), (50 + (($py[$i][$j + 1] - $shiftY) - ($minY - $shiftY))*$stretchX), (50 + (1000 - ($px[$i][$j + 1] - $shiftX))*$stretchX), $bleu);
			}else{
				ImageLine($im, (50 + (($py[$i][$j] - $shiftY) - ($minY - $shiftY))*$stretchX), (50 + (1000 - ($px[$i][$j] - $shiftX))*$stretchX), (50 + (($py[$i][1] - $shiftY) - ($minY - $shiftY))*$stretchX), (50 + (1000 - ($px[$i][1] - $shiftX))*$stretchX), $bleu);
			}
		}
	}
	
	ImagePng ($im);
	
	}
?>