diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index 2136c3bd..9b71d778 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -200,6 +200,14 @@ abstract class AbstractImage implements ImageInterface return $this->modify($modifier); } + public function drawPixel(int $x, int $y, $color = null): ImageInterface + { + $color = $this->handleInput($color); + $modifier = $this->resolveDriverClass('Modifiers\DrawPixelModifier', new Point($x, $y), $color); + + return $this->modify($modifier); + } + public function resize(?int $width = null, ?int $height = null): ImageInterface { return $this->modify( diff --git a/src/Drivers/Gd/Modifiers/DrawPixelModifier.php b/src/Drivers/Gd/Modifiers/DrawPixelModifier.php new file mode 100644 index 00000000..3f44a23b --- /dev/null +++ b/src/Drivers/Gd/Modifiers/DrawPixelModifier.php @@ -0,0 +1,30 @@ +getCore(), + $this->position->getX(), + $this->position->getY(), + $this->color->toInt() + ); + } + + return $image; + } +} diff --git a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php new file mode 100644 index 00000000..00f34ba4 --- /dev/null +++ b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php @@ -0,0 +1,41 @@ +setFillColor($this->getColor()->getPixel()); + $pixel->point($this->position->getX(), $this->position->getY()); + + foreach ($image as $frame) { + $frame->getCore()->drawImage($pixel); + } + + return $image; + } + + public function getColor(): Color + { + if (!is_a($this->color, Color::class)) { + throw new DecoderException('Unable to decode given pixel color.'); + } + + return $this->color; + } +} diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index f69a9825..2ba694d6 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -36,6 +36,7 @@ interface ImageInterface extends Traversable, Countable public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface; public function pad(int $width, int $height, $background = 'ffffff', string $position = 'center'): ImageInterface; public function padDown(int $width, int $height, $background = 'ffffff', string $position = 'center'): ImageInterface; + public function drawPixel(int $x, int $y, $color = null): ImageInterface; public function getWidth(): int; public function getHeight(): int; public function destroy(): void; diff --git a/tests/Drivers/Gd/Modifiers/DrawPixelModifierTest.php b/tests/Drivers/Gd/Modifiers/DrawPixelModifierTest.php new file mode 100644 index 00000000..0b552116 --- /dev/null +++ b/tests/Drivers/Gd/Modifiers/DrawPixelModifierTest.php @@ -0,0 +1,26 @@ +createTestImage('trim.png'); + $this->assertEquals('00aef0', $image->pickColor(14, 14)->toHex()); + $image->modify(new DrawPixelModifier(new Point(14, 14), new Color(16777215))); + $this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex()); + } +} diff --git a/tests/Drivers/Imagick/Modifiers/DrawPixelModifierTest.php b/tests/Drivers/Imagick/Modifiers/DrawPixelModifierTest.php new file mode 100644 index 00000000..c0f3cf54 --- /dev/null +++ b/tests/Drivers/Imagick/Modifiers/DrawPixelModifierTest.php @@ -0,0 +1,27 @@ +createTestImage('trim.png'); + $this->assertEquals('00aef0', $image->pickColor(14, 14)->toHex()); + $image->modify(new DrawPixelModifier(new Point(14, 14), new Color(new ImagickPixel('#ffffff')))); + $this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex()); + } +}