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

Implement DrawPixelModifier

This commit is contained in:
Oliver Vogel
2022-07-07 16:39:27 +02:00
parent b80b097615
commit 6092ec113f
6 changed files with 133 additions and 0 deletions

View File

@@ -200,6 +200,14 @@ abstract class AbstractImage implements ImageInterface
return $this->modify($modifier); 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 public function resize(?int $width = null, ?int $height = null): ImageInterface
{ {
return $this->modify( return $this->modify(

View 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;
}
}

View 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;
}
}

View File

@@ -36,6 +36,7 @@ interface ImageInterface extends Traversable, Countable
public function fitDown(int $width, int $height, string $position = 'center'): ImageInterface; 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 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 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 getWidth(): int;
public function getHeight(): int; public function getHeight(): int;
public function destroy(): void; public function destroy(): void;

View 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());
}
}

View 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());
}
}