2009-09-18 10:06:55 +00:00
|
|
|
<?php
|
|
|
|
|
2018-02-11 18:28:06 +00:00
|
|
|
namespace StaticMapLite\Printer;
|
2014-10-31 20:52:47 +00:00
|
|
|
|
2017-05-26 15:24:07 +00:00
|
|
|
use StaticMapLite\Canvas\Canvas;
|
2017-06-08 21:29:04 +00:00
|
|
|
use StaticMapLite\CanvasTilePainter\CanvasTilePainter;
|
2017-09-29 14:20:00 +00:00
|
|
|
use StaticMapLite\Element\Marker\AbstractMarker;
|
2017-05-26 15:28:30 +00:00
|
|
|
use StaticMapLite\Element\Polyline\Polyline;
|
2017-09-30 17:45:24 +00:00
|
|
|
use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter;
|
2017-06-08 18:36:08 +00:00
|
|
|
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
|
2017-05-26 15:04:30 +00:00
|
|
|
use StaticMapLite\TileResolver\CachedTileResolver;
|
2017-05-26 14:32:08 +00:00
|
|
|
|
2018-02-11 18:26:56 +00:00
|
|
|
class Printer extends AbstractPrinter
|
2017-05-26 09:56:16 +00:00
|
|
|
{
|
2014-10-31 20:52:47 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->zoom = 0;
|
|
|
|
$this->lat = 0;
|
|
|
|
$this->lon = 0;
|
|
|
|
$this->width = 500;
|
|
|
|
$this->height = 350;
|
|
|
|
$this->maptype = $this->tileDefaultSrc;
|
2017-05-26 14:32:08 +00:00
|
|
|
|
2017-05-26 15:04:30 +00:00
|
|
|
$this->tileResolver = new CachedTileResolver();
|
2017-05-26 15:18:49 +00:00
|
|
|
$this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]);
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 14:20:00 +00:00
|
|
|
public function addMarker(AbstractMarker $marker): Printer
|
2014-10-31 20:52:47 +00:00
|
|
|
{
|
2017-05-26 15:11:00 +00:00
|
|
|
$this->markers[] = $marker;
|
2017-05-26 14:16:40 +00:00
|
|
|
|
|
|
|
return $this;
|
2014-12-14 13:52:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 15:28:30 +00:00
|
|
|
public function addPolyline(Polyline $polyline): Printer
|
2014-12-14 13:52:49 +00:00
|
|
|
{
|
2017-05-26 15:28:30 +00:00
|
|
|
$this->polylines[] = $polyline;
|
2017-05-26 14:16:40 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setCenter(float $latitude, float $longitude): Printer
|
|
|
|
{
|
|
|
|
$this->lat = $latitude;
|
|
|
|
$this->lon = $longitude;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSize(int $width, int $height): Printer
|
|
|
|
{
|
|
|
|
$this->width = $width;
|
|
|
|
$this->height = $height;
|
|
|
|
|
|
|
|
if ($this->width > $this->maxWidth) {
|
|
|
|
$this->width = $this->maxWidth;
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
2017-05-26 14:16:40 +00:00
|
|
|
|
|
|
|
if ($this->height > $this->maxHeight) {
|
|
|
|
$this->height = $this->maxHeight;
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
2017-05-26 14:16:40 +00:00
|
|
|
|
|
|
|
return $this;
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
public function setZoom(int $zoom): Printer
|
2014-12-14 13:52:49 +00:00
|
|
|
{
|
2017-05-26 14:16:40 +00:00
|
|
|
$this->zoom = $zoom;
|
2014-12-14 13:52:49 +00:00
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
if ($this->zoom > 18) {
|
|
|
|
$this->zoom = 18;
|
|
|
|
}
|
2014-12-14 13:52:49 +00:00
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
return $this;
|
|
|
|
}
|
2017-05-26 09:56:16 +00:00
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
public function setMapType(string $mapType): Printer
|
|
|
|
{
|
|
|
|
$this->maptype = $mapType;
|
|
|
|
|
2017-05-26 15:18:49 +00:00
|
|
|
$this->tileResolver->setTileLayerUrl($this->tileSrcUrl[$this->maptype]);
|
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
return $this;
|
2014-12-14 13:52:49 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 20:52:47 +00:00
|
|
|
public function initCoords()
|
|
|
|
{
|
2017-05-26 14:21:41 +00:00
|
|
|
$this->centerX = Util::lonToTile($this->lon, $this->zoom);
|
|
|
|
$this->centerY = Util::latToTile($this->lat, $this->zoom);
|
|
|
|
|
2014-10-31 20:52:47 +00:00
|
|
|
$this->offsetX = floor((floor($this->centerX) - $this->centerX) * $this->tileSize);
|
|
|
|
$this->offsetY = floor((floor($this->centerY) - $this->centerY) * $this->tileSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createBaseMap()
|
|
|
|
{
|
2017-06-08 18:36:08 +00:00
|
|
|
$this->canvas = new Canvas(
|
|
|
|
$this->width,
|
|
|
|
$this->height,
|
|
|
|
$this->zoom,
|
|
|
|
$this->centerX,
|
|
|
|
$this->centerY
|
|
|
|
);
|
2017-05-26 15:24:07 +00:00
|
|
|
|
2017-06-08 21:29:04 +00:00
|
|
|
$ctp = new CanvasTilePainter();
|
|
|
|
$ctp
|
|
|
|
->setCanvas($this->canvas)
|
|
|
|
->setTileResolver($this->tileResolver)
|
|
|
|
->paint()
|
|
|
|
;
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function placeMarkers()
|
|
|
|
{
|
2017-09-30 17:45:24 +00:00
|
|
|
$printer = new ExtraMarkerPrinter();
|
2014-10-31 20:52:47 +00:00
|
|
|
|
2017-06-08 18:39:01 +00:00
|
|
|
foreach ($this->markers as $marker) {
|
|
|
|
$printer
|
|
|
|
->setMarker($marker)
|
|
|
|
->paint($this->canvas)
|
|
|
|
;
|
|
|
|
}
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 12:58:47 +00:00
|
|
|
public function placePolylines()
|
|
|
|
{
|
2017-06-08 18:36:08 +00:00
|
|
|
$printer = new PolylinePrinter();
|
|
|
|
|
2017-05-26 15:28:30 +00:00
|
|
|
/** @var Polyline $polyline */
|
2017-05-26 12:58:47 +00:00
|
|
|
foreach ($this->polylines as $polyline) {
|
2017-06-08 18:36:08 +00:00
|
|
|
$printer
|
|
|
|
->setPolyline($polyline)
|
|
|
|
->paint($this->canvas)
|
|
|
|
;
|
2017-05-26 12:58:47 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-31 20:52:47 +00:00
|
|
|
|
|
|
|
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);
|
2017-06-08 20:55:20 +00:00
|
|
|
imagecopy($this->canvas->getImage(), $logoImg, imagesx($this->canvas->getImage()) - imagesx($logoImg), imagesy($this->canvas->getImage()) - imagesy($logoImg), 0, 0, imagesx($logoImg), imagesy($logoImg));
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function makeMap()
|
|
|
|
{
|
|
|
|
$this->initCoords();
|
|
|
|
$this->createBaseMap();
|
2017-09-30 16:51:39 +00:00
|
|
|
|
|
|
|
if (count($this->polylines)) {
|
|
|
|
$this->placePolylines();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->markers)) {
|
|
|
|
$this->placeMarkers();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->osmLogo) {
|
|
|
|
$this->copyrightNotice();
|
|
|
|
}
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function showMap()
|
|
|
|
{
|
|
|
|
if ($this->useMapCache) {
|
|
|
|
// use map cache, so check cache for map
|
|
|
|
if (!$this->checkMapCache()) {
|
|
|
|
// map is not in cache, needs to be build
|
|
|
|
$this->makeMap();
|
|
|
|
$this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777);
|
2017-05-26 15:24:07 +00:00
|
|
|
imagepng($this->canvas->getImage(), $this->mapCacheIDToFilename(), 9);
|
2014-10-31 20:52:47 +00:00
|
|
|
$this->sendHeader();
|
|
|
|
if (file_exists($this->mapCacheIDToFilename())) {
|
|
|
|
return file_get_contents($this->mapCacheIDToFilename());
|
|
|
|
} else {
|
2017-05-26 15:24:07 +00:00
|
|
|
return imagepng($this->canvas->getImage());
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// map is in cache
|
|
|
|
$this->sendHeader();
|
|
|
|
return file_get_contents($this->mapCacheIDToFilename());
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// no cache, make map, send headers and deliver png
|
|
|
|
$this->makeMap();
|
|
|
|
$this->sendHeader();
|
2017-05-26 15:24:07 +00:00
|
|
|
return imagepng($this->canvas->getImage());
|
2014-10-31 20:52:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 15:04:30 +00:00
|
|
|
|
|
|
|
public function mkdir_recursive($pathname, $mode)
|
|
|
|
{
|
|
|
|
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
|
|
|
|
return is_dir($pathname) || @mkdir($pathname, $mode);
|
|
|
|
}
|
2009-09-18 10:06:55 +00:00
|
|
|
}
|