diff --git a/src/Canvas/Canvas.php b/src/Canvas/Canvas.php index 4ec543f..0d5217c 100644 --- a/src/Canvas/Canvas.php +++ b/src/Canvas/Canvas.php @@ -2,8 +2,11 @@ namespace StaticMapLite\Canvas; +use Imagine\Image\ImageInterface; + class Canvas implements CanvasInterface { + /** @var ImageInterface $image */ protected $image = null; /** @var int $tileSize */ @@ -34,10 +37,12 @@ class Canvas implements CanvasInterface $this->centerX = $centerX; $this->centerY = $centerY; - $this->image = imagecreatetruecolor($this->width, $this->height); + $imagine = new \Imagine\Gd\Imagine(); + $size = new \Imagine\Image\Box($this->width, $this->height); + $this->image = $imagine->create($size); } - public function getImage() + public function getImage(): ImageInterface { return $this->image; } diff --git a/src/ElementPrinter/Marker/ExtraMarkerPrinter.php b/src/ElementPrinter/Marker/ExtraMarkerPrinter.php index decb6e1..78522eb 100644 --- a/src/ElementPrinter/Marker/ExtraMarkerPrinter.php +++ b/src/ElementPrinter/Marker/ExtraMarkerPrinter.php @@ -2,6 +2,12 @@ namespace StaticMapLite\ElementPrinter\Marker; +use Imagine\Gd\Font; +use Imagine\Image\Box; +use Imagine\Image\FontInterface; +use Imagine\Image\ImageInterface; +use Imagine\Image\ImagineInterface; +use Imagine\Image\Point; use StaticMapLite\Canvas\Canvas; use StaticMapLite\Element\Marker\AbstractMarker; use StaticMapLite\Element\Marker\ExtraMarker; @@ -9,6 +15,9 @@ use StaticMapLite\Util; class ExtraMarkerPrinter { + /** @var ImagineInterface $imagine */ + protected $imagine; + /** @var ExtraMarker $marker */ protected $marker = null; @@ -29,7 +38,7 @@ class ExtraMarkerPrinter public function __construct() { - + $this->imagine = new \Imagine\Gd\Imagine(); } public function setMarkerSize(float $markerSize): ExtraMarkerPrinter @@ -61,60 +70,38 @@ class ExtraMarkerPrinter $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()))); - $markerWidth = imagesx($markerImage); - $markerHeight = imagesy($markerImage); + $destX -= $markerImage->getSize()->getWidth() * $this->markerSize / 2; + $destY -= $markerImage->getSize()->getHeight() * $this->markerSize; - $destX -= $markerWidth * $this->markerSize / 2; - $destY -= $markerHeight * $this->markerSize; + $point = new Point($destX, $destY); - imagecopyresampled( - $canvas->getImage(), - $markerImage, - $destX, - $destY, - 0, - 0, - $markerWidth * $this->markerSize, - $markerHeight * $this->markerSize, - $markerWidth, - $markerHeight - ); + $markerImage->paste($canvas->getImage(), $point); return $this; } - protected function createMarker() + protected function createMarker(): ImageInterface { - $extramarkersImgUrl = __DIR__.'/../../../images/extramarkers.png'; - $extramarkers = imagecreatefrompng($extramarkersImgUrl); - - $markerImage = imagecreatetruecolor($this->baseMarkerWidth, $this->baseMarkerHeight); - $transparentColor = imagecolorallocatealpha($markerImage, 0, 0, 0, 127); - imagefill($markerImage, 0, 0, $transparentColor); + $extramarkers = $this + ->imagine + ->open(__DIR__.'/../../../images/extramarkers.png') + ; $sourceX = $this->baseMarkerWidth * $this->marker->getColor(); $sourceY = $this->baseMarkerHeight * $this->marker->getShape(); - imagecopy( - $markerImage, - $extramarkers, - 0, - 0, - $sourceX, - $sourceY, - $this->baseMarkerWidth, - $this->baseMarkerHeight - ); + $point = new Point($sourceX, $sourceY); + $box = new Box($this->baseMarkerWidth, $this->baseMarkerHeight); - $this->writeMarker($markerImage); + $markerImage = $extramarkers->crop($point, $box); + + //$this->writeMarker($markerImage); return $markerImage; } - protected function writeMarker($markerImage) + protected function writeMarker(ImageInterface $markerImage): ExtraMarkerPrinter { - $fontSize = 20; - $fontFile = __DIR__.'/../../../fonts/fontawesome-webfont.ttf'; $text = json_decode(sprintf('"&#x%s;"', $this->marker->getAwesome())); $bbox = imagettfbbox($fontSize, 0, $fontFile, $text); @@ -122,8 +109,21 @@ class ExtraMarkerPrinter $x = $bbox[0] + (imagesx($markerImage) / 2) - ($bbox[4] / 2) + 3; $y = 42; - $white = imagecolorallocate($markerImage, 255, 255, 255); + imagettftext($markerImage, $fontSize, 0, $x, $y, $white, $fontFile, $text); + + return $this; + } + + protected function getFont(ImageInterface $markerImage): FontInterface + { + $fontColor = $markerImage->palette()->color('white'); + $fontSize = 20; + $fontFilename = __DIR__.'/../../../fonts/fontawesome-webfont.ttf'; + + $font = new Font($fontFilename, $fontSize, $fontColor); + + return $font; } protected function paintShadow(Canvas $canvas): ExtraMarkerPrinter diff --git a/src/MapCache/MapCache.php b/src/MapCache/MapCache.php index 2fed3cc..87702b0 100644 --- a/src/MapCache/MapCache.php +++ b/src/MapCache/MapCache.php @@ -34,6 +34,7 @@ class MapCache public function checkMapCache(): bool { + return false; $this->mapCacheID = md5($this->serializeParams()); $filename = $this->getFilename();