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:
@@ -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(
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
41
src/Drivers/Gd/Modifiers/DrawPolygonModifier.php
Normal file
41
src/Drivers/Gd/Modifiers/DrawPolygonModifier.php
Normal 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()
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
47
src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php
Normal file
47
src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user