Merge branch 'enhancement' of https://github.com/calderacc/staticmaplite into enhancement

# Conflicts:
#	src/Printer.php
This commit is contained in:
Malte Hübner 2017-09-29 16:21:41 +02:00
commit d1aa0a1f9e
6 changed files with 294 additions and 155 deletions

View File

@ -5,14 +5,25 @@ namespace StaticMapLite\Canvas;
class Canvas
{
protected $image = null;
protected $width = 0;
protected $height = 0;
public function __construct(int $width, int $height)
protected $centerX = 0;
protected $centerY = 0;
protected $zoom = 0;
public function __construct(int $width, int $height, int $zoom, int $centerX, int $centerY)
{
$this->width = $width;
$this->height = $height;
$this->zoom = $zoom;
$this->centerX = $centerX;
$this->centerY = $centerY;
$this->image = imagecreatetruecolor($this->width, $this->height);
}
@ -20,4 +31,29 @@ class Canvas
{
return $this->image;
}
public function getWidth(): int
{
return $this->width;
}
public function getHeight(): int
{
return $this->height;
}
public function getCenterX(): int
{
return $this->centerX;
}
public function getCenterY(): int
{
return $this->centerY;
}
public function getZoom(): int
{
return $this->zoom;
}
}

View File

@ -9,7 +9,7 @@ class Polyline
protected $colorGreen = 0;
protected $colorBlue = 0;
public function __construct(string $polyline, int $colorRed, int $colorGreen, int $colorBlue)
public function __construct(string $polyline, int $colorRed = 0, int $colorGreen = 0, int $colorBlue = 0)
{
$this->polyline = $polyline;

View File

@ -0,0 +1,147 @@
<?php
namespace StaticMapLite\ElementPrinter\Marker;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\Element\Marker\ExtraMarker;
use StaticMapLite\Element\Marker\Marker;
use StaticMapLite\Util;
class MarkerPrinter
{
/** @var Marker $marker */
protected $marker = null;
protected $markerBaseDir = '../images/markers';
protected $tileSize = 256;
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',
'offsetShadow' => false,
),
// openlayers std markers
'ol-marker' => array('regex' => '/^ol-marker(|-blue|-gold|-green)+$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-25',
'offsetShadow' => '-1,-13',
),
// 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',
'offsetShadow' => '-1,-13',
),
// http://svn.openstreetmap.org/sites/other/StaticMap/symbols/0.png
'ojw' => array('regex' => '/^bullseye$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '-20,-20',
'offsetShadow' => false,
),
);
public function __construct()
{
}
public function setMarker(Marker $marker): MarkerPrinter
{
$this->marker = $marker;
return $this;
}
/**
public function placeExtraMarker(ExtraMarker $extraMarker)
{
$extramarkers = imagecreatefrompng($this->markerBaseDir . '/../extramarkers.png');
$markerImage = imagecreatetruecolor(75, 100);
$trans_colour = imagecolorallocatealpha($markerImage, 0, 0, 0, 127);
imagefill($markerImage, 0, 0, $trans_colour);
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($extraMarker->getLongitude(), $this->zoom)));
$destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($extraMarker->getLatitude(), $this->zoom)));
$markerWidth = imagesx($markerImage);
$markerHeight = imagesy($markerImage);
$destX -= $markerWidth / 2;
$destY -= $markerHeight;
imagecopy($markerImage, $extramarkers, 0, 0, 0, 0, $markerWidth, $markerHeight);
imagecopy($this->canvas->getImage(), $markerImage, $destX, $destY, 0, 0, imagesx($markerImage), imagesy($markerImage));
}
**/
public function paint(Canvas $canvas): MarkerPrinter
{
$markerFilename = '';
$markerShadow = '';
$matches = false;
// check for marker type, get settings from markerPrototypes
if ($this->marker->getMarkerType()) {
foreach ($this->markerPrototypes as $markerPrototype) {
if (preg_match($markerPrototype['regex'], $this->marker->getMarkerType(), $matches)) {
$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
$destX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterX() - Util::lonToTile($this->marker->getLongitude(), $canvas->getZoom())));
$destY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($this->marker->getLatitude(), $canvas->getZoom())));
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {
imagecopy($canvas->getImage(), $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
}
// copy marker on basemap above shadow
imagecopy($canvas->getImage(), $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg));
return $this;
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace StaticMapLite\ElementPrinter\Polyline;
use StaticMapLite\Canvas\Canvas;
use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\Util;
class PolylinePrinter
{
/** @var Polyline $polyline */
protected $polyline = null;
protected $tileSize = 256;
public function __construct()
{
}
public function setPolyline(Polyline $polyline): PolylinePrinter
{
$this->polyline = $polyline;
return $this;
}
public function paint(Canvas $canvas): PolylinePrinter
{
$polylineList = \Polyline::decode($this->polyline->getPolyline());
$sourceLatitude = null;
$sourceLongitude = null;
$destinationLatitude = null;
$destinationLongitude = null;
$color = imagecolorallocate($canvas->getImage(), $this->polyline->getColorRed(), $this->polyline->getColorGreen(), $this->polyline->getColorBlue());
imagesetthickness($canvas->getImage(), 5);
//imageantialias($this->image, true);
while (!empty($polylineList)) {
if (!$sourceLatitude) {
$sourceLatitude = array_shift($polylineList);
}
if (!$sourceLongitude) {
$sourceLongitude = array_shift($polylineList);
}
$sourceX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterX() - Util::lonToTile($sourceLongitude, $canvas->getZoom())));
$sourceY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($sourceLatitude, $canvas->getZoom())));
$destinationLatitude = array_shift($polylineList);
$destinationLongitude = array_shift($polylineList);
$destinationX = floor(($canvas->getWidth() / 2) - $this->tileSize * ($canvas->getCenterX() - Util::lonToTile($destinationLongitude, $canvas->getZoom())));
$destinationY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($destinationLatitude, $canvas->getZoom())));
imageline($canvas->getImage() , $sourceX, $sourceY , $destinationX, $destinationY, $color);
$sourceLatitude = $destinationLatitude;
$sourceLongitude = $destinationLongitude;
}
return $this;
}
}

View File

@ -7,6 +7,8 @@ use StaticMapLite\Element\Marker\AbstractMarker;
use StaticMapLite\Element\Marker\ExtraMarker;
use StaticMapLite\Element\Marker\Marker;
use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\ElementPrinter\Marker\MarkerPrinter;
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
use StaticMapLite\TileResolver\CachedTileResolver;
class Printer
@ -28,44 +30,13 @@ class Printer
];
protected $tileDefaultSrc = 'mapnik';
protected $markerBaseDir = '../images/markers';
protected $osmLogo = '../images/osm_logo.png';
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',
'offsetShadow' => false,
),
// openlayers std markers
'ol-marker' => array('regex' => '/^ol-marker(|-blue|-gold|-green)+$/',
'extension' => '.png',
'shadow' => '../marker_shadow.png',
'offsetImage' => '-10,-25',
'offsetShadow' => '-1,-13',
),
// 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',
'offsetShadow' => '-1,-13',
),
// http://svn.openstreetmap.org/sites/other/StaticMap/symbols/0.png
'ojw' => array('regex' => '/^bullseye$/',
'extension' => '.png',
'shadow' => false,
'offsetImage' => '-20,-20',
'offsetShadow' => false,
),
);
protected $useTileCache = true;
protected $useMapCache = false;
protected $mapCacheBaseDir = '../cache/maps';
protected $mapCacheID = '';
@ -160,7 +131,13 @@ class Printer
public function createBaseMap()
{
$this->canvas = new Canvas($this->width, $this->height);
$this->canvas = new Canvas(
$this->width,
$this->height,
$this->zoom,
$this->centerX,
$this->centerY
);
$startX = floor($this->centerX - ($this->width / $this->tileSize) / 2);
$startY = floor($this->centerY - ($this->height / $this->tileSize) / 2);
@ -191,133 +168,28 @@ class Printer
}
}
public function placeExtraMarker(ExtraMarker $extraMarker)
{
$extramarkers = imagecreatefrompng($this->markerBaseDir . '/../extramarkers.png');
$markerImage = imagecreatetruecolor(75, 100);
$trans_colour = imagecolorallocatealpha($markerImage, 0, 0, 0, 127);
imagefill($markerImage, 0, 0, $trans_colour);
$destX = floor(($this->width / 2) - $this->tileSize * ($this->centerX - Util::lonToTile($extraMarker->getLongitude(), $this->zoom)));
$destY = floor(($this->height / 2) - $this->tileSize * ($this->centerY - Util::latToTile($extraMarker->getLatitude(), $this->zoom)));
$markerWidth = imagesx($markerImage);
$markerHeight = imagesy($markerImage);
$destX -= $markerWidth / 2;
$destY -= $markerHeight;
imagecopy($markerImage, $extramarkers, 0, 0, 0, 0, $markerWidth, $markerHeight);
imagecopy($this->canvas->getImage(), $markerImage, $destX, $destY, 0, 0, imagesx($markerImage), imagesy($markerImage));
}
public function placeMarkers()
{
$printer = new MarkerPrinter();
foreach ($this->markers as $marker) {
if ($marker instanceof ExtraMarker) {
$this->placeExtraMarker($marker);
continue;
}
$markerFilename = '';
$markerShadow = '';
$matches = false;
// check for marker type, get settings from markerPrototypes
if ($marker->getMarkerType()) {
foreach ($this->markerPrototypes as $markerPrototype) {
if (preg_match($markerPrototype['regex'], $marker->getMarkerType(), $matches)) {
$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
$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)));
// copy shadow on basemap
if ($markerShadow && $markerShadowImg) {
imagecopy($this->canvas->getImage(), $markerShadowImg, $destX + intval($markerShadowOffsetX), $destY + intval($markerShadowOffsetY),
0, 0, imagesx($markerShadowImg), imagesy($markerShadowImg));
}
// copy marker on basemap above shadow
imagecopy($this->canvas->getImage(), $markerImg, $destX + intval($markerImageOffsetX), $destY + intval($markerImageOffsetY),
0, 0, imagesx($markerImg), imagesy($markerImg));
};
$printer
->setMarker($marker)
->paint($this->canvas)
;
}
}
public function placePolylines()
{
$printer = new PolylinePrinter();
/** @var Polyline $polyline */
foreach ($this->polylines as $polyline) {
$polylineList = \Polyline::decode($polyline->getPolyline());
$sourceLatitude = null;
$sourceLongitude = null;
$destinationLatitude = null;
$destinationLongitude = null;
$color = imagecolorallocate($this->canvas->getImage(), $polyline->getColorRed(), $polyline->getColorGreen(), $polyline->getColorBlue());
imagesetthickness($this->image, 3);
//imageantialias($this->image, true);
while (!empty($polylineList)) {
if (!$sourceLatitude) {
$sourceLatitude = array_shift($polylineList);
}
if (!$sourceLongitude) {
$sourceLongitude = array_shift($polylineList);
}
$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)));
$destinationLatitude = array_shift($polylineList);
$destinationLongitude = array_shift($polylineList);
$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)));
imageline($this->canvas->getImage() , $sourceX, $sourceY , $destinationX, $destinationY, $color);
$sourceLatitude = $destinationLatitude;
$sourceLongitude = $destinationLongitude;
}
$printer
->setPolyline($polyline)
->paint($this->canvas)
;
}
}

View File

@ -29,6 +29,7 @@ require_once '../vendor/autoload.php';
use StaticMapLite\Element\Marker\ExtraMarker;
use StaticMapLite\Element\Marker\Marker;
use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\Printer;
$printer = new Printer();
@ -48,8 +49,8 @@ $markers = $_GET['markers'];
if ($markers) {
$markerList = explode('|', $markers);
foreach ($markerList as $marker) {
list($markerLatitude, $markerLongitude, $markerType) = explode(',', $marker);
foreach ($markerList as $markerData) {
list($markerLatitude, $markerLongitude, $markerType) = explode(',', $markerData);
$marker = new ExtraMarker(ExtraMarker::SHAPE_CIRCLE, ExtraMarker::COLOR_GREEN, $markerLatitude, $markerLongitude);
@ -57,4 +58,20 @@ if ($markers) {
}
}
$polylines = $_GET['polylines'];
if ($polylines) {
$polylineList = explode('|', $polylines);
foreach ($polylineList as $polylineData) {
list($polyline64String, $colorRed, $colorGreen, $colorBlue) = explode(',', $polylineData);
$polylineString = base64_decode($polyline64String);
$polyline = new Polyline($polylineString, $colorRed, $colorGreen, $colorBlue);
$printer->addPolyline($polyline);
}
}
print $printer->showMap();