From 322728cf166776781c9a11d9c3f1ce3b66dc98a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Hu=CC=88bner?= Date: Wed, 14 Feb 2018 09:44:47 +0100 Subject: [PATCH] WIP. --- src/MapCache/MapCache.php | 51 +++++++++++++++++++++++-- src/MapCache/MapCacheInterface.php | 15 ++++++++ src/Printer/AbstractPrinter.php | 30 ++++++++++----- src/Printer/Printer.php | 60 +++++++----------------------- src/Printer/PrinterInterface.php | 8 ++-- 5 files changed, 100 insertions(+), 64 deletions(-) create mode 100644 src/MapCache/MapCacheInterface.php diff --git a/src/MapCache/MapCache.php b/src/MapCache/MapCache.php index 3c92304..2eddf97 100644 --- a/src/MapCache/MapCache.php +++ b/src/MapCache/MapCache.php @@ -2,6 +2,7 @@ namespace StaticMapLite\MapCache; +use StaticMapLite\Canvas\CanvasInterface; use StaticMapLite\Printer\PrinterInterface; class MapCache @@ -31,9 +32,10 @@ class MapCache public function checkMapCache(): bool { + return false; $this->mapCacheID = md5($this->serializeParams()); - $filename = $this->mapCacheIDToFilename(); + $filename = $this->getFilename(); return file_exists($filename); } @@ -42,8 +44,8 @@ class MapCache { return join('&', [ $this->printer->getZoom(), - $this->printer->getCenterLatitude(), - $this->printer->getCenterLongitude(), + $this->printer->getLatitude(), + $this->printer->getLongitude(), $this->printer->getWidth(), $this->printer->getHeight(), serialize($this->printer->getMarkers()), @@ -55,11 +57,52 @@ class MapCache public function mapCacheIDToFilename() { 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 = sprintf( + '%s/%s/%s/cache_%/%s/%s', + $this->mapCacheBaseDir, + $this->printer->getMaptype(), + $this->printer->getZoom(), + substr($this->mapCacheID, 0, 2), + substr($this->mapCacheID, 2, 2), + substr($this->mapCacheID, 4) + ); } $filename = sprintf('%s.%s', $this->mapCacheFile, $this->mapCacheExtension); return $filename; } + + public function cache(CanvasInterface $canvas) + { + $this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777); + imagepng($canvas->getImage(), $this->mapCacheIDToFilename(), 9); + $this->sendHeader(); + if (file_exists($this->mapCacheIDToFilename())) { + return file_get_contents($this->mapCacheIDToFilename()); + } else { + return imagepng($canvas->getImage()); + } + } + + 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 getFilename(): string + { + return $this->mapCacheIDToFilename(); + } + + public function sendHeader() + { + header('Content-Type: image/png'); + $expires = 60 * 60 * 24 * 14; + header("Pragma: public"); + header("Cache-Control: maxage=" . $expires); + header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT'); + } + } diff --git a/src/MapCache/MapCacheInterface.php b/src/MapCache/MapCacheInterface.php new file mode 100644 index 0000000..dd9f9b1 --- /dev/null +++ b/src/MapCache/MapCacheInterface.php @@ -0,0 +1,15 @@ +lat; + return $this->latitude; } - public function setLat(float $lat): PrinterInterface + public function setLatitude(float $latitude): PrinterInterface { - $this->lat = $lat; + $this->latitude = $latitude; return $this; } - public function getLon(): float + public function getLongitude(): float { - return $this->lon; + return $this->longitude; } - public function setLon(float $lon): PrinterInterface + public function setLongitude(float $longitude): PrinterInterface { - $this->lon = $lon; + $this->longitude = $longitude; return $this; } diff --git a/src/Printer/Printer.php b/src/Printer/Printer.php index e9eb14b..2345998 100644 --- a/src/Printer/Printer.php +++ b/src/Printer/Printer.php @@ -8,6 +8,7 @@ use StaticMapLite\Element\Marker\AbstractMarker; use StaticMapLite\Element\Polyline\Polyline; use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter; use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter; +use StaticMapLite\MapCache\MapCache; use StaticMapLite\TileResolver\CachedTileResolver; use StaticMapLite\Util; @@ -16,12 +17,13 @@ class Printer extends AbstractPrinter public function __construct() { $this->zoom = 0; - $this->lat = 0; - $this->lon = 0; + $this->latitude = 0; + $this->longitude = 0; $this->width = 500; $this->height = 350; $this->maptype = $this->tileDefaultSrc; + $this->mapCache = new MapCache($this); $this->tileResolver = new CachedTileResolver(); $this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]); } @@ -42,8 +44,8 @@ class Printer extends AbstractPrinter public function setCenter(float $latitude, float $longitude): Printer { - $this->lat = $latitude; - $this->lon = $longitude; + $this->latitude = $latitude; + $this->longitude = $longitude; return $this; } @@ -64,7 +66,7 @@ class Printer extends AbstractPrinter return $this; } - public function setZoom(int $zoom): AbstractPrinter + public function setZoom(int $zoom): PrinterInterface { $this->zoom = $zoom; @@ -75,7 +77,7 @@ class Printer extends AbstractPrinter return $this; } - public function setMapType(string $mapType): AbstractPrinter + public function setMapType(string $mapType): PrinterInterface { $this->maptype = $mapType; @@ -86,8 +88,8 @@ class Printer extends AbstractPrinter public function initCoords() { - $this->centerX = Util::lonToTile($this->lon, $this->zoom); - $this->centerY = Util::latToTile($this->lat, $this->zoom); + $this->centerX = Util::lonToTile($this->longitude, $this->zoom); + $this->centerY = Util::latToTile($this->latitude, $this->zoom); $this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize); $this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize); @@ -136,32 +138,11 @@ class Printer extends AbstractPrinter } } - public function checkMapCache() - { - $this->mapCacheID = md5($this->serializeParams()); - $filename = $this->mapCacheIDToFilename(); - if (file_exists($filename)) return true; - } - - public function serializeParams() - { - return join("&", array($this->zoom, $this->lat, $this->lon, $this->width, $this->height, serialize($this->markers), $this->maptype)); - } - - public function mapCacheIDToFilename() - { - 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); - } - return $this->mapCacheFile . "." . $this->mapCacheExtension; - } - public function copyrightNotice() { $logoImg = imagecreatefrompng($this->osmLogo); imagecopy($this->canvas->getImage(), $logoImg, imagesx($this->canvas->getImage()) - imagesx($logoImg), imagesy($this->canvas->getImage()) - imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg)); } - public function sendHeader() { header('Content-Type: image/png'); @@ -191,23 +172,16 @@ class Printer extends AbstractPrinter public function showMap() { - if ($this->useMapCache) { + if ($this->mapCache) { // use map cache, so check cache for map - if (!$this->checkMapCache()) { + if (!$this->mapCache->checkMapCache()) { // map is not in cache, needs to be build $this->makeMap(); - $this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777); - imagepng($this->canvas->getImage(), $this->mapCacheIDToFilename(), 9); - $this->sendHeader(); - if (file_exists($this->mapCacheIDToFilename())) { - return file_get_contents($this->mapCacheIDToFilename()); - } else { - return imagepng($this->canvas->getImage()); - } + $this->mapCache->cache($this->canvas); } else { // map is in cache $this->sendHeader(); - return file_get_contents($this->mapCacheIDToFilename()); + return file_get_contents($this->mapCache->getFilename()); } } else { @@ -218,10 +192,4 @@ class Printer extends AbstractPrinter } } - - public function mkdir_recursive($pathname, $mode) - { - is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); - return is_dir($pathname) || @mkdir($pathname, $mode); - } } diff --git a/src/Printer/PrinterInterface.php b/src/Printer/PrinterInterface.php index b2be518..3863458 100644 --- a/src/Printer/PrinterInterface.php +++ b/src/Printer/PrinterInterface.php @@ -37,11 +37,11 @@ interface PrinterInterface public function getZoom(): int; public function setZoom(int $zoom): PrinterInterface; - public function getLat(): float; - public function setLat(float $lat): PrinterInterface; + public function getLatitude(): float; + public function setLatitude(float $lat): PrinterInterface; - public function getLon(): float; - public function setLon(float $lon): PrinterInterface; + public function getLongitude(): float; + public function setLongitude(float $lon): PrinterInterface; public function getWidth(): int; public function setWidth($width): PrinterInterface;