2009-09-18 10:06:55 +00:00
|
|
|
<?php
|
|
|
|
|
2017-05-26 09:56:16 +00:00
|
|
|
namespace StaticMapLite;
|
2014-10-31 20:52:47 +00:00
|
|
|
|
2017-05-26 15:24:07 +00:00
|
|
|
use StaticMapLite\Canvas\Canvas;
|
2017-05-26 15:12:11 +00:00
|
|
|
use StaticMapLite\Element\Marker\Marker;
|
2017-05-26 15:28:30 +00:00
|
|
|
use StaticMapLite\Element\Polyline\Polyline;
|
2017-05-26 15:04:30 +00:00
|
|
|
use StaticMapLite\TileResolver\CachedTileResolver;
|
2017-05-26 14:32:08 +00:00
|
|
|
|
2017-05-26 14:06:21 +00:00
|
|
|
class Printer
|
2017-05-26 09:56:16 +00:00
|
|
|
{
|
2014-10-31 20:52:47 +00:00
|
|
|
protected $maxWidth = 1024;
|
|
|
|
protected $maxHeight = 1024;
|
|
|
|
|
2017-05-26 14:32:08 +00:00
|
|
|
protected $tileResolver = null;
|
|
|
|
|
2017-05-26 15:24:07 +00:00
|
|
|
/** @var Canvas $canvas */
|
|
|
|
protected $canvas = null;
|
|
|
|
|
2014-10-31 20:52:47 +00:00
|
|
|
protected $tileSize = 256;
|
2017-05-26 15:18:49 +00:00
|
|
|
protected $tileSrcUrl = [
|
|
|
|
'mapnik' => 'http://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
|
|
'osmarenderer' => 'http://otile1.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png',
|
|
|
|
'cycle' => 'http://a.tile.opencyclemap.org/cycle/{z}/{x}/{y}.png',
|
|
|
|
'wikimedia-intl' => 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png',
|
|
|
|
];
|
2014-10-31 20:52:47 +00:00
|
|
|
|
|
|
|
protected $tileDefaultSrc = 'mapnik';
|
2017-05-26 15:04:30 +00:00
|
|
|
protected $markerBaseDir = '../images/markers';
|
|
|
|
protected $osmLogo = '../images/osm_logo.png';
|
2014-10-31 20:52:47 +00:00
|
|
|
|
|
|
|
protected $markerPrototypes = array(
|
|
|
|
// found at http://www.mapito.net/map-marker-icons.html
|
|
|
|
'lighblue' => array('regex' => '/^lightblue([0-9]+)$/',
|
|
|
|
'extension' => '.png',
|
|
|
|
'shadow' => false,
|
|
|
|
'offsetImage' => '0,-19',
|
2017-05-26 15:28:30 +00:00
|
|
|
'offsetShadow' => false,
|
2014-10-31 20:52:47 +00:00
|
|
|
),
|
|
|
|
// openlayers std markers
|
|
|
|
'ol-marker' => array('regex' => '/^ol-marker(|-blue|-gold|-green)+$/',
|
|
|
|
'extension' => '.png',
|
|
|
|
'shadow' => '../marker_shadow.png',
|
|
|
|
'offsetImage' => '-10,-25',
|
2017-05-26 15:28:30 +00:00
|
|
|
'offsetShadow' => '-1,-13',
|
2014-10-31 20:52:47 +00:00
|
|
|
),
|
|
|
|
// taken from http://www.visual-case.it/cgi-bin/vc/GMapsIcons.pl
|
|
|
|
'ylw' => array('regex' => '/^(pink|purple|red|ltblu|ylw)-pushpin$/',
|
|
|
|
'extension' => '.png',
|
|
|
|
'shadow' => '../marker_shadow.png',
|
|
|
|
'offsetImage' => '-10,-32',
|
2017-05-26 15:28:30 +00:00
|
|
|
'offsetShadow' => '-1,-13',
|
2014-12-14 13:52:49 +00:00
|
|
|
),
|
|
|
|
// http://svn.openstreetmap.org/sites/other/StaticMap/symbols/0.png
|
|
|
|
'ojw' => array('regex' => '/^bullseye$/',
|
|
|
|
'extension' => '.png',
|
2014-12-14 14:11:49 +00:00
|
|
|
'shadow' => false,
|
|
|
|
'offsetImage' => '-20,-20',
|
2017-05-26 15:28:30 +00:00
|
|
|
'offsetShadow' => false,
|
|
|
|
),
|
2014-10-31 20:52:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
protected $useTileCache = true;
|
2017-05-26 15:04:30 +00:00
|
|
|
|
2017-05-26 12:58:47 +00:00
|
|
|
protected $useMapCache = false;
|
2014-10-31 20:52:47 +00:00
|
|
|
protected $mapCacheBaseDir = '../cache/maps';
|
|
|
|
protected $mapCacheID = '';
|
|
|
|
protected $mapCacheFile = '';
|
|
|
|
protected $mapCacheExtension = 'png';
|
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
protected $zoom, $lat, $lon, $width, $height, $image, $maptype;
|
2014-10-31 20:52:47 +00:00
|
|
|
protected $centerX, $centerY, $offsetX, $offsetY;
|
|
|
|
|
2017-05-26 14:16:40 +00:00
|
|
|
protected $markers = [];
|
|
|
|
protected $polylines = [];
|
|
|
|
|
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-05-26 15:11:00 +00:00
|
|
|
public function addMarker(Marker $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-05-26 15:24:07 +00:00
|
|
|
$this->canvas = new Canvas($this->width, $this->height);
|
|
|
|
|
2014-10-31 20:52:47 +00:00
|
|
|
$startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
|
|
|
|
$startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
|
|
|
|
$endX = ceil($this->centerX + ($this->width / $this->tileSize) / 2);
|
|
|
|
$endY = ceil($this->centerY + ($this->height / $this->tileSize) / 2);
|
|
|
|
$this->offsetX = -floor(($this->centerX - floor($this->centerX)) * $this->tileSize);
|
|
|
|
$this->offsetY = -floor(($this->centerY - floor($this->centerY)) * $this->tileSize);
|
|
|
|
$this->offsetX += floor($this->width / 2);
|
|
|
|
$this->offsetY += floor($this->height / 2);
|
|
|
|
$this->offsetX += floor($startX - floor($this->centerX)) * $this->tileSize;
|
|
|
|
$this->offsetY += floor($startY - floor($this->centerY)) * $this->tileSize;
|
|
|
|
|
|
|
|
for ($x = $startX; $x <= $endX; $x++) {
|
|
|
|
for ($y = $startY; $y <= $endY; $y++) {
|
2017-05-26 15:18:49 +00:00
|
|
|
$tileData = $this->tileResolver->fetch($this->zoom, $x, $y);
|
|
|
|
|
2014-10-31 20:52:47 +00:00
|
|
|
if ($tileData) {
|
|
|
|
$tileImage = imagecreatefromstring($tileData);
|
|
|
|
} else {
|
|
|
|
$tileImage = imagecreate($this->tileSize, $this->tileSize);
|
|
|
|
$color = imagecolorallocate($tileImage, 255, 255, 255);
|
|
|
|
@imagestring($tileImage, 1, 127, 127, 'err', $color);
|
|
|
|
}
|
|
|
|
$destX = ($x - $startX) * $this->tileSize + $this->offsetX;
|
|
|
|
$destY = ($y - $startY) * $this->tileSize + $this->offsetY;
|
2017-05-26 15:24:07 +00:00
|
|
|
imagecopy($this->canvas->getImage(), $tileImage, $destX, $destY, 0, 0, $this->tileSize, $this->tileSize);
|
2014-10-31 20:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function placeMarkers()
|
|
|
|
{
|
|
|
|
foreach ($this->markers as $marker) {
|
|
|
|
$markerFilename = '';
|
|
|
|
$markerShadow = '';
|
|
|
|
$matches = false;
|
2017-05-26 15:04:30 +00:00
|
|
|
|
2014-10-31 20:52:47 +00:00
|
|
|
// check for marker type, get settings from markerPrototypes
|
2017-05-26 15:11:00 +00:00
|
|
|
if ($marker->getMarkerType()) {
|
2014-10-31 20:52:47 +00:00
|
|
|
foreach ($this->markerPrototypes as $markerPrototype) {
|
2017-05-26 15:11:00 +00:00
|
|
|
if (preg_match($markerPrototype['regex'], $marker->getMarkerType(), $matches)) {
|
2014-10-31 20:52:47 +00:00
|
|
|
$markerFilename = $matches[0] . $markerPrototype['extension'];
|
|
|
|
if ($markerPrototype['offsetImage']) {
|
|
|
|
list($markerImageOffsetX, $markerImageOffsetY) = explode(",", $markerPrototype['offsetImage']);
|
|
|
|
}
|
|
|
|
$markerShadow = $markerPrototype['shadow'];
|
|
|
|
if ($markerShadow) {
|
|
|
|
list($markerShadowOffsetX, $markerShadowOffsetY) = explode(",", $markerPrototype['offsetShadow']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check required files or set default
|
|
|
|
if ($markerFilename == '' || !file_exists($this->markerBaseDir . '/' . $markerFilename)) {
|
|
|
|
$markerIndex++;
|
|
|
|
$markerFilename = 'lightblue' . $markerIndex . '.png';
|
|
|
|
$markerImageOffsetX = 0;
|
|
|
|
$markerImageOffsetY = -19;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create img resource
|
|
|
|
if (file_exists($this->markerBaseDir . '/' . $markerFilename)) {
|
|
|
|
$markerImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerFilename);
|
|
|
|
} else {
|
|
|
|
$markerImg = imagecreatefrompng($this->markerBaseDir . '/lightblue1.png');
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for shadow + create shadow recource
|
|
|
|
if ($markerShadow && file_exists($this->markerBaseDir . '/' . $markerShadow)) {
|
|
|
|
$markerShadowImg = imagecreatefrompng($this->markerBaseDir . '/' . $markerShadow);
|
|
|
|
}
|
|
|
|
|
|
|
|
// calc position
|
2017-05-26 15:11:00 +00:00
|
|
|
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($marker->getLongitude(), $this->zoom)));
|
|
|
|
$destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($marker->getLatitude(), $this->zoom)));
|
2014-10-31 20:52:47 +00:00
|
|
|
|
|
|
|
// copy shadow on basemap
|
|
|
|
if ($markerShadow && $markerShadowImg) {
|
2017-05-26 15:24:07 +00:00
|
|
|
imagecopy($this->canvas->getImage(), $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
|
2014-10-31 20:52:47 +00:00
|
|
|
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy marker on basemap above shadow
|
2017-05-26 15:24:07 +00:00
|
|
|
imagecopy($this->canvas->getImage(), $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
|
2014-10-31 20:52:47 +00:00
|
|
|
0, 0, imagesx($markerImg), imagesy($markerImg));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-26 12:58:47 +00:00
|
|
|
public function placePolylines()
|
|
|
|
{
|
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-05-26 15:28:30 +00:00
|
|
|
$polylineList = \Polyline::decode($polyline->getPolyline());
|
2017-05-26 12:58:47 +00:00
|
|
|
|
|
|
|
$sourceLatitude = null;
|
|
|
|
$sourceLongitude = null;
|
|
|
|
$destinationLatitude = null;
|
|
|
|
$destinationLongitude = null;
|
|
|
|
|
2017-05-26 15:28:30 +00:00
|
|
|
$color = imagecolorallocate($this->canvas->getImage(), $polyline->getColorRed(), $polyline->getColorGreen(), $polyline->getColorBlue());
|
2017-06-08 18:09:43 +00:00
|
|
|
imagesetthickness($this->image, 5);
|
2017-05-26 13:23:56 +00:00
|
|
|
//imageantialias($this->image, true);
|
2017-05-26 12:58:47 +00:00
|
|
|
|
|
|
|
while (!empty($polylineList)) {
|
|
|
|
if (!$sourceLatitude) {
|
|
|
|
$sourceLatitude = array_shift($polylineList);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$sourceLongitude) {
|
|
|
|
$sourceLongitude = array_shift($polylineList);
|
|
|
|
}
|
|
|
|
|
2017-05-26 14:21:41 +00:00
|
|
|
$sourceX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($sourceLongitude, $this->zoom)));
|
|
|
|
$sourceY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($sourceLatitude, $this->zoom)));
|
2017-05-26 12:58:47 +00:00
|
|
|
|
|
|
|
$destinationLatitude = array_shift($polylineList);
|
|
|
|
$destinationLongitude = array_shift($polylineList);
|
|
|
|
|
2017-05-26 14:21:41 +00:00
|
|
|
$destinationX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($destinationLongitude, $this->zoom)));
|
|
|
|
$destinationY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($destinationLatitude, $this->zoom)));
|
2017-05-26 12:58:47 +00:00
|
|
|
|
2017-05-26 15:24:07 +00:00
|
|
|
imageline($this->canvas->getImage() , $sourceX, $sourceY , $destinationX, $destinationY, $color);
|
2017-05-26 12:58:47 +00:00
|
|
|
|
|
|
|
$sourceLatitude = $destinationLatitude;
|
|
|
|
$sourceLongitude = $destinationLongitude;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-05-26 15:24:07 +00:00
|
|
|
imagecopy($this->canvas->getImage(), $logoImg, imagesx($this->image) - imagesx($logoImg), imagesy($this->image) - 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();
|
|
|
|
if (count($this->markers)) $this->placeMarkers();
|
2017-05-26 12:58:47 +00:00
|
|
|
if (count($this->polylines)) $this->placePolylines();
|
2014-10-31 20:52:47 +00:00
|
|
|
if ($this->osmLogo) $this->copyrightNotice();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|