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
* *
@ -28,7 +28,8 @@
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;
@ -36,19 +37,15 @@ Class staticMapLite {
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 $tileDefaultSrc = 'mapnik';
protected $markerBaseDir = 'images/markers'; protected $markerBaseDir = 'images/markers';
protected $osmLogo = 'images/osm_logo.png'; protected $osmLogo = 'images/osm_logo.png';
protected $markerPrototypes = array(// found at http://www.mapito.net/map-marker-icons.html protected $markerPrototypes = array(
// found at http://www.mapito.net/map-marker-icons.html
'lighblue' => array('regex' => '/^lightblue([0-9]+)$/', 'lighblue' => array('regex' => '/^lightblue([0-9]+)$/',
'extension' => '.png', 'extension' => '.png',
'shadow' => false, 'shadow' => false,
@ -73,12 +70,11 @@ Class staticMapLite {
); );
protected $useTileCache = true; protected $useTileCache = true;
protected $tileCacheBaseDir = 'cache/tiles'; protected $tileCacheBaseDir = '../cache/tiles';
protected $useMapCache = true; protected $useMapCache = true;
protected $mapCacheBaseDir = 'cache/maps'; protected $mapCacheBaseDir = '../cache/maps';
protected $mapCacheID = ''; protected $mapCacheID = '';
protected $mapCacheFile = ''; protected $mapCacheFile = '';
protected $mapCacheExtension = 'png'; protected $mapCacheExtension = 'png';
@ -86,7 +82,8 @@ Class staticMapLite {
protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype; protected $zoom, $lat, $lon, $width, $height, $markers, $image, $maptype;
protected $centerX, $centerY, $offsetX, $offsetY; protected $centerX, $centerY, $offsetX, $offsetY;
public function __construct(){ public function __construct()
{
$this->zoom = 0; $this->zoom = 0;
$this->lat = 0; $this->lat = 0;
$this->lon = 0; $this->lon = 0;
@ -96,7 +93,8 @@ Class staticMapLite {
$this->maptype = $this->tileDefaultSrc; $this->maptype = $this->tileDefaultSrc;
} }
public function parseParams(){ public function parseParams()
{
global $_GET; global $_GET;
// get zoom from GET paramter // get zoom from GET paramter
@ -104,22 +102,22 @@ Class staticMapLite {
if ($this->zoom > 18) $this->zoom = 18; if ($this->zoom > 18) $this->zoom = 18;
// get lat and lon from GET paramter // get lat and lon from GET paramter
list($this->lat,$this->lon) = split(',',$_GET['center']); list($this->lat, $this->lon) = explode(',', $_GET['center']);
$this->lat = floatval($this->lat); $this->lat = floatval($this->lat);
$this->lon = floatval($this->lon); $this->lon = floatval($this->lon);
// get zoom from GET paramter // get zoom from GET paramter
if ($_GET['size']) { if ($_GET['size']) {
list($this->width, $this->height) = split('x',$_GET['size']); list($this->width, $this->height) = explode('x', $_GET['size']);
$this->width = intval($this->width); $this->width = intval($this->width);
if ($this->width > $this->maxWidth) $this->width = $this->maxWidth; if ($this->width > $this->maxWidth) $this->width = $this->maxWidth;
$this->height = intval($this->height); $this->height = intval($this->height);
if ($this->height > $this->maxHeight) $this->height = $this->maxHeight; if ($this->height > $this->maxHeight) $this->height = $this->maxHeight;
} }
if($_GET['markers']){ if (!empty($_GET['markers'])) {
$markers = split('%7C|\|',$_GET['markers']); $markers = explode('|', $_GET['markers']);
foreach ($markers as $marker) { foreach ($markers as $marker) {
list($markerLat, $markerLon, $markerType) = split(',',$marker); list($markerLat, $markerLon, $markerType) = explode(',', $marker);
$markerLat = floatval($markerLat); $markerLat = floatval($markerLat);
$markerLon = floatval($markerLon); $markerLon = floatval($markerLon);
$markerType = basename($markerType); $markerType = basename($markerType);
@ -132,22 +130,26 @@ Class staticMapLite {
} }
} }
public function lonToTile($long, $zoom){ public function lonToTile($long, $zoom)
{
return (($long + 180) / 360) * pow(2, $zoom); return (($long + 180) / 360) * pow(2, $zoom);
} }
public function latToTile($lat, $zoom){ public function latToTile($lat, $zoom)
{
return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom); return (1 - log(tan($lat * pi() / 180) + 1 / cos($lat * pi() / 180)) / pi()) / 2 * pow(2, $zoom);
} }
public function initCoords(){ public function initCoords()
{
$this->centerX = $this->lonToTile($this->lon, $this->zoom); $this->centerX = $this->lonToTile($this->lon, $this->zoom);
$this->centerY = $this->latToTile($this->lat, $this->zoom); $this->centerY = $this->latToTile($this->lat, $this->zoom);
$this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize); $this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize); $this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
} }
public function createBaseMap(){ public function createBaseMap()
{
$this->image = imagecreatetruecolor($this->width, $this->height); $this->image = imagecreatetruecolor($this->width, $this->height);
$startX = floor($this->centerX - ($this->width / $this->tileSize) / 2); $startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
$startY = floor($this->centerY - ($this->height / $this->tileSize) / 2); $startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
@ -179,7 +181,8 @@ Class staticMapLite {
} }
public function placeMarkers(){ public function placeMarkers()
{
// loop thru marker array // loop thru marker array
foreach ($this->markers as $marker) { foreach ($this->markers as $marker) {
// set some local variables // set some local variables
@ -196,13 +199,14 @@ Class staticMapLite {
if (preg_match($markerPrototype['regex'], $markerType, $matches)) { if (preg_match($markerPrototype['regex'], $markerType, $matches)) {
$markerFilename = $matches[0] . $markerPrototype['extension']; $markerFilename = $matches[0] . $markerPrototype['extension'];
if ($markerPrototype['offsetImage']) { if ($markerPrototype['offsetImage']) {
list($markerImageOffsetX, $markerImageOffsetY) = split(",",$markerPrototype['offsetImage']); list($markerImageOffsetX, $markerImageOffsetY) = explode(",", $markerPrototype['offsetImage']);
} }
$markerShadow = $markerPrototype['shadow']; $markerShadow = $markerPrototype['shadow'];
if ($markerShadow) { if ($markerShadow) {
list($markerShadowOffsetX, $markerShadowOffsetY) = split(",",$markerPrototype['offsetShadow']); list($markerShadowOffsetX, $markerShadowOffsetY) = explode(",", $markerPrototype['offsetShadow']);
} }
} }
} }
} }
@ -211,7 +215,8 @@ Class staticMapLite {
$markerIndex++; $markerIndex++;
$markerFilename = 'lightblue' . $markerIndex . '.png'; $markerFilename = 'lightblue' . $markerIndex . '.png';
$markerImageOffsetX = 0; $markerImageOffsetX = 0;
$markerImageOffsetY = -19; } $markerImageOffsetY = -19;
}
// create img resource // create img resource
if (file_exists($this->markerBaseDir . '/' . $markerFilename)) { if (file_exists($this->markerBaseDir . '/' . $markerFilename)) {
@ -243,29 +248,33 @@ Class staticMapLite {
} }
public function tileUrlToFilename($url)
public function tileUrlToFilename($url){ {
return $this->tileCacheBaseDir . "/" . str_replace(array('http://'), '', $url); return $this->tileCacheBaseDir . "/" . str_replace(array('http://'), '', $url);
} }
public function checkTileCache($url){ public function checkTileCache($url)
{
$filename = $this->tileUrlToFilename($url); $filename = $this->tileUrlToFilename($url);
if (file_exists($filename)) { if (file_exists($filename)) {
return file_get_contents($filename); return file_get_contents($filename);
} }
} }
public function checkMapCache(){ public function checkMapCache()
{
$this->mapCacheID = md5($this->serializeParams()); $this->mapCacheID = md5($this->serializeParams());
$filename = $this->mapCacheIDToFilename(); $filename = $this->mapCacheIDToFilename();
if (file_exists($filename)) return true; if (file_exists($filename)) return true;
} }
public function serializeParams(){ public function serializeParams()
{
return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype)); return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype));
} }
public function mapCacheIDToFilename(){ public function mapCacheIDToFilename()
{
if (!$this->mapCacheFile) { 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); $this->mapCacheFile = $this->mapCacheBaseDir . "/" . $this->maptype . "/" . $this->zoom . "/cache_" . substr($this->mapCacheID, 0, 2) . "/" . substr($this->mapCacheID, 2, 2) . "/" . substr($this->mapCacheID, 4);
} }
@ -273,18 +282,21 @@ Class staticMapLite {
} }
public function mkdir_recursive($pathname, $mode)
public function mkdir_recursive($pathname, $mode){ {
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode); return is_dir($pathname) || @mkdir($pathname, $mode);
} }
public function writeTileToCache($url, $data){
public function writeTileToCache($url, $data)
{
$filename = $this->tileUrlToFilename($url); $filename = $this->tileUrlToFilename($url);
$this->mkdir_recursive(dirname($filename), 0777); $this->mkdir_recursive(dirname($filename), 0777);
file_put_contents($filename, $data); file_put_contents($filename, $data);
} }
public function fetchTile($url){ public function fetchTile($url)
{
if ($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached; if ($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached;
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@ -299,13 +311,15 @@ Class staticMapLite {
} }
public function copyrightNotice(){ public function copyrightNotice()
{
$logoImg = imagecreatefrompng($this->osmLogo); $logoImg = imagecreatefrompng($this->osmLogo);
imagecopy($this->image, $logoImg, imagesx($this->image) - imagesx($logoImg), imagesy($this->image) - imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg)); 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'); header('Content-Type: image/png');
$expires = 60 * 60 * 24 * 14; $expires = 60 * 60 * 24 * 14;
header("Pragma: public"); header("Pragma: public");
@ -313,14 +327,16 @@ Class staticMapLite {
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
} }
public function makeMap(){ public function makeMap()
{
$this->initCoords(); $this->initCoords();
$this->createBaseMap(); $this->createBaseMap();
if (count($this->markers)) $this->placeMarkers(); if (count($this->markers)) $this->placeMarkers();
if ($this->osmLogo) $this->copyrightNotice(); if ($this->osmLogo) $this->copyrightNotice();
} }
public function showMap(){ public function showMap()
{
$this->parseParams(); $this->parseParams();
if ($this->useMapCache) { if ($this->useMapCache) {
// use map cache, so check cache for map // use map cache, so check cache for map
@ -354,5 +370,3 @@ Class staticMapLite {
$map = new staticMapLite(); $map = new staticMapLite();
print $map->showMap(); print $map->showMap();
?>