diff --git a/readme.md b/readme.md index 0377b12..f18589b 100644 --- a/readme.md +++ b/readme.md @@ -109,6 +109,7 @@ the features that have already been implemented. | Image::drawEllipse() | ✅ | | Image::drawCircle() | ✅ | | Image::drawLine() | ✅ | +| Image::drawRectangle() | ✅ | | Image::drawPolygon() | ✅ | | Image::drawBezier() | ✅ | | Image::resolution() | ✅ | diff --git a/src/Modifiers/DrawRectangleModifier.php b/src/Modifiers/DrawRectangleModifier.php new file mode 100644 index 0000000..ba1c4e6 --- /dev/null +++ b/src/Modifiers/DrawRectangleModifier.php @@ -0,0 +1,54 @@ + $this->drawable->position()->x(), + 'y' => $this->drawable->position()->y(), + 'width' => $this->drawable->width(), + 'height' => $this->drawable->height(), + 'fill' => $this->backgroundColor()->toString(), + ]; + + if ($this->drawable->hasBorder()) { + $xmlAttributes['stroke'] = $this->borderColor()->toString(); + $xmlAttributes['stroke-width'] = $this->drawable->borderSize(); + } + + $rectangle = Driver::createShape('rect', $xmlAttributes, $image->width(), $image->height()); + + $frames = []; + foreach ($image as $frame) { + $frames[] = $frame->setNative( + $frame->native()->composite($rectangle->core()->native(), [BlendMode::OVER]) + ); + } + + $image->core()->setNative( + Core::replaceFrames($image->core()->native(), $frames) + ); + + return $image; + } +} diff --git a/tests/Unit/Modifiers/DrawRectangleModifierTest.php b/tests/Unit/Modifiers/DrawRectangleModifierTest.php new file mode 100644 index 0000000..05aee83 --- /dev/null +++ b/tests/Unit/Modifiers/DrawRectangleModifierTest.php @@ -0,0 +1,25 @@ +readTestImage('trim.png'); + $this->assertEquals('00aef0', $image->pickColor(14, 14)->toHex()); + $rectangle = new Rectangle(300, 200, new Point(14, 14)); + $rectangle->setBackgroundColor('ffffff'); + $image->modify(new DrawRectangleModifier($rectangle)); + $this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex()); + } +}