mirror of
https://github.com/Intervention/image.git
synced 2025-08-28 08:09:54 +02:00
Add DrawLineModifiers
This commit is contained in:
@@ -6,6 +6,7 @@ use Intervention\Image\Collection;
|
||||
use Intervention\Image\EncodedImage;
|
||||
use Intervention\Image\Geometry\Circle;
|
||||
use Intervention\Image\Geometry\Ellipse;
|
||||
use Intervention\Image\Geometry\Line;
|
||||
use Intervention\Image\Geometry\Point;
|
||||
use Intervention\Image\Geometry\Rectangle;
|
||||
use Intervention\Image\Interfaces\CollectionInterface;
|
||||
@@ -245,6 +246,14 @@ abstract class AbstractImage implements ImageInterface
|
||||
return $this->modify($modifier);
|
||||
}
|
||||
|
||||
public function drawLine(callable $init = null): ImageInterface
|
||||
{
|
||||
$line = $this->runCallback($init, new Line(new Point(), new Point()));
|
||||
$modifier = $this->resolveDriverClass('Modifiers\DrawLineModifier', $line->getStart(), $line);
|
||||
|
||||
return $this->modify($modifier);
|
||||
}
|
||||
|
||||
public function resize(?int $width = null, ?int $height = null): ImageInterface
|
||||
{
|
||||
return $this->modify(
|
||||
|
39
src/Drivers/Gd/Modifiers/DrawLineModifier.php
Normal file
39
src/Drivers/Gd/Modifiers/DrawLineModifier.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Drivers\Gd\Modifiers;
|
||||
|
||||
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\ModifierInterface;
|
||||
|
||||
class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
|
||||
{
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
return $image->eachFrame(function ($frame) {
|
||||
imageline(
|
||||
$frame->getCore(),
|
||||
$this->drawable()->getStart()->getX(),
|
||||
$this->drawable()->getStart()->getY(),
|
||||
$this->drawable()->getEnd()->getX(),
|
||||
$this->drawable()->getEnd()->getY(),
|
||||
$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;
|
||||
}
|
||||
}
|
42
src/Drivers/Imagick/Modifiers/DrawLineModifier.php
Normal file
42
src/Drivers/Imagick/Modifiers/DrawLineModifier.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Drivers\Imagick\Modifiers;
|
||||
|
||||
use ImagickDraw;
|
||||
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\ModifierInterface;
|
||||
|
||||
class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
|
||||
{
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$drawing = new ImagickDraw();
|
||||
$drawing->setStrokeColor($this->getBackgroundColor()->getPixel());
|
||||
$drawing->setStrokeWidth($this->drawable()->getWidth());
|
||||
$drawing->line(
|
||||
$this->drawable()->getStart()->getX(),
|
||||
$this->drawable()->getStart()->getY(),
|
||||
$this->drawable()->getEnd()->getX(),
|
||||
$this->drawable()->getEnd()->getY(),
|
||||
);
|
||||
return $image->eachFrame(function ($frame) use ($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;
|
||||
}
|
||||
}
|
85
src/Geometry/Line.php
Normal file
85
src/Geometry/Line.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Geometry;
|
||||
|
||||
use Intervention\Image\Geometry\Traits\HasBackgroundColor;
|
||||
use Intervention\Image\Geometry\Traits\HasBorder;
|
||||
use Intervention\Image\Interfaces\DrawableInterface;
|
||||
|
||||
class Line implements DrawableInterface
|
||||
{
|
||||
use HasBorder;
|
||||
use HasBackgroundColor;
|
||||
|
||||
public function __construct(
|
||||
protected Point $start,
|
||||
protected Point $end,
|
||||
protected int $width = 1
|
||||
) {
|
||||
//
|
||||
}
|
||||
|
||||
public function getWidth(): int
|
||||
{
|
||||
return $this->width;
|
||||
}
|
||||
|
||||
public function setWidth(int $width): self
|
||||
{
|
||||
$this->width = $width;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function width(int $width): self
|
||||
{
|
||||
return $this->setWidth($width);
|
||||
}
|
||||
|
||||
public function color($color): self
|
||||
{
|
||||
$this->setBackgroundColor($color);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getStart(): Point
|
||||
{
|
||||
return $this->start;
|
||||
}
|
||||
|
||||
public function getEnd(): Point
|
||||
{
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
public function setStart(Point $start): self
|
||||
{
|
||||
$this->start = $start;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function from(int $x, int $y): self
|
||||
{
|
||||
$this->getStart()->setX($x);
|
||||
$this->getStart()->setY($y);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function to(int $x, int $y): self
|
||||
{
|
||||
$this->getEnd()->setX($x);
|
||||
$this->getEnd()->setY($y);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setEnd(Point $end): self
|
||||
{
|
||||
$this->end = $end;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user