mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 08:40:33 +02:00
Implement DrawPixelModifier
This commit is contained in:
@@ -200,6 +200,14 @@ abstract class AbstractImage implements ImageInterface
|
||||
return $this->modify($modifier);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public function resize(?int $width = null, ?int $height = null): ImageInterface
|
||||
{
|
||||
return $this->modify(
|
||||
|
30
src/Drivers/Gd/Modifiers/DrawPixelModifier.php
Normal file
30
src/Drivers/Gd/Modifiers/DrawPixelModifier.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
class DrawPixelModifier implements ModifierInterface
|
||||
{
|
||||
public function __construct(protected Point $position, protected ColorInterface $color)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
foreach ($image as $frame) {
|
||||
imagesetpixel(
|
||||
$frame->getCore(),
|
||||
$this->position->getX(),
|
||||
$this->position->getY(),
|
||||
$this->color->toInt()
|
||||
);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
}
|
41
src/Drivers/Imagick/Modifiers/DrawPixelModifier.php
Normal file
41
src/Drivers/Imagick/Modifiers/DrawPixelModifier.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
|
||||
class DrawPixelModifier implements ModifierInterface
|
||||
{
|
||||
public function __construct(protected Point $position, protected ColorInterface $color)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function apply(ImageInterface $image): ImageInterface
|
||||
{
|
||||
$pixel = new ImagickDraw();
|
||||
$pixel->setFillColor($this->getColor()->getPixel());
|
||||
$pixel->point($this->position->getX(), $this->position->getY());
|
||||
|
||||
foreach ($image as $frame) {
|
||||
$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;
|
||||
}
|
||||
}
|
@@ -36,6 +36,7 @@ interface ImageInterface extends Traversable, Countable
|
||||
public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface;
|
||||
public function pad(int $width, int $height, $background = 'ffffff', string $position = 'center'): ImageInterface;
|
||||
public function padDown(int $width, int $height, $background = 'ffffff', string $position = 'center'): ImageInterface;
|
||||
public function drawPixel(int $x, int $y, $color = null): ImageInterface;
|
||||
public function getWidth(): int;
|
||||
public function getHeight(): int;
|
||||
public function destroy(): void;
|
||||
|
26
tests/Drivers/Gd/Modifiers/DrawPixelModifierTest.php
Normal file
26
tests/Drivers/Gd/Modifiers/DrawPixelModifierTest.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
use Intervention\Image\Tests\Traits\CanCreateGdTestImage;
|
||||
|
||||
/**
|
||||
* @requires extension gd
|
||||
* @covers \Intervention\Image\Drivers\Gd\Modifiers\DrawPixelModifier
|
||||
*/
|
||||
class DrawPixelModifierTest extends TestCase
|
||||
{
|
||||
use CanCreateGdTestImage;
|
||||
|
||||
public function testApply(): void
|
||||
{
|
||||
$image = $this->createTestImage('trim.png');
|
||||
$this->assertEquals('00aef0', $image->pickColor(14, 14)->toHex());
|
||||
$image->modify(new DrawPixelModifier(new Point(14, 14), new Color(16777215)));
|
||||
$this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex());
|
||||
}
|
||||
}
|
27
tests/Drivers/Imagick/Modifiers/DrawPixelModifierTest.php
Normal file
27
tests/Drivers/Imagick/Modifiers/DrawPixelModifierTest.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
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;
|
||||
use Intervention\Image\Tests\Traits\CanCreateImagickTestImage;
|
||||
|
||||
/**
|
||||
* @requires extension gd
|
||||
* @covers \Intervention\Image\Drivers\Gd\Modifiers\DrawPixelModifier
|
||||
*/
|
||||
class DrawPixelModifierTest extends TestCase
|
||||
{
|
||||
use CanCreateImagickTestImage;
|
||||
|
||||
public function testApply(): void
|
||||
{
|
||||
$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'))));
|
||||
$this->assertEquals('ffffff', $image->pickColor(14, 14)->toHex());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user