1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 01:29:51 +02:00

Add Image::drawPolygon()

This commit is contained in:
Oliver Vogel
2022-07-25 11:14:10 +02:00
parent 7a103cbfa8
commit 105e11d1d5
4 changed files with 146 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ use Intervention\Image\Geometry\Circle;
use Intervention\Image\Geometry\Ellipse;
use Intervention\Image\Geometry\Line;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Geometry\Polygon;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\CollectionInterface;
use Intervention\Image\Interfaces\EncoderInterface;
@@ -254,6 +255,14 @@ abstract class AbstractImage implements ImageInterface
return $this->modify($modifier);
}
public function drawPolygon(callable $init = null): ImageInterface
{
$polygon = $this->runCallback($init, new Polygon());
$modifier = $this->resolveDriverClass('Modifiers\DrawPolygonModifier', $polygon);
return $this->modify($modifier);
}
public function resize(?int $width = null, ?int $height = null): ImageInterface
{
return $this->modify(

View File

@@ -3,6 +3,11 @@
namespace Intervention\Image\Drivers\Abstract\Modifiers;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\GeometryException;
use Intervention\Image\Geometry\Ellipse;
use Intervention\Image\Geometry\Line;
use Intervention\Image\Geometry\Polygon;
use Intervention\Image\Geometry\Rectangle;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\PointInterface;
@@ -45,4 +50,48 @@ class AbstractDrawModifier
return $color;
}
public function polygon(): Polygon
{
if (!is_a($this->drawable(), Polygon::class)) {
throw new GeometryException(
'Shape mismatch. Excepted Polygon::class, found ' . get_class($this->drawable())
);
}
return $this->drawable();
}
public function ellipse(): Ellipse
{
if (!is_a($this->drawable(), Ellipse::class)) {
throw new GeometryException(
'Shape mismatch. Excepted Ellipse::class, found ' . get_class($this->drawable())
);
}
return $this->drawable();
}
public function line(): Line
{
if (!is_a($this->drawable(), Line::class)) {
throw new GeometryException(
'Shape mismatch. Excepted Line::class, found ' . get_class($this->drawable())
);
}
return $this->drawable();
}
public function rectangle(): Rectangle
{
if (!is_a($this->drawable(), Rectangle::class)) {
throw new GeometryException(
'Shape mismatch. Excepted Rectangle::class, found ' . get_class($this->drawable())
);
}
return $this->drawable();
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterface
{
public function __construct(
protected DrawableInterface $drawable
) {
//
}
public function apply(ImageInterface $image): ImageInterface
{
return $image->eachFrame(function ($frame) {
if ($this->polygon()->hasBackgroundColor()) {
imagefilledpolygon(
$frame->getCore(),
$this->polygon()->toArray(),
$this->polygon()->count(),
$this->getBackgroundColor()->toInt()
);
}
if ($this->polygon()->hasBorder()) {
imagesetthickness($frame->getCore(), $this->polygon()->getBorderSize());
imagepolygon(
$frame->getCore(),
$this->polygon()->toArray(),
$this->polygon()->count(),
$this->getBorderColor()->toInt()
);
}
});
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterface
{
public function __construct(
protected DrawableInterface $drawable
) {
//
}
public function apply(ImageInterface $image): ImageInterface
{
$drawing = new ImagickDraw();
if ($this->polygon()->hasBackgroundColor()) {
$drawing->setFillColor($this->getBackgroundColor()->getPixel());
}
if ($this->polygon()->hasBorder()) {
$drawing->setStrokeColor($this->getBorderColor()->getPixel());
$drawing->setStrokeWidth($this->polygon()->getBorderSize());
}
$drawing->polygon($this->points());
return $image->eachFrame(function ($frame) use ($drawing) {
$frame->getCore()->drawImage($drawing);
});
}
private function points(): array
{
$points = [];
foreach ($this->polygon() as $point) {
$points[] = ['x' => $point->getX(), 'y' => $point->getY()];
}
return $points;
}
}