1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-18 11:41:17 +02:00

Add Drawable improvements & preparation for future features

* Add DrawableFactoryInterface
* Add universal Drawable factory class
* Add x/y setter methods to PointInterface
* Replace Point::class type hints by PointInterface::class
This commit is contained in:
Oliver Vogel
2024-06-09 10:49:00 +02:00
committed by GitHub
parent 56a92c7144
commit 410d98002a
18 changed files with 461 additions and 104 deletions

View File

@@ -15,6 +15,7 @@ use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\FontInterface; use Intervention\Image\Interfaces\FontInterface;
use Intervention\Image\Interfaces\FrameInterface; use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\PointInterface;
use Intervention\Image\Interfaces\SpecializedInterface; use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\Modifiers\TextModifier as GenericTextModifier; use Intervention\Image\Modifiers\TextModifier as GenericTextModifier;
use Intervention\Image\Typography\Line; use Intervention\Image\Typography\Line;
@@ -112,14 +113,14 @@ class TextModifier extends GenericTextModifier implements SpecializedInterface
* @param FrameInterface $frame * @param FrameInterface $frame
* @param Line $textline * @param Line $textline
* @param null|ImagickDraw $draw * @param null|ImagickDraw $draw
* @param Point $offset * @param PointInterface $offset
* @return void * @return void
*/ */
private function maybeDrawTextline( private function maybeDrawTextline(
FrameInterface $frame, FrameInterface $frame,
Line $textline, Line $textline,
?ImagickDraw $draw = null, ?ImagickDraw $draw = null,
Point $offset = new Point(), PointInterface $offset = new Point(),
): void { ): void {
if ($draw !== null) { if ($draw !== null) {
$frame->native()->annotateImage( $frame->native()->annotateImage(

View File

@@ -15,8 +15,8 @@ use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\PointInterface; use Intervention\Image\Interfaces\PointInterface;
/** /**
* @implements IteratorAggregate<Point> * @implements IteratorAggregate<PointInterface>
* @implements ArrayAccess<int, Point> * @implements ArrayAccess<int, PointInterface>
*/ */
class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface
{ {
@@ -26,7 +26,7 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Create new bezier instance * Create new bezier instance
* *
* @param array<Point> $points * @param array<PointInterface> $points
* @param PointInterface $pivot * @param PointInterface $pivot
* @return void * @return void
*/ */
@@ -49,7 +49,7 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Implement iteration through all points of bezier * Implement iteration through all points of bezier
* *
* @return Traversable<Point> * @return Traversable<PointInterface>
*/ */
public function getIterator(): Traversable public function getIterator(): Traversable
{ {
@@ -69,10 +69,10 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Change pivot point to given point * Change pivot point to given point
* *
* @param Point $pivot * @param PointInterface $pivot
* @return Bezier * @return Bezier
*/ */
public function setPivot(Point $pivot): self public function setPivot(PointInterface $pivot): self
{ {
$this->pivot = $pivot; $this->pivot = $pivot;
@@ -82,9 +82,9 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Return first control point of bezier * Return first control point of bezier
* *
* @return ?Point * @return ?PointInterface
*/ */
public function first(): ?Point public function first(): ?PointInterface
{ {
if ($point = reset($this->points)) { if ($point = reset($this->points)) {
return $point; return $point;
@@ -96,9 +96,9 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Return second control point of bezier * Return second control point of bezier
* *
* @return ?Point * @return ?PointInterface
*/ */
public function second(): ?Point public function second(): ?PointInterface
{ {
if (array_key_exists(1, $this->points)) { if (array_key_exists(1, $this->points)) {
return $this->points[1]; return $this->points[1];
@@ -110,9 +110,9 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Return third control point of bezier * Return third control point of bezier
* *
* @return ?Point * @return ?PointInterface
*/ */
public function third(): ?Point public function third(): ?PointInterface
{ {
if (array_key_exists(2, $this->points)) { if (array_key_exists(2, $this->points)) {
return $this->points[2]; return $this->points[2];
@@ -124,9 +124,9 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Return last control point of bezier * Return last control point of bezier
* *
* @return ?Point * @return ?PointInterface
*/ */
public function last(): ?Point public function last(): ?PointInterface
{ {
if ($point = end($this->points)) { if ($point = end($this->points)) {
return $point; return $point;
@@ -160,7 +160,7 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
* Return point at given offset * Return point at given offset
* *
* @param mixed $offset * @param mixed $offset
* @return Point * @return PointInterface
*/ */
public function offsetGet($offset): mixed public function offsetGet($offset): mixed
{ {
@@ -171,7 +171,7 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
* Set point at given offset * Set point at given offset
* *
* @param mixed $offset * @param mixed $offset
* @param Point $value * @param PointInterface $value
* @return void * @return void
*/ */
public function offsetSet($offset, $value): void public function offsetSet($offset, $value): void
@@ -193,10 +193,10 @@ class Bezier implements IteratorAggregate, Countable, ArrayAccess, DrawableInter
/** /**
* Add given point to bezier * Add given point to bezier
* *
* @param Point $point * @param PointInterface $point
* @return Bezier * @return Bezier
*/ */
public function addPoint(Point $point): self public function addPoint(PointInterface $point): self
{ {
$this->points[] = $point; $this->points[] = $point;

View File

@@ -6,18 +6,20 @@ namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Bezier; use Intervention\Image\Geometry\Bezier;
use Intervention\Image\Interfaces\DrawableFactoryInterface;
use Intervention\Image\Interfaces\DrawableInterface;
class BezierFactory class BezierFactory implements DrawableFactoryInterface
{ {
protected Bezier $bezier; protected Bezier $bezier;
/** /**
* Create new factory instance * Create new factory instance
* *
* @param callable|Bezier $init * @param null|callable|Bezier $init
* @return void * @return void
*/ */
public function __construct(callable|Bezier $init) public function __construct(null|callable|Bezier $init = null)
{ {
$this->bezier = is_a($init, Bezier::class) ? $init : new Bezier([]); $this->bezier = is_a($init, Bezier::class) ? $init : new Bezier([]);
@@ -26,6 +28,16 @@ class BezierFactory
} }
} }
/**
* {@inheritdoc}
*
* @see DrawableFactoryInterface::create()
*/
public function create(): DrawableInterface
{
return $this->bezier;
}
/** /**
* Add a point to the bezier to be produced * Add a point to the bezier to be produced
* *

View File

@@ -4,8 +4,44 @@ declare(strict_types=1);
namespace Intervention\Image\Geometry\Factories; namespace Intervention\Image\Geometry\Factories;
class CircleFactory extends EllipseFactory use Intervention\Image\Geometry\Circle;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\DrawableFactoryInterface;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\PointInterface;
class CircleFactory implements DrawableFactoryInterface
{ {
protected Circle $circle;
/**
* Create new factory instance
*
* @param PointInterface $pivot
* @param null|callable|Circle $init
* @return void
*/
public function __construct(
protected PointInterface $pivot = new Point(),
null|callable|Circle $init = null,
) {
$this->circle = is_a($init, Circle::class) ? $init : new Circle(0, $pivot);
if (is_callable($init)) {
$init($this);
}
}
/**
* {@inheritdoc}
*
* @see DrawableFactoryInterface::create()
*/
public function create(): DrawableInterface
{
return $this->circle;
}
/** /**
* Set the radius of the circle to be produced * Set the radius of the circle to be produced
* *
@@ -14,7 +50,7 @@ class CircleFactory extends EllipseFactory
*/ */
public function radius(int $radius): self public function radius(int $radius): self
{ {
$this->ellipse->setSize($radius * 2, $radius * 2); $this->circle->setSize($radius * 2, $radius * 2);
return $this; return $this;
} }
@@ -27,8 +63,45 @@ class CircleFactory extends EllipseFactory
*/ */
public function diameter(int $diameter): self public function diameter(int $diameter): self
{ {
$this->ellipse->setSize($diameter, $diameter); $this->circle->setSize($diameter, $diameter);
return $this; return $this;
} }
/**
* Set the background color of the circle to be produced
*
* @param mixed $color
* @return CircleFactory
*/
public function background(mixed $color): self
{
$this->circle->setBackgroundColor($color);
return $this;
}
/**
* Set the border color & border size of the ellipse to be produced
*
* @param mixed $color
* @param int $size
* @return CircleFactory
*/
public function border(mixed $color, int $size = 1): self
{
$this->circle->setBorder($color, $size);
return $this;
}
/**
* Produce the circle
*
* @return Circle
*/
public function __invoke(): Circle
{
return $this->circle;
}
} }

View File

@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Geometry\Factories;
class Drawable
{
/**
* Creeate BezierFactory statically
*
* @return BezierFactory
*/
public static function bezier(): BezierFactory
{
return new BezierFactory();
}
/**
* Creeate CircleFactory statically
*
* @return CircleFactory
*/
public static function circle(): CircleFactory
{
return new CircleFactory();
}
/**
*
* Create EllipseFactory statically
*
* @return EllipseFactory
*/
public static function ellipse(): EllipseFactory
{
return new EllipseFactory();
}
/**
* Creeate LineFactory statically
*
* @return LineFactory
*/
public static function line(): LineFactory
{
return new LineFactory();
}
/**
* Creeate PolygonFactory statically
*
* @return PolygonFactory
*/
public static function polygon(): PolygonFactory
{
return new PolygonFactory();
}
/**
* Creeate RectangleFactory statically
*
* @return RectangleFactory
*/
public static function rectangle(): RectangleFactory
{
return new RectangleFactory();
}
}

View File

@@ -6,20 +6,25 @@ namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Ellipse; use Intervention\Image\Geometry\Ellipse;
use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\DrawableFactoryInterface;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\PointInterface;
class EllipseFactory class EllipseFactory implements DrawableFactoryInterface
{ {
protected Ellipse $ellipse; protected Ellipse $ellipse;
/** /**
* Create new factory instance * Create new factory instance
* *
* @param Point $pivot * @param PointInterface $pivot
* @param callable|Ellipse $init * @param null|callable|Ellipse $init
* @return void * @return void
*/ */
public function __construct(protected Point $pivot, callable|Ellipse $init) public function __construct(
{ protected PointInterface $pivot = new Point(),
null|callable|Ellipse $init = null,
) {
$this->ellipse = is_a($init, Ellipse::class) ? $init : new Ellipse(0, 0, $pivot); $this->ellipse = is_a($init, Ellipse::class) ? $init : new Ellipse(0, 0, $pivot);
if (is_callable($init)) { if (is_callable($init)) {
@@ -27,6 +32,16 @@ class EllipseFactory
} }
} }
/**
* {@inheritdoc}
*
* @see DrawableFactoryInterface::create()
*/
public function create(): DrawableInterface
{
return $this->ellipse;
}
/** /**
* Set the size of the ellipse to be produced * Set the size of the ellipse to be produced
* *

View File

@@ -6,18 +6,20 @@ namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Line; use Intervention\Image\Geometry\Line;
use Intervention\Image\Interfaces\DrawableFactoryInterface;
use Intervention\Image\Interfaces\DrawableInterface;
class LineFactory class LineFactory implements DrawableFactoryInterface
{ {
protected Line $line; protected Line $line;
/** /**
* Create the factory instance * Create the factory instance
* *
* @param callable|Line $init * @param null|callable|Line $init
* @return void * @return void
*/ */
public function __construct(callable|Line $init) public function __construct(null|callable|Line $init = null)
{ {
$this->line = is_a($init, Line::class) ? $init : new Line(new Point(), new Point()); $this->line = is_a($init, Line::class) ? $init : new Line(new Point(), new Point());
@@ -26,6 +28,16 @@ class LineFactory
} }
} }
/**
* {@inheritdoc}
*
* @see DrawableFactoryInterface::create()
*/
public function create(): DrawableInterface
{
return $this->line;
}
/** /**
* Set the color of the line to be produced * Set the color of the line to be produced
* *

View File

@@ -6,18 +6,20 @@ namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Polygon; use Intervention\Image\Geometry\Polygon;
use Intervention\Image\Interfaces\DrawableFactoryInterface;
use Intervention\Image\Interfaces\DrawableInterface;
class PolygonFactory class PolygonFactory implements DrawableFactoryInterface
{ {
protected Polygon $polygon; protected Polygon $polygon;
/** /**
* Create new factory instance * Create new factory instance
* *
* @param callable|Polygon $init * @param null|callable|Polygon $init
* @return void * @return void
*/ */
public function __construct(callable|Polygon $init) public function __construct(null|callable|Polygon $init = null)
{ {
$this->polygon = is_a($init, Polygon::class) ? $init : new Polygon([]); $this->polygon = is_a($init, Polygon::class) ? $init : new Polygon([]);
@@ -26,6 +28,16 @@ class PolygonFactory
} }
} }
/**
* {@inheritdoc}
*
* @see DrawableFactoryInterface::create()
*/
public function create(): DrawableInterface
{
return $this->polygon;
}
/** /**
* Add a point to the polygon to be produced * Add a point to the polygon to be produced
* *

View File

@@ -6,20 +6,25 @@ namespace Intervention\Image\Geometry\Factories;
use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\DrawableFactoryInterface;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\PointInterface;
class RectangleFactory class RectangleFactory implements DrawableFactoryInterface
{ {
protected Rectangle $rectangle; protected Rectangle $rectangle;
/** /**
* Create new instance * Create new instance
* *
* @param Point $pivot * @param PointInterface $pivot
* @param callable|Rectangle $init * @param null|callable|Rectangle $init
* @return void * @return void
*/ */
public function __construct(protected Point $pivot, callable|Rectangle $init) public function __construct(
{ protected PointInterface $pivot = new Point(),
null|callable|Rectangle $init = null,
) {
$this->rectangle = is_a($init, Rectangle::class) ? $init : new Rectangle(0, 0, $pivot); $this->rectangle = is_a($init, Rectangle::class) ? $init : new Rectangle(0, 0, $pivot);
if (is_callable($init)) { if (is_callable($init)) {
@@ -27,6 +32,16 @@ class RectangleFactory
} }
} }
/**
* {@inheritdoc}
*
* @see DrawableFactoryInterface::create()
*/
public function create(): DrawableInterface
{
return $this->rectangle;
}
/** /**
* Set the size of the rectangle to be produced * Set the size of the rectangle to be produced
* *

View File

@@ -17,14 +17,14 @@ class Line implements DrawableInterface
/** /**
* Create new line instance * Create new line instance
* *
* @param Point $start * @param PointInterface $start
* @param Point $end * @param PointInterface $end
* @param int $width * @param int $width
* @return void * @return void
*/ */
public function __construct( public function __construct(
protected Point $start, protected PointInterface $start,
protected Point $end, protected PointInterface $end,
protected int $width = 1 protected int $width = 1
) { ) {
} }
@@ -39,6 +39,18 @@ class Line implements DrawableInterface
return $this->start; return $this->start;
} }
/**
* {@inheritdoc}
*
* @see DrawableInterface::setPosition()
*/
public function setPosition(PointInterface $position): DrawableInterface
{
$this->start = $position;
return $this;
}
/** /**
* Return line width * Return line width
* *
@@ -65,9 +77,9 @@ class Line implements DrawableInterface
/** /**
* Get starting point of line * Get starting point of line
* *
* @return Point * @return PointInterface
*/ */
public function start(): Point public function start(): PointInterface
{ {
return $this->start; return $this->start;
} }
@@ -75,9 +87,9 @@ class Line implements DrawableInterface
/** /**
* get end point of line * get end point of line
* *
* @return Point * @return PointInterface
*/ */
public function end(): Point public function end(): PointInterface
{ {
return $this->end; return $this->end;
} }
@@ -85,10 +97,10 @@ class Line implements DrawableInterface
/** /**
* Set starting point of line * Set starting point of line
* *
* @param Point $start * @param PointInterface $start
* @return Line * @return Line
*/ */
public function setStart(Point $start): self public function setStart(PointInterface $start): self
{ {
$this->start = $start; $this->start = $start;
@@ -128,10 +140,10 @@ class Line implements DrawableInterface
/** /**
* Set end point of line * Set end point of line
* *
* @param Point $end * @param PointInterface $end
* @return Line * @return Line
*/ */
public function setEnd(Point $end): self public function setEnd(PointInterface $end): self
{ {
$this->end = $end; $this->end = $end;

View File

@@ -22,9 +22,9 @@ class Point implements PointInterface
} }
/** /**
* Sets X coordinate * {@inheritdoc}
* *
* @param int $x * @see PointInterface::setX()
*/ */
public function setX(int $x): self public function setX(int $x): self
{ {
@@ -34,9 +34,9 @@ class Point implements PointInterface
} }
/** /**
* Get X coordinate * {@inheritdoc}
* *
* @return int * @see PointInterface::x()
*/ */
public function x(): int public function x(): int
{ {
@@ -44,9 +44,9 @@ class Point implements PointInterface
} }
/** /**
* Sets Y coordinate * {@inheritdoc}
* *
* @param int $y * @see PointInterface::setY()
*/ */
public function setY(int $y): self public function setY(int $y): self
{ {
@@ -56,9 +56,9 @@ class Point implements PointInterface
} }
/** /**
* Get Y coordinate * {@inheritdoc}
* *
* @return int * @see PointInterface::y()
*/ */
public function y(): int public function y(): int
{ {
@@ -66,9 +66,9 @@ class Point implements PointInterface
} }
/** /**
* Move X coordinate * {@inheritdoc}
* *
* @param int $value * @see PointInterface::moveX()
*/ */
public function moveX(int $value): self public function moveX(int $value): self
{ {
@@ -78,9 +78,9 @@ class Point implements PointInterface
} }
/** /**
* Move Y coordinate * {@inheritdoc}
* *
* @param int $value * @see PointInterface::moveY()
*/ */
public function moveY(int $value): self public function moveY(int $value): self
{ {
@@ -89,17 +89,20 @@ class Point implements PointInterface
return $this; return $this;
} }
/**
* {@inheritdoc}
*
* @see PointInterface::move()
*/
public function move(int $x, int $y): self public function move(int $x, int $y): self
{ {
return $this->moveX($x)->moveY($y); return $this->moveX($x)->moveY($y);
} }
/** /**
* Sets both X and Y coordinate * {@inheritdoc}
* *
* @param int $x * @see PointInterface::setPosition()
* @param int $y
* @return Point
*/ */
public function setPosition(int $x, int $y): self public function setPosition(int $x, int $y): self
{ {
@@ -110,11 +113,9 @@ class Point implements PointInterface
} }
/** /**
* Rotate point ccw around pivot * {@inheritdoc}
* *
* @param float $angle * @see PointInterface::rotate()
* @param PointInterface $pivot
* @return Point
*/ */
public function rotate(float $angle, PointInterface $pivot): self public function rotate(float $angle, PointInterface $pivot): self
{ {

View File

@@ -15,8 +15,8 @@ use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\PointInterface; use Intervention\Image\Interfaces\PointInterface;
/** /**
* @implements IteratorAggregate<Point> * @implements IteratorAggregate<PointInterface>
* @implements ArrayAccess<int, Point> * @implements ArrayAccess<int, PointInterface>
*/ */
class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface
{ {
@@ -26,7 +26,7 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Create new polygon instance * Create new polygon instance
* *
* @param array<Point> $points * @param array<PointInterface> $points
* @param PointInterface $pivot * @param PointInterface $pivot
* @return void * @return void
*/ */
@@ -49,7 +49,7 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Implement iteration through all points of polygon * Implement iteration through all points of polygon
* *
* @return Traversable<Point> * @return Traversable<PointInterface>
*/ */
public function getIterator(): Traversable public function getIterator(): Traversable
{ {
@@ -69,10 +69,10 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Change pivot point to given point * Change pivot point to given point
* *
* @param Point $pivot * @param PointInterface $pivot
* @return Polygon * @return Polygon
*/ */
public function setPivot(Point $pivot): self public function setPivot(PointInterface $pivot): self
{ {
$this->pivot = $pivot; $this->pivot = $pivot;
@@ -82,9 +82,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return first point of polygon * Return first point of polygon
* *
* @return ?Point * @return ?PointInterface
*/ */
public function first(): ?Point public function first(): ?PointInterface
{ {
if ($point = reset($this->points)) { if ($point = reset($this->points)) {
return $point; return $point;
@@ -96,9 +96,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return last point of polygon * Return last point of polygon
* *
* @return ?Point * @return ?PointInterface
*/ */
public function last(): ?Point public function last(): ?PointInterface
{ {
if ($point = end($this->points)) { if ($point = end($this->points)) {
return $point; return $point;
@@ -132,7 +132,7 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
* Return point at given offset * Return point at given offset
* *
* @param mixed $offset * @param mixed $offset
* @return Point * @return PointInterface
*/ */
public function offsetGet($offset): mixed public function offsetGet($offset): mixed
{ {
@@ -143,7 +143,7 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
* Set point at given offset * Set point at given offset
* *
* @param mixed $offset * @param mixed $offset
* @param Point $value * @param PointInterface $value
* @return void * @return void
*/ */
public function offsetSet($offset, $value): void public function offsetSet($offset, $value): void
@@ -165,10 +165,10 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Add given point to polygon * Add given point to polygon
* *
* @param Point $point * @param PointInterface $point
* @return Polygon * @return Polygon
*/ */
public function addPoint(Point $point): self public function addPoint(PointInterface $point): self
{ {
$this->points[] = $point; $this->points[] = $point;
@@ -198,9 +198,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return most left point of all points in polygon * Return most left point of all points in polygon
* *
* @return Point * @return PointInterface
*/ */
public function mostLeftPoint(): Point public function mostLeftPoint(): PointInterface
{ {
$points = []; $points = [];
foreach ($this->points as $point) { foreach ($this->points as $point) {
@@ -220,9 +220,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return most right point in polygon * Return most right point in polygon
* *
* @return Point * @return PointInterface
*/ */
public function mostRightPoint(): Point public function mostRightPoint(): PointInterface
{ {
$points = []; $points = [];
foreach ($this->points as $point) { foreach ($this->points as $point) {
@@ -242,9 +242,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return most top point in polygon * Return most top point in polygon
* *
* @return Point * @return PointInterface
*/ */
public function mostTopPoint(): Point public function mostTopPoint(): PointInterface
{ {
$points = []; $points = [];
foreach ($this->points as $point) { foreach ($this->points as $point) {
@@ -264,9 +264,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return most bottom point in polygon * Return most bottom point in polygon
* *
* @return Point * @return PointInterface
*/ */
public function mostBottomPoint(): Point public function mostBottomPoint(): PointInterface
{ {
$points = []; $points = [];
foreach ($this->points as $point) { foreach ($this->points as $point) {
@@ -286,9 +286,9 @@ class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInte
/** /**
* Return point in absolute center of the polygon * Return point in absolute center of the polygon
* *
* @return Point * @return PointInterface
*/ */
public function centerPoint(): Point public function centerPoint(): PointInterface
{ {
return new Point( return new Point(
$this->mostRightPoint()->x() - (intval(round($this->width() / 2))), $this->mostRightPoint()->x() - (intval(round($this->width() / 2))),

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Interfaces;
interface DrawableFactoryInterface
{
/**
* Create the end product of the factory
*
* @return DrawableInterface
*/
public function create(): DrawableInterface;
/**
* Create the end product by invoking the factory
*
* @return DrawableInterface
*/
public function __invoke(): DrawableInterface;
/**
* Define the background color of the drawable object
*
* @param mixed $color
* @return DrawableFactoryInterface
*/
public function background(mixed $color): self;
/**
* Set the border size & color of the drawable object to be produced
*
* @param mixed $color
* @param int $size
* @return DrawableFactoryInterface
*/
public function border(mixed $color, int $size = 1): self;
}

View File

@@ -20,6 +20,45 @@ interface PointInterface
*/ */
public function y(): int; public function y(): int;
/**
* Set x position
*
* @param int $x
* @return PointInterface
*/
public function setX(int $x): self;
/**
* Set y position
*
* @param int $y
* @return PointInterface
*/
public function setY(int $y): self;
/**
* Move X coordinate
*
* @param int $value
*/
public function moveX(int $value): self;
/**
* Move Y coordinate
*
* @param int $value
*/
public function moveY(int $value): self;
/**
* Move position of current point by given coordinates
*
* @param int $x
* @param int $y
* @return PointInterface
*/
public function move(int $x, int $y): self;
/** /**
* Set position of point * Set position of point
* *
@@ -28,4 +67,13 @@ interface PointInterface
* @return PointInterface * @return PointInterface
*/ */
public function setPosition(int $x, int $y): self; public function setPosition(int $x, int $y): self;
/**
* Rotate point counter clock wise around given pivot point
*
* @param float $angle
* @param PointInterface $pivot
* @return PointInterface
*/
public function rotate(float $angle, self $pivot): self;
} }

View File

@@ -5,13 +5,13 @@ declare(strict_types=1);
namespace Intervention\Image\Modifiers; namespace Intervention\Image\Modifiers;
use Intervention\Image\Drivers\SpecializableModifier; use Intervention\Image\Drivers\SpecializableModifier;
use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\PointInterface;
class FillModifier extends SpecializableModifier class FillModifier extends SpecializableModifier
{ {
public function __construct( public function __construct(
public mixed $color, public mixed $color,
public ?Point $position = null public ?PointInterface $position = null
) { ) {
} }

View File

@@ -10,12 +10,13 @@ use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Geometry\Point; use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\FontInterface; use Intervention\Image\Interfaces\FontInterface;
use Intervention\Image\Interfaces\PointInterface;
class TextModifier extends SpecializableModifier class TextModifier extends SpecializableModifier
{ {
public function __construct( public function __construct(
public string $text, public string $text,
public Point $position, public PointInterface $position,
public FontInterface $font public FontInterface $font
) { ) {
} }
@@ -69,7 +70,7 @@ class TextModifier extends SpecializableModifier
* Return array of offset points to draw text stroke effect below the actual text * Return array of offset points to draw text stroke effect below the actual text
* *
* @param FontInterface $font * @param FontInterface $font
* @return array<Point> * @return array<PointInterface>
*/ */
protected function strokeOffsets(FontInterface $font): array protected function strokeOffsets(FontInterface $font): array
{ {

View File

@@ -75,10 +75,10 @@ class Line implements IteratorAggregate, Countable
/** /**
* Set position of current line * Set position of current line
* *
* @param Point $point * @param PointInterface $point
* @return Line * @return Line
*/ */
public function setPosition(Point $point): self public function setPosition(PointInterface $point): self
{ {
$this->position = $point; $this->position = $point;

View File

@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Geometry\Factories;
use Intervention\Image\Geometry\Factories\BezierFactory;
use Intervention\Image\Geometry\Factories\CircleFactory;
use Intervention\Image\Geometry\Factories\Drawable;
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\Tests\BaseTestCase;
final class DrawableTest extends BaseTestCase
{
public function testBezier(): void
{
$this->assertInstanceOf(BezierFactory::class, Drawable::bezier());
}
public function testCircle(): void
{
$this->assertInstanceOf(CircleFactory::class, Drawable::circle());
}
public function testEllipse(): void
{
$this->assertInstanceOf(EllipseFactory::class, Drawable::ellipse());
}
public function testLine(): void
{
$this->assertInstanceOf(LineFactory::class, Drawable::line());
}
public function testPolygon(): void
{
$this->assertInstanceOf(PolygonFactory::class, Drawable::polygon());
}
public function testRectangle(): void
{
$this->assertInstanceOf(RectangleFactory::class, Drawable::rectangle());
}
}