Location: PHPKode > scripts > GPX Mapper & CMS > gpx-mapper-cms/cls.gpxAltimeter.php
<?

/*		
	GPX Altimer Class
	Version: 1.0 
	Author: David Boardman
	URL: http://www.netzfunk.org/?usr=d
	Licenced under Creative Commons Attribution-NonCommercial-ShareAlike 2.5

	If redistributed in any form, please include credits and a link to http://www.netzfunk.org/?usr=d
*/

include("cls.gpxDrawer.php");

class gpxAltimeter extends gpxDrawer{

	var $config;
	var $xml;
	var $mwidth;
	var $mheight;
	var $path;
	var $ele;
	var $ptime;
	var $elemax;
	var $elemin;
	var $elemed;
	var $elediff;
	var $mapname;
	var $font;
	var $rapp;
	var $trkpt;
	
	function gpxAltimeter($config,$mapname,$xmlfile,$width,$height,$rapp){
		$this->xml=$xmlfile;
		$this->mwidth=$width;
		$this->mheight=$height;
		$this->path=$path;
		$this->mapname=$mapname;
		$this->rapp=$rapp;
		$this->config=$config;
		$this->path=$this->config['mainfolder'].$this->config['mapfolder'];
		$this->font=$this->path."/vera.ttf";
	}
	
	function collectAltData(){
		$total=0;
		for($i=0;$i<count($this->trkpt);$i++){
			$this->ele[]=$this->trkpt[$i]['ele'];
			$this->ptime[]=$this->trkpt[$i]['time'];
			$total+=$this->trkpt[$i]['ele'];
		}
		$this->elemin=min($this->ele);
		$this->elemax=max($this->ele);
		$this->elemed=number_format(($total/$i),1,".","");
		$this->elediff=$this->elemax-$this->elemin;
	}

	function drawAltimeter(){
		if(!is_writable($this->path)){
			exit("Check permissions on \"".$this->path."\" directory.");
		}
		
		$map=@imagecreatetruecolor($this->mwidth,$this->mheight) or die("Failed to init GD truecolor stream");
		
		$white=imagecolorallocate($map, 255, 255, 255);
		$red=imagecolorallocate($map,255,0,0);
		$black=imagecolorallocate($map, 0, 0, 0); 
		$blue=imagecolorallocate($map,10,10,255);
		$green=imagecolorallocate($map,10,255,10);
		
		$xcoord=35;
		$xcoord2=$xcoord+2;
		
		for($i=0;$i<count($this->ele);$i++){
			$ycoord=$this->calcxy($this->ele[$i]);			
			imagerectangle($map,$xcoord+2,$this->mheight,$xcoord2,$ycoord,$white);  // ALTITUDINI			
			$xcoord+=2;
			$xcoord2=$xcoord;
		}
	
		$divrapp=($this->elediff/$this->rapp);
		$divrapp=round($divrapp+$this->elemin);
		while($this->elemax > $divrapp){
			$y=$this->calcxy($divrapp);
			imageline($map,0,$y,$this->mwidth,$y,$red);
			imagettftext($map,8,0,2,$y+10,$red,$this->font,round($divrapp)."m");
			$divrapp+=$this->rapp;
		}
		$med=$this->calcxy($this->elemed);
		imageline($map,0,$med,$this->mwidth,$med,$green);
		imagettftext($map,9,0,2,$med+10,$green,$this->font,round($this->elemed)."m");
		$this->showMapToBrowser($map);
		$this->saveMapAs($map);
	}
	
	function calcxy($alt){
		$altd=($alt - $this->elemin);
		$altd=round(($altd / $this->elediff) * $this->mheight);
		$altd=$this->mheight-$altd;
		return $altd;
	}
	
	function showMapToBrowser($map){  // MOSTRA MAPPA NEL BROWSER
		header("Content-type:image/png");
		return imagepng($map);
	}
	
	function saveMapAs($map){  // SALVA LA MAPPA
		return imagepng($map,$this->path."/alt_".$this->mapname.".png");
	}
}

?>
Return current item: GPX Mapper & CMS