From bd9e686cf068191678aa86568891a4086e770725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Hu=CC=88bner?= Date: Thu, 8 Jun 2017 20:36:08 +0200 Subject: [PATCH] Much WIP. --- src/Canvas/Canvas.php | 38 ++++- src/ElementPrinter/Marker/MarkerPrinter.php | 118 ++++++++++++++++ .../Polyline/PolylinePrinter.php | 67 +++++++++ src/Printer.php | 133 ++---------------- 4 files changed, 237 insertions(+), 119 deletions(-) create mode 100644 src/ElementPrinter/Marker/MarkerPrinter.php create mode 100644 src/ElementPrinter/Polyline/PolylinePrinter.php diff --git a/src/Canvas/Canvas.php b/src/Canvas/Canvas.php index b22ed48..e822816 100644 --- a/src/Canvas/Canvas.php +++ b/src/Canvas/Canvas.php @@ -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; + } } diff --git a/src/ElementPrinter/Marker/MarkerPrinter.php b/src/ElementPrinter/Marker/MarkerPrinter.php new file mode 100644 index 0000000..ad8e083 --- /dev/null +++ b/src/ElementPrinter/Marker/MarkerPrinter.php @@ -0,0 +1,118 @@ + 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 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->getCenterY() - Util::lonToTile($this->marker->getLongitude(), $this->zoom))); + $destY = floor(($canvas->getHeight() / 2) - $this->tileSize * ($canvas->getCenterY() - Util::latToTile($this->marker->getLatitude(), $this->zoom))); + + // 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)); + } +} \ No newline at end of file diff --git a/src/ElementPrinter/Polyline/PolylinePrinter.php b/src/ElementPrinter/Polyline/PolylinePrinter.php new file mode 100644 index 0000000..65c3043 --- /dev/null +++ b/src/ElementPrinter/Polyline/PolylinePrinter.php @@ -0,0 +1,67 @@ +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; + } +} \ No newline at end of file diff --git a/src/Printer.php b/src/Printer.php index f99853b..97fb7ea 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -5,6 +5,7 @@ namespace StaticMapLite; use StaticMapLite\Canvas\Canvas; use StaticMapLite\Element\Marker\Marker; use StaticMapLite\Element\Polyline\Polyline; +use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter; use StaticMapLite\TileResolver\CachedTileResolver; class Printer @@ -26,39 +27,9 @@ 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; @@ -157,7 +128,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); @@ -192,101 +169,21 @@ class Printer public function placeMarkers() { foreach ($this->markers as $marker) { - $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)); }; } 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, 5); - //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) + ; } }