This commit is contained in:
Malte Hübner 2018-02-14 09:44:47 +01:00
parent fa8fbd06cf
commit 322728cf16
5 changed files with 100 additions and 64 deletions

View File

@ -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');
}
}

View File

@ -0,0 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: maltehuebner
* Date: 14.02.18
* Time: 09:31
*/
namespace StaticMapLite\MapCache;
interface MapCacheInterface
{
}

View File

@ -3,10 +3,15 @@
namespace StaticMapLite\Printer;
use StaticMapLite\Canvas\CanvasInterface;
use StaticMapLite\MapCache\MapCache;
use StaticMapLite\MapCache\MapCacheInterface;
use StaticMapLite\TileResolver\TileResolverInterface;
abstract class AbstractPrinter implements PrinterInterface
{
/** @var MapCacheInterface $mapCache */
protected $mapCache;
/** @var int $maxWidth */
protected $maxWidth = 1024;
@ -39,14 +44,19 @@ abstract class AbstractPrinter implements PrinterInterface
/** @var bool $useTileCache */
protected $useTileCache = true;
/** @var int $zoom */
protected $zoom;
protected $lat;
/** @var float $latitude */
protected $latitude;
protected $lon;
/** @var float $longitude */
protected $longitude;
/** @var int $width */
protected $width;
/** @var int $height */
protected $height;
protected $image;
@ -187,26 +197,26 @@ abstract class AbstractPrinter implements PrinterInterface
return $this;
}
public function getLat(): float
public function getLatitude(): float
{
return $this->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;
}

View File

@ -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);
}
}

View File

@ -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;