This commit is contained in:
Malte Hübner 2017-05-26 17:04:30 +02:00
parent b0fe549018
commit ecf28c9888
3 changed files with 69 additions and 44 deletions

View File

@ -2,6 +2,7 @@
namespace StaticMapLite; namespace StaticMapLite;
use StaticMapLite\TileResolver\CachedTileResolver;
use StaticMapLite\TileResolver\TileResolver; use StaticMapLite\TileResolver\TileResolver;
class Printer class Printer
@ -19,8 +20,8 @@ class Printer
); );
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( protected $markerPrototypes = array(
// found at http://www.mapito.net/map-marker-icons.html // found at http://www.mapito.net/map-marker-icons.html
@ -55,7 +56,7 @@ class Printer
protected $useTileCache = true; protected $useTileCache = true;
protected $tileCacheBaseDir = '../cache/tiles';
protected $useMapCache = false; protected $useMapCache = false;
protected $mapCacheBaseDir = '../cache/maps'; protected $mapCacheBaseDir = '../cache/maps';
@ -78,7 +79,7 @@ class Printer
$this->height = 350; $this->height = 350;
$this->maptype = $this->tileDefaultSrc; $this->maptype = $this->tileDefaultSrc;
$this->tileResolver = new TileResolver(); $this->tileResolver = new CachedTileResolver();
} }
public function addMarker(string $markerType, float $latitude, float $longitude): Printer public function addMarker(string $markerType, float $latitude, float $longitude): Printer
@ -195,6 +196,7 @@ class Printer
$markerFilename = ''; $markerFilename = '';
$markerShadow = ''; $markerShadow = '';
$matches = false; $matches = false;
// check for marker type, get settings from markerPrototypes // check for marker type, get settings from markerPrototypes
if ($markerType) { if ($markerType) {
foreach ($this->markerPrototypes as $markerPrototype) { foreach ($this->markerPrototypes as $markerPrototype) {
@ -245,7 +247,6 @@ class Printer
// copy marker on basemap above shadow // copy marker on basemap above shadow
imagecopy($this->image, $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY), imagecopy($this->image, $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg)); 0, 0, imagesx($markerImg), imagesy($markerImg));
}; };
} }
@ -296,19 +297,6 @@ class Printer
} }
} }
public function tileUrlToFilename($url)
{
return $this->tileCacheBaseDir . "/" . str_replace(array('http://'), '', $url);
}
public function checkTileCache($url)
{
$filename = $this->tileUrlToFilename($url);
if (file_exists($filename)) {
return file_get_contents($filename);
}
}
public function checkMapCache() public function checkMapCache()
{ {
$this->mapCacheID = md5($this->serializeParams()); $this->mapCacheID = md5($this->serializeParams());
@ -329,20 +317,6 @@ class Printer
return $this->mapCacheFile . "." . $this->mapCacheExtension; 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 writeTileToCache($url, $data)
{
$filename = $this->tileUrlToFilename($url);
$this->mkdir_recursive(dirname($filename), 0777);
file_put_contents($filename, $data);
}
public function fetchTile($url) public function fetchTile($url)
{ {
return $this->tileResolver->fetch($url); return $this->tileResolver->fetch($url);
@ -402,4 +376,10 @@ class Printer
} }
} }
public function mkdir_recursive($pathname, $mode)
{
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
}
} }

View File

@ -4,22 +4,55 @@ namespace StaticMapLite\TileResolver;
class CachedTileResolver extends TileResolver class CachedTileResolver extends TileResolver
{ {
protected $tileCacheBaseDir = '../cache/tiles';
public function fetch(string $url): string public function fetch(string $url): string
{ {
/* $cachedTile = $this->checkTileCache($url);
if ($this->useTileCache && ($cached = $this->checkTileCache($url))) return $cached;
$ch = curl_init(); if ($cachedTile) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); return $cachedTile;
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0"); }
curl_setopt($ch, CURLOPT_URL, $url);
$tile = curl_exec($ch); $tile = parent::fetch($url);
curl_close($ch);
if ($tile && $this->useTileCache) { if ($tile) {
$this->writeTileToCache($url, $tile); $this->writeTileToCache($url, $tile);
} }
return $tile;
*/
throw new \Exception('Not implemented'); return $tile;
}
public function tileUrlToFilename(string $url): string
{
return $this->tileCacheBaseDir . '/' . str_replace(['http://', 'https://'], '', $url);
}
public function checkTileCache(string $url)
{
$filename = $this->tileUrlToFilename($url);
if (file_exists($filename)) {
return file_get_contents($filename);
}
return false;
}
public function writeTileToCache($url, $data): CachedTileResolver
{
$filename = $this->tileUrlToFilename($url);
$this->mkdir_recursive(dirname($filename), 0777);
file_put_contents($filename, $data);
return $this;
}
public function mkdir_recursive($pathname, $mode): bool
{
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode);
} }
} }

View File

@ -41,4 +41,16 @@ $printer
->setMapType($_GET['maptype']) ->setMapType($_GET['maptype'])
; ;
$markers = $_GET['markers'];
if ($markers) {
$markerList = explode('|', $markers);
foreach ($markerList as $marker) {
list($markerLatitude, $markerLongitude, $markerType) = explode(',', $marker);
$printer->addMarker($markerType, $markerLatitude, $markerLongitude);
}
}
print $printer->showMap(); print $printer->showMap();