replaces deprecated split(), adds .gitignore

This commit is contained in:
Gerhard Koch 2014-10-31 21:52:47 +01:00
parent 0331b801af
commit f5ee38f2e3
2 changed files with 321 additions and 306 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.idea/

View File

@ -1,7 +1,7 @@
<?php <?php
/** /**
* staticMapLite 0.03 * staticMapLite 0.3.1
* *
* Copyright 2009 Gerhard Koch * Copyright 2009 Gerhard Koch
* *
@ -26,333 +26,347 @@
*/ */
error_reporting(0); error_reporting(0);
ini_set('display_errors','off'); ini_set('display_errors', 'off');
Class staticMapLite { Class staticMapLite
{
protected $maxWidth = 1024; protected $maxWidth = 1024;
protected $maxHeight = 1024; protected $maxHeight = 1024;
protected $tileSize = 256; protected $tileSize = 256;
protected $tileSrcUrl = array( 'mapnik' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png', protected $tileSrcUrl = array('mapnik' => 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png',
'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png', 'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{Z}/{X}/{Y}.png',
'cycle' => 'http://c.andy.sandbox.cloudmade.com/tiles/cycle/{Z}/{X}/{Y}.png', 'cycle' => 'http://a.tile.opencyclemap.org/cycle/{Z}/{X}/{Y}.png',
'cycle' => 'http://a.tile.opencyclemap.org/cycle/{Z}/{X}/{Y}.png', );
'piste' => 'http://tiles.openpistemap.org/nocontours/{Z}/{X}/{Y}.png',
'piste' => 'http://openpistemap.org/tiles/contours/{Z}/{X}/{Y}.png',
'hikebike' => 'http://toolserver.org/tiles/hikebike/{Z}/{X}/{Y}.png'
); protected $tileDefaultSrc = 'mapnik';
protected $markerBaseDir = 'images/markers';
protected $osmLogo = 'images/osm_logo.png';
protected $tileDefaultSrc = 'mapnik'; protected $markerPrototypes = array(
protected $markerBaseDir = 'images/markers'; // found at http://www.mapito.net/map-marker-icons.html
protected $osmLogo = 'images/osm_logo.png'; 'lighblue' => array('regex' => '/^lightblue([0-9]+)$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '0,-19',
'offsetShadow' => false
),
// openlayers std markers
'ol-marker' => array('regex' => '/^ol-marker(|-blue|-gold|-green)+$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-25',
'offsetShadow' => '-1,-13'
),
// taken from http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
'ylw' => array('regex' => '/^(pink|purple|red|ltblu|ylw)-pushpin$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-32',
'offsetShadow' => '-1,-13'
)
protected $markerPrototypes = array(// found at http://www.mapito.net/map-marker-icons.html );
'lighblue' => array('regex'=>'/^lightblue([0-9]+)$/',
'extension'=>'.png',
'shadow'=>false,
'offsetImage'=>'0,-19',
'offsetShadow'=>false
),
// openlayers std markers
'ol-marker'=> array('regex'=>'/^ol-marker(|-blue|-gold|-green)+$/',
'extension'=>'.png',
'shadow'=>'../marker_shadow.png',
'offsetImage'=>'-10,-25',
'offsetShadow'=>'-1,-13'
),
// taken from http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
'ylw'=> array('regex'=>'/^(pink|purple|red|ltblu|ylw)-pushpin$/',
'extension'=>'.png',
'shadow'=>'../marker_shadow.png',
'offsetImage'=>'-10,-32',
'offsetShadow'=>'-1,-13'
)
);
protected $useTileCache = true;
protected $tileCacheBaseDir = '../cache/tiles';
protected $useTileCache = true; protected $useMapCache = true;
protected $tileCacheBaseDir = 'cache/tiles'; protected $mapCacheBaseDir = '../cache/maps';
protected $mapCacheID = '';
protected $mapCacheFile = '';
protected $mapCacheExtension = 'png';
protected $useMapCache = true; protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype;
protected $mapCacheBaseDir = 'cache/maps'; protected $centerX, $centerY, $offsetX, $offsetY;
protected $mapCacheID = '';
protected $mapCacheFile = '';
protected $mapCacheExtension = 'png';
protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype; public function __construct()
protected $centerX, $centerY, $offsetX, $offsetY; {
$this->zoom = 0;
$this->lat = 0;
$this->lon = 0;
$this->width = 500;
$this->height = 350;
$this->markers = array();
$this->maptype = $this->tileDefaultSrc;
}
public function __construct(){ public function parseParams()
$this->zoom = 0; {
$this->lat = 0; global $_GET;
$this->lon = 0;
$this->width = 500;
$this->height = 350;
$this->markers = array();
$this->maptype = $this->tileDefaultSrc;
}
public function parseParams(){ // get zoom from GET paramter
global $_GET; $this->zoom = $_GET['zoom'] ? intval($_GET['zoom']) : 0;
if ($this->zoom > 18) $this->zoom = 18;
// get zoom from GET paramter // get lat and lon from GET paramter
$this->zoom = $_GET['zoom']?intval($_GET['zoom']):0; list($this->lat, $this->lon) = explode(',', $_GET['center']);
if($this->zoom>18)$this->zoom = 18; $this->lat = floatval($this->lat);
$this->lon = floatval($this->lon);
// get lat and lon from GET paramter // get zoom from GET paramter
list($this->lat,$this->lon) = split(',',$_GET['center']); if ($_GET['size']) {
$this->lat = floatval($this->lat); list($this->width, $this->height) = explode('x', $_GET['size']);
$this->lon = floatval($this->lon); $this->width = intval($this->width);
if ($this->width > $this->maxWidth) $this->width = $this->maxWidth;
$this->height = intval($this->height);
if ($this->height > $this->maxHeight) $this->height = $this->maxHeight;
}
if (!empty($_GET['markers'])) {
$markers = explode('|', $_GET['markers']);
foreach ($markers as $marker) {
list($markerLat, $markerLon, $markerType) = explode(',', $marker);
$markerLat = floatval($markerLat);
$markerLon = floatval($markerLon);
$markerType = basename($markerType);
$this->markers[] = array('lat' => $markerLat, 'lon' => $markerLon, 'type' => $markerType);
}
// get zoom from GET paramter }
if($_GET['size']){ if ($_GET['maptype']) {
list($this->width, $this->height) = split('x',$_GET['size']); if (array_key_exists($_GET['maptype'], $this->tileSrcUrl)) $this->maptype = $_GET['maptype'];
$this->width = intval($this->width); }
if($this->width > $this->maxWidth) $this->width = $this->maxWidth; }
$this->height = intval($this->height);
if($this->height > $this->maxHeight) $this->height = $this->maxHeight;
}
if($_GET['markers']){
$markers = split('%7C|\|',$_GET['markers']);
foreach($markers as $marker){
list($markerLat, $markerLon, $markerType) = split(',',$marker);
$markerLat = floatval($markerLat);
$markerLon = floatval($markerLon);
$markerType = basename($markerType);
$this->markers[] = array('lat'=>$markerLat, 'lon'=>$markerLon, 'type'=>$markerType);
}
} public function lonToTile($long, $zoom)
if($_GET['maptype']){ {
if(array_key_exists($_GET['maptype'],$this->tileSrcUrl)) $this->maptype = $_GET['maptype']; return (($long + 180) / 360) * pow(2, $zoom);
} }
}
public function lonToTile($long, $zoom){ public function latToTile($lat, $zoom)
return (($long + 180) / 360) * pow(2, $zoom); {
} return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
}
public function latToTile($lat, $zoom){ public function initCoords()
return (1 - log(tan($lat * pi()/180) + 1 / cos($lat* pi()/180)) / pi()) /2 * pow(2, $zoom); {
} $this->centerX = $this->lonToTile($this->lon, $this->zoom);
$this->centerY = $this->latToTile($this->lat, $this->zoom);
$this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
}
public function initCoords(){ public function createBaseMap()
$this->centerX = $this->lonToTile($this->lon, $this->zoom); {
$this->centerY = $this->latToTile($this->lat, $this->zoom); $this->image = imagecreatetruecolor($this->width, $this->height);
$this->offsetX = floor((floor($this->centerX)-$this->centerX)*$this->tileSize); $startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
$this->offsetY = floor((floor($this->centerY)-$this->centerY)*$this->tileSize); $startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
} $endX = ceil($this->centerX + ($this->width / $this->tileSize) / 2);
$endY = ceil($this->centerY + ($this->height / $this->tileSize) / 2);
$this->offsetX = -floor(($this->centerX - floor($this->centerX)) * $this->tileSize);
$this->offsetY = -floor(($this->centerY - floor($this->centerY)) * $this->tileSize);
$this->offsetX += floor($this->width / 2);
$this->offsetY += floor($this->height / 2);
$this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
$this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;
public function createBaseMap(){ for ($x = $startX; $x <= $endX; $x++) {
$this->image = imagecreatetruecolor($this->width, $this->height); for ($y = $startY; $y <= $endY; $y++) {
$startX = floor($this->centerX-($this->width/$this->tileSize)/2); $url = str_replace(array('{Z}', '{X}', '{Y}'), array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]);
$startY = floor($this->centerY-($this->height/$this->tileSize)/2); $tileData = $this->fetchTile($url);
$endX = ceil($this->centerX+($this->width/$this->tileSize)/2); if ($tileData) {
$endY = ceil($this->centerY+($this->height/$this->tileSize)/2); $tileImage = imagecreatefromstring($tileData);
$this->offsetX = -floor(($this->centerX-floor($this->centerX))*$this->tileSize); } else {
$this->offsetY = -floor(($this->centerY-floor($this->centerY))*$this->tileSize); $tileImage = imagecreate($this->tileSize, $this->tileSize);
$this->offsetX += floor($this->width/2); $color = imagecolorallocate($tileImage, 255, 255, 255);
$this->offsetY += floor($this->height/2); @imagestring($tileImage, 1, 127, 127, 'err', $color);
$this->offsetX += floor($startX-floor($this->centerX))*$this->tileSize; }
$this->offsetY += floor($startY-floor($this->centerY))*$this->tileSize; $destX = ($x - $startX) * $this->tileSize + $this->offsetX;
$destY = ($y - $startY) * $this->tileSize + $this->offsetY;
for($x=$startX; $x<=$endX; $x++){ imagecopy($this->image, $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
for($y=$startY; $y<=$endY; $y++){ }
$url = str_replace(array('{Z}','{X}','{Y}'),array($this->zoom, $x, $y), $this->tileSrcUrl[$this->maptype]); }
$tileData = $this->fetchTile($url); }
if($tileData){
$tileImage = imagecreatefromstring($tileData);
} else {
$tileImage = imagecreate($this->tileSize,$this->tileSize);
$color = imagecolorallocate($tileImage, 255, 255, 255);
@imagestring($tileImage,1,127,127,'err',$color);
}
$destX = ($x-$startX)*$this->tileSize+$this->offsetX;
$destY = ($y-$startY)*$this->tileSize+$this->offsetY;
imagecopy($this->image, $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
}
}
}
public function placeMarkers(){ public function placeMarkers()
// loop thru marker array {
foreach($this->markers as $marker){ // loop thru marker array
// set some local variables foreach ($this->markers as $marker) {
$markerLat = $marker['lat']; // set some local variables
$markerLon = $marker['lon']; $markerLat = $marker['lat'];
$markerType = $marker['type']; $markerLon = $marker['lon'];
// clear variables from previous loops $markerType = $marker['type'];
$markerFilename = ''; // clear variables from previous loops
$markerShadow = ''; $markerFilename = '';
$matches = false; $markerShadow = '';
// check for marker type, get settings from markerPrototypes $matches = false;
if($markerType){ // check for marker type, get settings from markerPrototypes
foreach($this->markerPrototypes as $markerPrototype){ if ($markerType) {
if(preg_match($markerPrototype['regex'],$markerType,$matches)){ foreach ($this->markerPrototypes as $markerPrototype) {
$markerFilename = $matches[0].$markerPrototype['extension']; if (preg_match($markerPrototype['regex'], $markerType, $matches)) {
if($markerPrototype['offsetImage']){ $markerFilename = $matches[0] . $markerPrototype['extension'];
list($markerImageOffsetX, $markerImageOffsetY) = split(",",$markerPrototype['offsetImage']); if ($markerPrototype['offsetImage']) {
} list($markerImageOffsetX, $markerImageOffsetY) = explode(",", $markerPrototype['offsetImage']);
$markerShadow = $markerPrototype['shadow']; }
if($markerShadow){ $markerShadow = $markerPrototype['shadow'];
list($markerShadowOffsetX, $markerShadowOffsetY) = split(",",$markerPrototype['offsetShadow']); if ($markerShadow) {
} list($markerShadowOffsetX, $markerShadowOffsetY) = explode(",", $markerPrototype['offsetShadow']);
} }
} }
}
// check required files or set default }
if($markerFilename == '' || !file_exists($this->markerBaseDir.'/'.$markerFilename)){ }
$markerIndex++;
$markerFilename = 'lightblue'.$markerIndex.'.png';
$markerImageOffsetX = 0;
$markerImageOffsetY = -19; }
// create img resource // check required files or set default
if(file_exists($this->markerBaseDir.'/'.$markerFilename)){ if ($markerFilename == '' || !file_exists($this->markerBaseDir . '/' . $markerFilename)) {
$markerImg = imagecreatefrompng($this->markerBaseDir.'/'.$markerFilename); $markerIndex++;
} else { $markerFilename = 'lightblue' . $markerIndex . '.png';
$markerImg = imagecreatefrompng($this->markerBaseDir.'/lightblue1.png'); $markerImageOffsetX = 0;
} $markerImageOffsetY = -19;
}
// check for shadow + create shadow recource // create img resource
if($markerShadow && file_exists($this->markerBaseDir.'/'.$markerShadow)){ if (file_exists($this->markerBaseDir . '/' . $markerFilename)) {
$markerShadowImg = imagecreatefrompng($this->markerBaseDir.'/'.$markerShadow); $markerImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerFilename);
} } else {
$markerImg = imagecreatefrompng($this->markerBaseDir . '/lightblue1.png');
}
// calc position // check for shadow + create shadow recource
$destX = floor(($this->width/2)-$this->tileSize*($this->centerX-$this->lonToTile($markerLon, $this->zoom))); if ($markerShadow && file_exists($this->markerBaseDir . '/' . $markerShadow)) {
$destY = floor(($this->height/2)-$this->tileSize*($this->centerY-$this->latToTile($markerLat, $this->zoom))); $markerShadowImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerShadow);
}
// copy shadow on basemap // calc position
if($markerShadow && $markerShadowImg){ $destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - $this->lonToTile($markerLon, $this->zoom)));
imagecopy($this->image, $markerShadowImg, $destX+intval($markerShadowOffsetX), $destY+intval($markerShadowOffsetY), $destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - $this->latToTile($markerLat, $this->zoom)));
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
}
// copy marker on basemap above shadow // copy shadow on basemap
imagecopy($this->image, $markerImg, $destX+intval($markerImageOffsetX), $destY+intval($markerImageOffsetY), if ($markerShadow && $markerShadowImg) {
0, 0, imagesx($markerImg), imagesy($markerImg)); imagecopy($this->image, $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
}
}; // copy marker on basemap above shadow
} imagecopy($this->image, $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg));
};
}
public function tileUrlToFilename($url)
{
return $this->tileCacheBaseDir . "/" . str_replace(array('http://'), '', $url);
}
public function tileUrlToFilename($url){ public function checkTileCache($url)
return $this->tileCacheBaseDir."/".str_replace(array('http://'),'',$url); {
} $filename = $this->tileUrlToFilename($url);
if (file_exists($filename)) {
return file_get_contents($filename);
}
}
public function checkTileCache($url){ public function checkMapCache()
$filename = $this->tileUrlToFilename($url); {
if(file_exists($filename)){ $this->mapCacheID = md5($this->serializeParams());
return file_get_contents($filename); $filename = $this->mapCacheIDToFilename();
} if (file_exists($filename)) return true;
} }
public function checkMapCache(){ public function serializeParams()
$this->mapCacheID = md5($this->serializeParams()); {
$filename = $this->mapCacheIDToFilename(); return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype));
if(file_exists($filename)) return true; }
}
public function serializeParams(){ public function mapCacheIDToFilename()
return join("&",array($this->zoom,$this->lat,$this->lon,$this->width,$this->height, serialize($this->markers),$this->maptype)); {
} if (!$this->mapCacheFile) {
$this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_" . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2) . "/" . substr($this->mapCacheID, 4);
public function mapCacheIDToFilename(){ }
if(!$this->mapCacheFile){ return $this->mapCacheFile . "." . $this->mapCacheExtension;
$this->mapCacheFile = $this->mapCacheBaseDir."/".$this->maptype."/".$this->zoom."/cache_".substr($this->mapCacheID,0,2)."/".substr($this->mapCacheID,2,2)."/".substr($this->mapCacheID,4); }
}
return $this->mapCacheFile.".".$this->mapCacheExtension;
}
public function mkdir_recursive($pathname, $mode)
{
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
}
public function mkdir_recursive($pathname, $mode){ public function writeTileToCache($url, $data)
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); {
return is_dir($pathname) || @mkdir($pathname, $mode); $filename = $this->tileUrlToFilename($url);
} $this->mkdir_recursive(dirname($filename), 0777);
public function writeTileToCache($url, $data){ file_put_contents($filename, $data);
$filename = $this->tileUrlToFilename($url); }
$this->mkdir_recursive(dirname($filename),0777);
file_put_contents($filename, $data);
}
public function fetchTile($url){ public function fetchTile($url)
if($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached; {
$ch = curl_init(); if ($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0");
$tile = curl_exec($ch); curl_setopt($ch, CURLOPT_URL, $url);
curl_close($ch); $tile = curl_exec($ch);
if($tile && $this->useTileCache){ curl_close($ch);
$this->writeTileToCache($url,$tile); if ($tile && $this->useTileCache) {
} $this->writeTileToCache($url, $tile);
return $tile; }
return $tile;
} }
public function copyrightNotice(){ public function copyrightNotice()
$logoImg = imagecreatefrompng($this->osmLogo); {
imagecopy($this->image, $logoImg, imagesx($this->image)-imagesx($logoImg), imagesy($this->image)-imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg)); $logoImg = imagecreatefrompng($this->osmLogo);
imagecopy($this->image, $logoImg, imagesx($this->image) - imagesx($logoImg), imagesy($this->image) - imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg));
} }
public function sendHeader(){ public function sendHeader()
header('Content-Type: image/png'); {
$expires = 60*60*24*14; header('Content-Type: image/png');
header("Pragma: public"); $expires = 60 * 60 * 24 * 14;
header("Cache-Control: maxage=".$expires); header("Pragma: public");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT'); header("Cache-Control: maxage=" . $expires);
} header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
}
public function makeMap(){ public function makeMap()
$this->initCoords(); {
$this->createBaseMap(); $this->initCoords();
if(count($this->markers))$this->placeMarkers(); $this->createBaseMap();
if($this->osmLogo) $this->copyrightNotice(); if (count($this->markers)) $this->placeMarkers();
} if ($this->osmLogo) $this->copyrightNotice();
}
public function showMap(){ public function showMap()
$this->parseParams(); {
if($this->useMapCache){ $this->parseParams();
// use map cache, so check cache for map if ($this->useMapCache) {
if(!$this->checkMapCache()){ // use map cache, so check cache for map
// map is not in cache, needs to be build if (!$this->checkMapCache()) {
$this->makeMap(); // map is not in cache, needs to be build
$this->mkdir_recursive(dirname($this->mapCacheIDToFilename()),0777); $this->makeMap();
imagepng($this->image,$this->mapCacheIDToFilename(),9); $this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777);
$this->sendHeader(); imagepng($this->image, $this->mapCacheIDToFilename(), 9);
if(file_exists($this->mapCacheIDToFilename())){ $this->sendHeader();
return file_get_contents($this->mapCacheIDToFilename()); if (file_exists($this->mapCacheIDToFilename())) {
} else { return file_get_contents($this->mapCacheIDToFilename());
return imagepng($this->image); } else {
} return imagepng($this->image);
} else { }
// map is in cache } else {
$this->sendHeader(); // map is in cache
return file_get_contents($this->mapCacheIDToFilename()); $this->sendHeader();
} return file_get_contents($this->mapCacheIDToFilename());
}
} else { } else {
// no cache, make map, send headers and deliver png // no cache, make map, send headers and deliver png
$this->makeMap(); $this->makeMap();
$this->sendHeader(); $this->sendHeader();
return imagepng($this->image); return imagepng($this->image);
} }
} }
} }
$map = new staticMapLite(); $map = new staticMapLite();
print $map->showMap(); print $map->showMap();
?>