Skip to content

Commit

Permalink
Add drawing shortcut methods
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 26, 2023
1 parent 8a698b6 commit 7c6a8fc
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Geometry/Factories/CircleFactory.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/Geometry/Factories/EllipseFactory.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
71 changes: 71 additions & 0 deletions src/Geometry/Factories/LineFactory.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/Geometry/Factories/PolygonFactory.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 46 additions & 0 deletions src/Geometry/Factories/RectangleFactory.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
80 changes: 80 additions & 0 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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}
*
Expand Down
56 changes: 56 additions & 0 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 7c6a8fc

Please sign in to comment.