From a339f86493774be700b38106d7fa4c7acf0dd9e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20Hu=CC=88bner?= Date: Sat, 10 Feb 2018 18:18:53 +0100 Subject: [PATCH] Allow to resize marker. --- .../Marker/ExtraMarkerPrinter.php | 55 +++++++++++++++---- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/src/ElementPrinter/Marker/ExtraMarkerPrinter.php b/src/ElementPrinter/Marker/ExtraMarkerPrinter.php index aff2189..b4bb2da 100644 --- a/src/ElementPrinter/Marker/ExtraMarkerPrinter.php +++ b/src/ElementPrinter/Marker/ExtraMarkerPrinter.php @@ -13,11 +13,27 @@ class ExtraMarkerPrinter /** @var ExtraMarker $marker */ protected $marker = null; + /** @var int $baseMarkerWidth */ + protected $baseMarkerWidth = 72; + + /** @var int $baseMarkerHeight */ + protected $baseMarkerHeight = 92; + + /** @var float $markerSize */ + protected $markerSize = 0.75; + public function __construct() { } + public function setMarkerSize(float $markerSize): ExtraMarkerPrinter + { + $this->markerSize = $markerSize; + + return $this; + } + public function setMarker(AbstractMarker $marker): ExtraMarkerPrinter { $this->marker = $marker; @@ -35,10 +51,21 @@ class ExtraMarkerPrinter $markerWidth = imagesx($markerImage); $markerHeight = imagesy($markerImage); - $destX -= $markerWidth / 2; - $destY -= $markerHeight; + $destX -= $markerWidth * $this->markerSize / 2; + $destY -= $markerHeight * $this->markerSize; - imagecopy($canvas->getImage(), $markerImage, $destX, $destY, 0, 0, imagesx($markerImage), imagesy($markerImage)); + imagecopyresampled( + $canvas->getImage(), + $markerImage, + $destX, + $destY, + 0, + 0, + $markerWidth * $this->markerSize, + $markerHeight * $this->markerSize, + $markerWidth, + $markerHeight + ); return $this; } @@ -48,17 +75,23 @@ class ExtraMarkerPrinter $extramarkersImgUrl = __DIR__.'/../../../images/extramarkers.png'; $extramarkers = imagecreatefrompng($extramarkersImgUrl); - $markerImage = imagecreatetruecolor(72, 92); + $markerImage = imagecreatetruecolor($this->baseMarkerWidth, $this->baseMarkerHeight); $transparentColor = imagecolorallocatealpha($markerImage, 0, 0, 0, 127); imagefill($markerImage, 0, 0, $transparentColor); - $markerWidth = imagesx($markerImage); - $markerHeight = imagesy($markerImage); + $sourceX = $this->baseMarkerWidth * $this->marker->getColor(); + $sourceY = $this->baseMarkerHeight * $this->marker->getShape(); - $sourceX = $markerWidth * $this->marker->getColor(); - $sourceY = $markerHeight * $this->marker->getShape(); - - imagecopy($markerImage, $extramarkers, 0, 0, $sourceX, $sourceY, $markerWidth, $markerHeight); + imagecopy( + $markerImage, + $extramarkers, + 0, + 0, + $sourceX, + $sourceY, + $this->baseMarkerWidth, + $this->baseMarkerHeight + ); $this->writeMarker($markerImage); @@ -78,7 +111,5 @@ class ExtraMarkerPrinter $white = imagecolorallocate($markerImage, 255, 255, 255); imagettftext($markerImage, $fontSize, 0, $x, $y, $white, $fontFile, $text); - - } }