diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index 01b702ab..097ac5bb 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -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( diff --git a/src/Drivers/Gd/Modifiers/DrawLineModifier.php b/src/Drivers/Gd/Modifiers/DrawLineModifier.php new file mode 100644 index 00000000..adcc455b --- /dev/null +++ b/src/Drivers/Gd/Modifiers/DrawLineModifier.php @@ -0,0 +1,39 @@ +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; + } +} diff --git a/src/Drivers/Imagick/Modifiers/DrawLineModifier.php b/src/Drivers/Imagick/Modifiers/DrawLineModifier.php new file mode 100644 index 00000000..6c224a55 --- /dev/null +++ b/src/Drivers/Imagick/Modifiers/DrawLineModifier.php @@ -0,0 +1,42 @@ +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; + } +} diff --git a/src/Geometry/Line.php b/src/Geometry/Line.php new file mode 100644 index 00000000..7ef5a827 --- /dev/null +++ b/src/Geometry/Line.php @@ -0,0 +1,85 @@ +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; + } +}