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

Add drawing shortcut methods

This commit is contained in:
Oliver Vogel
2023-11-26 19:31:53 +01:00
parent 8a698b6e60
commit 7c6a8fc64f
7 changed files with 365 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
<?php
namespace Intervention\Image\Geometry\Factories;
class CircleFactory extends EllipseFactory
{
public function radius(int $radius): self
{
$this->ellipse->setSize($radius * 2, $radius * 2);
return $this;
}
public function diameter(int $diameter): self
{
$this->ellipse->setSize($diameter, $diameter);
return $this;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Ellipse;
use Intervention\Image\Geometry\Point;
class EllipseFactory
{
protected Ellipse $ellipse;
public function __construct(protected Point $pivot, callable|Ellipse $init)
{
$this->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;
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Line;
class LineFactory
{
protected Line $line;
public function __construct(callable|Line $init)
{
$this->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;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Polygon;
class PolygonFactory
{
protected Polygon $polygon;
public function __construct(callable|Polygon $init)
{
$this->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;
}
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Rectangle;
class RectangleFactory
{
protected Rectangle $rectangle;
public function __construct(protected Point $pivot, callable|Rectangle $init)
{
$this->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;
}
}

View File

@@ -17,6 +17,11 @@ use Intervention\Image\Encoders\GifEncoder;
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\ColorizeModifier;
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 @@ final class Image implements ImageInterface, Countable
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}
*

View File

@@ -436,6 +436,62 @@ interface ImageInterface extends IteratorAggregate, Countable
*/
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
*