From 15264d0abd2fa7e50b9aa625cef6b72dc744efe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Hu=CC=88bner?= Date: Sun, 11 Feb 2018 18:36:14 +0100 Subject: [PATCH] Try to add shadows. --- .../Marker/ExtraMarkerPrinter.php | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/ElementPrinter/Marker/ExtraMarkerPrinter.php b/src/ElementPrinter/Marker/ExtraMarkerPrinter.php index b4bb2da..decb6e1 100644 --- a/src/ElementPrinter/Marker/ExtraMarkerPrinter.php +++ b/src/ElementPrinter/Marker/ExtraMarkerPrinter.php @@ -5,7 +5,6 @@ namespace StaticMapLite\ElementPrinter\Marker; use StaticMapLite\Canvas\Canvas; use StaticMapLite\Element\Marker\AbstractMarker; use StaticMapLite\Element\Marker\ExtraMarker; -use StaticMapLite\Element\Marker\Marker; use StaticMapLite\Util; class ExtraMarkerPrinter @@ -19,6 +18,12 @@ class ExtraMarkerPrinter /** @var int $baseMarkerHeight */ protected $baseMarkerHeight = 92; + /** @var int $baseShadowWidth */ + protected $baseShadowWidth = 21; + + /** @var int $baseShadowHeight */ + protected $baseShadowHeight = 14; + /** @var float $markerSize */ protected $markerSize = 0.75; @@ -42,6 +47,14 @@ class ExtraMarkerPrinter } public function paint(Canvas $canvas): ExtraMarkerPrinter + { + //$this->paintShadow($canvas); looks like shadows are currently broken, will fix that later + $this->paintMarker($canvas); + + return $this; + } + + protected function paintMarker(Canvas $canvas): ExtraMarkerPrinter { $markerImage = $this->createMarker(); @@ -112,4 +125,53 @@ class ExtraMarkerPrinter $white = imagecolorallocate($markerImage, 255, 255, 255); imagettftext($markerImage, $fontSize, 0, $x, $y, $white, $fontFile, $text); } + + protected function paintShadow(Canvas $canvas): ExtraMarkerPrinter + { + $shadowImage = $this->createShadow(); + + $destX = floor(($canvas->getWidth() / 2) - $canvas->getTileSize() * ($canvas->getCenterX() - Util::lonToTile($this->marker->getLongitude(), $canvas->getZoom()))); + $destY = floor(($canvas->getHeight() / 2) - $canvas->getTileSize() * ($canvas->getCenterY() - Util::latToTile($this->marker->getLatitude(), $canvas->getZoom()))); + + $destX -= $this->baseShadowWidth * $this->markerSize; + $destY -= $this->baseShadowHeight; + + imagecopyresampled( + $canvas->getImage(), + $shadowImage, + $destX, + $destY, + 0, + 0, + $this->baseShadowWidth, + $this->baseShadowHeight, + $this->baseShadowWidth, + $this->baseShadowHeight + ); + + return $this; + } + + protected function createShadow() + { + $shadowImgUrl = __DIR__.'/../../../images/marker_shadow.png'; + $shadow = imagecreatefrompng($shadowImgUrl); + + $shadowImage = imagecreatetruecolor(21, 14); + $transparentColor = imagecolorallocatealpha($shadowImage, 255, 255, 255, 0); + imagefill($shadowImage, 0, 0, $transparentColor); + + imagecopy( + $shadow, + $shadowImage, + 0, + 0, + 0, + 0, + $this->baseShadowWidth, + $this->baseShadowHeight + ); + + return $shadowImage; + } }