From 7c6c7be2badd62d738941f79df4300aa18547179 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Fri, 5 Nov 2021 17:13:27 +0000 Subject: [PATCH] PlaceModifier --- src/Drivers/Gd/Modifiers/PlaceModifier.php | 65 +++++++++++++++++++ .../Imagick/Modifiers/PlaceModifier.php | 58 +++++++++++++++++ src/Geometry/Size.php | 15 +++++ .../Gd/Modifiers/PlaceModifierTest.php | 22 +++++++ .../Imagick/Modifiers/PlaceModifierTest.php | 22 +++++++ 5 files changed, 182 insertions(+) create mode 100644 src/Drivers/Gd/Modifiers/PlaceModifier.php create mode 100644 src/Drivers/Imagick/Modifiers/PlaceModifier.php create mode 100644 tests/Drivers/Gd/Modifiers/PlaceModifierTest.php create mode 100644 tests/Drivers/Imagick/Modifiers/PlaceModifierTest.php diff --git a/src/Drivers/Gd/Modifiers/PlaceModifier.php b/src/Drivers/Gd/Modifiers/PlaceModifier.php new file mode 100644 index 00000000..059ac16c --- /dev/null +++ b/src/Drivers/Gd/Modifiers/PlaceModifier.php @@ -0,0 +1,65 @@ +element = $element; + $this->position = $position; + $this->offset_x = $offset_x; + $this->offset_y = $offset_y; + } + + public function apply(ImageInterface $image): ImageInterface + { + $watermark = $this->decodeWatermark(); + $position = $this->getPosition($image, $watermark); + + foreach ($image as $frame) { + imagecopy( + $frame->getCore(), + $watermark->getFrame()->getCore(), + $position->getX(), + $position->getY(), + 0, + 0, + $watermark->width(), + $watermark->height() + ); + } + + return $image; + } + + protected function decodeWatermark(): Image + { + return $this->resolveDriverClass('InputHandler')->handle($this->element); + } + + protected function getPosition(Image $image, Image $watermark): Point + { + $image_size = $image->getSize()->align($this->position, $this->offset_x, $this->offset_y); + $watermark_size = $watermark->getSize()->align($this->position); + + return $image_size->relativePosition($watermark_size); + } +} diff --git a/src/Drivers/Imagick/Modifiers/PlaceModifier.php b/src/Drivers/Imagick/Modifiers/PlaceModifier.php new file mode 100644 index 00000000..e91714bc --- /dev/null +++ b/src/Drivers/Imagick/Modifiers/PlaceModifier.php @@ -0,0 +1,58 @@ +element = $element; + $this->position = $position; + $this->offset_x = $offset_x; + $this->offset_y = $offset_y; + } + + public function apply(ImageInterface $image): ImageInterface + { + $watermark = $this->decodeWatermark(); + $position = $this->getPosition($image, $watermark); + + foreach ($image as $frame) { + $frame->getCore()->compositeImage( + $watermark->getFrame()->getCore(), + Imagick::COMPOSITE_DEFAULT, + $position->getX(), + $position->getY() + ); + } + + return $image; + } + + protected function decodeWatermark(): Image + { + return $this->resolveDriverClass('InputHandler')->handle($this->element); + } + + protected function getPosition(Image $image, Image $watermark): Point + { + $image_size = $image->getSize()->align($this->position, $this->offset_x, $this->offset_y); + $watermark_size = $watermark->getSize()->align($this->position); + + return $image_size->relativePosition($watermark_size); + } +} diff --git a/src/Geometry/Size.php b/src/Geometry/Size.php index b2e4f71f..c1adef14 100644 --- a/src/Geometry/Size.php +++ b/src/Geometry/Size.php @@ -176,4 +176,19 @@ class Size implements SizeInterface return $this; } + + /** + * Calculate the relative position to another Size + * based on the pivot point settings of both sizes. + * + * @param Size $size + * @return Point + */ + public function relativePosition(Size $size): Point + { + $x = $this->getPivot()->getX() - $size->getPivot()->getX(); + $y = $this->getPivot()->getY() - $size->getPivot()->getY(); + + return new Point($x, $y); + } } diff --git a/tests/Drivers/Gd/Modifiers/PlaceModifierTest.php b/tests/Drivers/Gd/Modifiers/PlaceModifierTest.php new file mode 100644 index 00000000..bd1d3e0b --- /dev/null +++ b/tests/Drivers/Gd/Modifiers/PlaceModifierTest.php @@ -0,0 +1,22 @@ +createTestImage('test.jpg'); + $this->assertEquals('febc44', $image->pickColor(300, 25)->toHex()); + $image->modify(new PlaceModifier(__DIR__ . '/../../../images/circle.png', 'top-right', 0, 0)); + $this->assertEquals('32250d', $image->pickColor(300, 25)->toHex()); + } +} diff --git a/tests/Drivers/Imagick/Modifiers/PlaceModifierTest.php b/tests/Drivers/Imagick/Modifiers/PlaceModifierTest.php new file mode 100644 index 00000000..20e2f077 --- /dev/null +++ b/tests/Drivers/Imagick/Modifiers/PlaceModifierTest.php @@ -0,0 +1,22 @@ +createTestImage('test.jpg'); + $this->assertEquals('fdbd42', $image->pickColor(300, 25)->toHex()); + $image->modify(new PlaceModifier(__DIR__ . '/../../../images/circle.png', 'top-right', 0, 0)); + $this->assertEquals('33260d', $image->pickColor(300, 25)->toHex()); + } +}