Skip to content

Commit

Permalink
Implement DrawRectangleModifier::class
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jan 12, 2025
1 parent 3458551 commit 397c944
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() ||
Expand Down
54 changes: 54 additions & 0 deletions src/Modifiers/DrawRectangleModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Vips\Modifiers;

use Intervention\Image\Drivers\Vips\Core;
use Intervention\Image\Drivers\Vips\Driver;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\DrawRectangleModifier as GenericDrawRectangleModifier;
use Jcupitt\Vips\BlendMode;
use Jcupitt\Vips\Exception as VipsException;

class DrawRectangleModifier extends GenericDrawRectangleModifier implements SpecializedInterface
{
/**
* {@inheritdoc}
*
* @see ModifierInterface::apply()
* @throws VipsException|RuntimeException|\RuntimeException
*/
public function apply(ImageInterface $image): ImageInterface
{
$xmlAttributes = [
'x' => $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;
}
}
25 changes: 25 additions & 0 deletions tests/Unit/Modifiers/DrawRectangleModifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Vips\Tests\Unit\Modifiers;

use Intervention\Image\Drivers\Vips\Modifiers\DrawRectangleModifier;
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Rectangle;

#[CoversClass(DrawRectangleModifier::class)]
final class DrawRectangleModifierTest extends BaseTestCase
{
public function testApply(): void
{
$image = $this->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());
}
}

0 comments on commit 397c944

Please sign in to comment.