diff --git a/src/Geometry/Factories/CircleFactory.php b/src/Geometry/Factories/CircleFactory.php new file mode 100644 index 00000000..74340fd6 --- /dev/null +++ b/src/Geometry/Factories/CircleFactory.php @@ -0,0 +1,20 @@ +ellipse->setSize($radius * 2, $radius * 2); + + return $this; + } + + public function diameter(int $diameter): self + { + $this->ellipse->setSize($diameter, $diameter); + + return $this; + } +} diff --git a/src/Geometry/Factories/EllipseFactory.php b/src/Geometry/Factories/EllipseFactory.php new file mode 100644 index 00000000..12662572 --- /dev/null +++ b/src/Geometry/Factories/EllipseFactory.php @@ -0,0 +1,46 @@ +ellipse = is_a($init, Ellipse::class) ? $init : new Ellipse(0, 0, $pivot); + + if (is_callable($init)) { + $init($this); + } + } + + public function size(int $width, int $height): self + { + $this->ellipse->setSize($width, $height); + + return $this; + } + + public function background(mixed $color): self + { + $this->ellipse->setBackgroundColor($color); + + return $this; + } + + public function border(mixed $color, int $size = 1): self + { + $this->ellipse->setBorder($color, $size); + + return $this; + } + + public function __invoke(): Ellipse + { + return $this->ellipse; + } +} diff --git a/src/Geometry/Factories/LineFactory.php b/src/Geometry/Factories/LineFactory.php new file mode 100644 index 00000000..db846633 --- /dev/null +++ b/src/Geometry/Factories/LineFactory.php @@ -0,0 +1,71 @@ +line = is_a($init, Line::class) ? $init : new Line(new Point(), new Point()); + + if (is_callable($init)) { + $init($this); + } + } + + public function color(mixed $color): self + { + $this->line->setBackgroundColor($color); + $this->line->setBorderColor($color); + + return $this; + } + + public function background(mixed $color): self + { + $this->line->setBackgroundColor($color); + $this->line->setBorderColor($color); + + return $this; + } + + public function border(mixed $color, int $size = 1): self + { + $this->line->setBackgroundColor($color); + $this->line->setBorderColor($color); + $this->line->setWidth($size); + + return $this; + } + + public function width(int $size): self + { + $this->line->setWidth($size); + + return $this; + } + + public function from(int $x, int $y): self + { + $this->line->setStart(new Point($x, $y)); + + return $this; + } + + public function to(int $x, int $y): self + { + $this->line->setEnd(new Point($x, $y)); + + return $this; + } + + public function __invoke(): Line + { + return $this->line; + } +} diff --git a/src/Geometry/Factories/PolygonFactory.php b/src/Geometry/Factories/PolygonFactory.php new file mode 100644 index 00000000..e7c501aa --- /dev/null +++ b/src/Geometry/Factories/PolygonFactory.php @@ -0,0 +1,46 @@ +polygon = is_a($init, Polygon::class) ? $init : new Polygon([]); + + if (is_callable($init)) { + $init($this); + } + } + + public function point(int $x, int $y): self + { + $this->polygon->addPoint(new Point($x, $y)); + + return $this; + } + + public function background(mixed $color): self + { + $this->polygon->setBackgroundColor($color); + + return $this; + } + + public function border(mixed $color, int $size = 1): self + { + $this->polygon->setBorder($color, $size); + + return $this; + } + + public function __invoke(): Polygon + { + return $this->polygon; + } +} diff --git a/src/Geometry/Factories/RectangleFactory.php b/src/Geometry/Factories/RectangleFactory.php new file mode 100644 index 00000000..6ad75cf7 --- /dev/null +++ b/src/Geometry/Factories/RectangleFactory.php @@ -0,0 +1,46 @@ +rectangle = is_a($init, Rectangle::class) ? $init : new Rectangle(0, 0, $pivot); + + if (is_callable($init)) { + $init($this); + } + } + + public function size(int $width, int $height): self + { + $this->rectangle->setSize($width, $height); + + return $this; + } + + public function background(mixed $color): self + { + $this->rectangle->setBackgroundColor($color); + + return $this; + } + + public function border(mixed $color, int $size = 1): self + { + $this->rectangle->setBorder($color, $size); + + return $this; + } + + public function __invoke(): Rectangle + { + return $this->rectangle; + } +} diff --git a/src/Image.php b/src/Image.php index e1566732..9a1a7437 100644 --- a/src/Image.php +++ b/src/Image.php @@ -17,6 +17,11 @@ use Intervention\Image\Encoders\JpegEncoder; use Intervention\Image\Encoders\PngEncoder; use Intervention\Image\Encoders\WebpEncoder; +use Intervention\Image\Geometry\Factories\CircleFactory; +use Intervention\Image\Geometry\Factories\EllipseFactory; +use Intervention\Image\Geometry\Factories\LineFactory; +use Intervention\Image\Geometry\Factories\PolygonFactory; +use Intervention\Image\Geometry\Factories\RectangleFactory; use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\AnalyzerInterface; @@ -39,6 +44,11 @@ use Intervention\Image\Modifiers\ColorspaceModifier; use Intervention\Image\Modifiers\ContrastModifier; use Intervention\Image\Modifiers\CropModifier; +use Intervention\Image\Modifiers\DrawEllipseModifier; +use Intervention\Image\Modifiers\DrawLineModifier; +use Intervention\Image\Modifiers\DrawPixelModifier; +use Intervention\Image\Modifiers\DrawPolygonModifier; +use Intervention\Image\Modifiers\DrawRectangleModifier; use Intervention\Image\Modifiers\FillModifier; use Intervention\Image\Modifiers\FitDownModifier; use Intervention\Image\Modifiers\FitModifier; @@ -577,6 +587,76 @@ public function fill(mixed $color, ?int $x = null, ?int $y = null): ImageInterfa return $this->modify(new FillModifier($color, new Point($x, $y))); } + /** + * {@inheritdoc} + * + * @see ImageInterface::drawPixel() + */ + public function drawPixel(int $x, int $y, mixed $color): ImageInterface + { + return $this->modify(new DrawPixelModifier(new Point($x, $y), $color)); + } + + /** + * {@inheritdoc} + * + * @see ImageInterface::drawRectangle() + */ + public function drawRectangle(int $x, int $y, callable|Rectangle $init): ImageInterface + { + return $this->modify( + new DrawRectangleModifier( + call_user_func(new RectangleFactory(new Point($x, $y), $init)), + ), + ); + } + + /** + * {@inheritdoc} + * + * @see ImageInterface::drawEllipse() + */ + public function drawEllipse(int $x, int $y, callable $init): ImageInterface + { + return $this->modify( + new DrawEllipseModifier( + call_user_func(new EllipseFactory(new Point($x, $y), $init)), + ), + ); + } + + /** + * {@inheritdoc} + * + * @see ImageInterface::drawCircle() + */ + public function drawCircle(int $x, int $y, callable $init): ImageInterface + { + return $this->modify( + new DrawEllipseModifier( + call_user_func(new CircleFactory(new Point($x, $y), $init)), + ), + ); + } + + public function drawPolygon(callable $init): ImageInterface + { + return $this->modify( + new DrawPolygonModifier( + call_user_func(new PolygonFactory($init)), + ), + ); + } + + public function drawLine(callable $init): ImageInterface + { + return $this->modify( + new DrawLineModifier( + call_user_func(new LineFactory($init)), + ), + ); + } + /** * {@inheritdoc} * diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index 9b41ee4c..e25f3711 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -436,6 +436,62 @@ public function place( */ public function fill(mixed $color, ?int $x = null, ?int $y = null): ImageInterface; + /** + * Draw a single pixel at given position defined by the coordinates x and y in a given color. + * + * @param int $x + * @param int $y + * @param mixed $color + * @return ImageInterface + */ + public function drawPixel(int $x, int $y, mixed $color): ImageInterface; + + /** + * Draw a rectangle on the current image + * + * @param int $x + * @param int $y + * @param callable $init + * @return ImageInterface + */ + public function drawRectangle(int $x, int $y, callable $init): ImageInterface; + + /** + * Draw ellipse on the current image + * + * @param int $x + * @param int $y + * @param callable $init + * @return ImageInterface + */ + public function drawEllipse(int $x, int $y, callable $init): ImageInterface; + + /** + * Draw circle on the current image + * + * @param int $x + * @param int $y + * @param callable $init + * @return ImageInterface + */ + public function drawCircle(int $x, int $y, callable $init): ImageInterface; + + /** + * Draw a polygon on the current image + * + * @param callable $init + * @return ImageInterface + */ + public function drawPolygon(callable $init): ImageInterface; + + /** + * Draw a line on the current image + * + * @param callable $init + * @return ImageInterface + */ + public function drawLine(callable $init): ImageInterface; + /** * Encode image to JPEG format *