1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-30 17:19:50 +02:00

Refactor draw modifiers

This commit is contained in:
Oliver Vogel
2022-08-12 17:27:59 +02:00
parent f54b18e01c
commit 7be06787a3
6 changed files with 28 additions and 36 deletions

View File

@@ -217,10 +217,9 @@ abstract class AbstractImage implements ImageInterface
public function drawPixel(int $x, int $y, $color = null): ImageInterface
{
$color = $this->handleInput($color);
$modifier = $this->resolveDriverClass('Modifiers\DrawPixelModifier', new Point($x, $y), $color);
return $this->modify($modifier);
return $this->modify(
$this->resolveDriverClass('Modifiers\DrawPixelModifier', new Point($x, $y), $color)
);
}
public function drawRectangle(int $x, int $y, ?callable $init = null): ImageInterface

View File

@@ -3,28 +3,31 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Traits\CanHandleInput;
class DrawPixelModifier implements ModifierInterface
{
public function __construct(protected Point $position, protected ColorInterface $color)
{
use CanHandleInput;
public function __construct(
protected Point $position,
protected $color
) {
//
}
public function apply(ImageInterface $image): ImageInterface
{
foreach ($image as $frame) {
$color = $this->handleInput($this->color);
return $image->eachFrame(function ($frame) use ($color) {
imagesetpixel(
$frame->getCore(),
$this->position->getX(),
$this->position->getY(),
$this->color->toInt()
$color->toInt()
);
}
return $image;
});
}
}

View File

@@ -3,39 +3,31 @@
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Traits\CanHandleInput;
class DrawPixelModifier implements ModifierInterface
{
public function __construct(protected Point $position, protected ColorInterface $color)
{
use CanHandleInput;
public function __construct(
protected Point $position,
protected $color
) {
//
}
public function apply(ImageInterface $image): ImageInterface
{
$color = $this->handleInput($this->color);
$pixel = new ImagickDraw();
$pixel->setFillColor($this->getColor()->getPixel());
$pixel->setFillColor($color->getPixel());
$pixel->point($this->position->getX(), $this->position->getY());
foreach ($image as $frame) {
return $image->eachFrame(function ($frame) use ($pixel) {
$frame->getCore()->drawImage($pixel);
}
return $image;
}
public function getColor(): Color
{
if (!is_a($this->color, Color::class)) {
throw new DecoderException('Unable to decode given pixel color.');
}
return $this->color;
});
}
}

View File

@@ -11,6 +11,7 @@ use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\PointInterface;
use Intervention\Image\Tests\TestCase;
use Mockery;

View File

@@ -2,7 +2,6 @@
namespace Intervention\Image\Tests\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Drivers\Gd\Modifiers\DrawPixelModifier;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Tests\TestCase;
@@ -20,7 +19,7 @@ class DrawPixelModifierTest extends TestCase
{
$image = $this->createTestImage('trim.png');
$this->assertEquals('00aef0', $image->pickColor(14, 14)->toHex());
$image->modify(new DrawPixelModifier(new Point(14, 14), new Color(16777215)));
$image->modify(new DrawPixelModifier(new Point(14, 14), 'ffffff'));
$this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex());
}
}

View File

@@ -2,8 +2,6 @@
namespace Intervention\Image\Tests\Drivers\Imagick\Modifiers;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Modifiers\DrawPixelModifier;
use Intervention\Image\Geometry\Point;
use Intervention\Image\Tests\TestCase;
@@ -21,7 +19,7 @@ class DrawPixelModifierTest extends TestCase
{
$image = $this->createTestImage('trim.png');
$this->assertEquals('00aef0', $image->pickColor(14, 14)->toHex());
$image->modify(new DrawPixelModifier(new Point(14, 14), new Color(new ImagickPixel('#ffffff'))));
$image->modify(new DrawPixelModifier(new Point(14, 14), 'ffffff'));
$this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex());
}
}