1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-24 14:32:52 +02:00

Refactor draw modifiers

This commit is contained in:
Oliver Vogel
2022-07-25 11:19:11 +02:00
parent 105e11d1d5
commit f54b18e01c
6 changed files with 34 additions and 63 deletions

View File

@@ -11,20 +11,20 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
return $image->eachFrame(function ($frame) { return $image->eachFrame(function ($frame) {
if ($this->drawable()->hasBorder()) { if ($this->ellipse()->hasBorder()) {
// slightly smaller ellipse to keep 1px bordered edges clean // slightly smaller ellipse to keep 1px bordered edges clean
if ($this->drawable()->hasBackgroundColor()) { if ($this->ellipse()->hasBackgroundColor()) {
imagefilledellipse( imagefilledellipse(
$frame->getCore(), $frame->getCore(),
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->drawable()->getWidth() - 1, $this->ellipse()->getWidth() - 1,
$this->drawable()->getHeight() - 1, $this->ellipse()->getHeight() - 1,
$this->getBackgroundColor()->toInt() $this->getBackgroundColor()->toInt()
); );
} }
imagesetthickness($frame->getCore(), $this->drawable()->getBorderSize()); imagesetthickness($frame->getCore(), $this->ellipse()->getBorderSize());
// gd's imageellipse doesn't respect imagesetthickness so i use // gd's imageellipse doesn't respect imagesetthickness so i use
// imagearc with 359.9 degrees here. // imagearc with 359.9 degrees here.
@@ -32,8 +32,8 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
$frame->getCore(), $frame->getCore(),
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->drawable()->getWidth(), $this->ellipse()->getWidth(),
$this->drawable()->getHeight(), $this->ellipse()->getHeight(),
0, 0,
359.99, 359.99,
$this->getBorderColor()->toInt() $this->getBorderColor()->toInt()
@@ -43,8 +43,8 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
$frame->getCore(), $frame->getCore(),
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->drawable()->getWidth(), $this->ellipse()->getWidth(),
$this->drawable()->getHeight(), $this->ellipse()->getHeight(),
$this->getBackgroundColor()->toInt() $this->getBackgroundColor()->toInt()
); );
} }

View File

@@ -3,9 +3,6 @@
namespace Intervention\Image\Drivers\Gd\Modifiers; namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Exceptions\GeometryException;
use Intervention\Image\Geometry\Line;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\ModifierInterface;
@@ -16,24 +13,12 @@ class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
return $image->eachFrame(function ($frame) { return $image->eachFrame(function ($frame) {
imageline( imageline(
$frame->getCore(), $frame->getCore(),
$this->drawable()->getStart()->getX(), $this->line()->getStart()->getX(),
$this->drawable()->getStart()->getY(), $this->line()->getStart()->getY(),
$this->drawable()->getEnd()->getX(), $this->line()->getEnd()->getX(),
$this->drawable()->getEnd()->getY(), $this->line()->getEnd()->getY(),
$this->getBackgroundColor()->toInt() $this->getBackgroundColor()->toInt()
); );
}); });
} }
public function drawable(): DrawableInterface
{
$drawable = parent::drawable();
if (!is_a($drawable, Line::class)) {
throw new GeometryException(
'Shape mismatch. Excepted Line::class, found ' . get_class($drawable)
);
}
return $drawable;
}
} }

View File

@@ -12,26 +12,26 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte
{ {
$image->eachFrame(function ($frame) { $image->eachFrame(function ($frame) {
// draw background // draw background
if ($this->drawable()->hasBackgroundColor()) { if ($this->rectangle()->hasBackgroundColor()) {
imagefilledrectangle( imagefilledrectangle(
$frame->getCore(), $frame->getCore(),
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->position->getX() + $this->drawable()->bottomRightPoint()->getX(), $this->position->getX() + $this->rectangle()->bottomRightPoint()->getX(),
$this->position->getY() + $this->drawable()->bottomRightPoint()->getY(), $this->position->getY() + $this->rectangle()->bottomRightPoint()->getY(),
$this->getBackgroundColor()->toInt() $this->getBackgroundColor()->toInt()
); );
} }
if ($this->drawable()->hasBorder()) { if ($this->rectangle()->hasBorder()) {
// draw border // draw border
imagesetthickness($frame->getCore(), $this->drawable()->getBorderSize()); imagesetthickness($frame->getCore(), $this->rectangle()->getBorderSize());
imagerectangle( imagerectangle(
$frame->getCore(), $frame->getCore(),
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->position->getX() + $this->drawable()->bottomRightPoint()->getX(), $this->position->getX() + $this->rectangle()->bottomRightPoint()->getX(),
$this->position->getY() + $this->drawable()->bottomRightPoint()->getY(), $this->position->getY() + $this->rectangle()->bottomRightPoint()->getY(),
$this->getBorderColor()->toInt() $this->getBorderColor()->toInt()
); );
} }

View File

@@ -18,16 +18,16 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
$drawing = new ImagickDraw(); $drawing = new ImagickDraw();
$drawing->setFillColor($this->getBackgroundColor()->getPixel()); $drawing->setFillColor($this->getBackgroundColor()->getPixel());
if ($this->drawable()->hasBorder()) { if ($this->ellipse()->hasBorder()) {
$drawing->setStrokeWidth($this->drawable()->getBorderSize()); $drawing->setStrokeWidth($this->ellipse()->getBorderSize());
$drawing->setStrokeColor($this->getBorderColor()->getPixel()); $drawing->setStrokeColor($this->getBorderColor()->getPixel());
} }
$drawing->ellipse( $drawing->ellipse(
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->drawable()->getWidth() / 2, $this->ellipse()->getWidth() / 2,
$this->drawable()->getHeight() / 2, $this->ellipse()->getHeight() / 2,
0, 0,
360 360
); );

View File

@@ -4,9 +4,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw; use ImagickDraw;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Exceptions\GeometryException;
use Intervention\Image\Geometry\Line;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\ModifierInterface;
@@ -16,27 +13,16 @@ class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
{ {
$drawing = new ImagickDraw(); $drawing = new ImagickDraw();
$drawing->setStrokeColor($this->getBackgroundColor()->getPixel()); $drawing->setStrokeColor($this->getBackgroundColor()->getPixel());
$drawing->setStrokeWidth($this->drawable()->getWidth()); $drawing->setStrokeWidth($this->line()->getWidth());
$drawing->line( $drawing->line(
$this->drawable()->getStart()->getX(), $this->line()->getStart()->getX(),
$this->drawable()->getStart()->getY(), $this->line()->getStart()->getY(),
$this->drawable()->getEnd()->getX(), $this->line()->getEnd()->getX(),
$this->drawable()->getEnd()->getY(), $this->line()->getEnd()->getY(),
); );
return $image->eachFrame(function ($frame) use ($drawing) { return $image->eachFrame(function ($frame) use ($drawing) {
$frame->getCore()->drawImage($drawing); $frame->getCore()->drawImage($drawing);
}); });
} }
public function drawable(): DrawableInterface
{
$drawable = parent::drawable();
if (!is_a($drawable, Line::class)) {
throw new GeometryException(
'Shape mismatch. Excepted Line::class, found ' . get_class($drawable)
);
}
return $drawable;
}
} }

View File

@@ -17,17 +17,17 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte
// setup rectangle // setup rectangle
$drawing = new ImagickDraw(); $drawing = new ImagickDraw();
$drawing->setFillColor($this->getBackgroundColor()->getPixel()); $drawing->setFillColor($this->getBackgroundColor()->getPixel());
if ($this->drawable()->hasBorder()) { if ($this->rectangle()->hasBorder()) {
$drawing->setStrokeColor($this->getBorderColor()->getPixel()); $drawing->setStrokeColor($this->getBorderColor()->getPixel());
$drawing->setStrokeWidth($this->drawable()->getBorderSize()); $drawing->setStrokeWidth($this->rectangle()->getBorderSize());
} }
// build rectangle // build rectangle
$drawing->rectangle( $drawing->rectangle(
$this->position->getX(), $this->position->getX(),
$this->position->getY(), $this->position->getY(),
$this->position->getX() + $this->drawable()->bottomRightPoint()->getX(), $this->position->getX() + $this->rectangle()->bottomRightPoint()->getX(),
$this->position->getY() + $this->drawable()->bottomRightPoint()->getY() $this->position->getY() + $this->rectangle()->bottomRightPoint()->getY()
); );
$image->eachFrame(function ($frame) use ($drawing) { $image->eachFrame(function ($frame) use ($drawing) {