Try to add shadows.

This commit is contained in:
Malte Hübner 2018-02-11 18:36:14 +01:00
parent a339f86493
commit 15264d0abd
1 changed files with 63 additions and 1 deletions

View File

@ -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;
}
}