-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DrawRectangleModifier::class
- Loading branch information
1 parent
3458551
commit 397c944
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |