1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-27 07:44:30 +02:00

Adjust modifiers for new color model

This commit is contained in:
Oliver Vogel
2023-10-20 15:58:35 +02:00
parent f24a337058
commit df4c3e0ddd
18 changed files with 113 additions and 49 deletions

View File

@@ -0,0 +1,23 @@
<?php
namespace Intervention\Image\Colors\Rgb\Decoders;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ColorInterface;
class TransparentColorDecoder extends HexColorDecoder
{
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input)) {
throw new DecoderException('Unable to decode input');
}
if (strtolower($input) != 'transparent') {
throw new DecoderException('Unable to decode input');
}
return parent::decode('#00000000');
}
}

View File

@@ -31,7 +31,7 @@ class AbstractDrawModifier
return $this->drawable;
}
protected function getBackgroundColor(): ?ColorInterface
protected function getBackgroundColor(): ColorInterface
{
try {
$color = $this->handleInput($this->drawable->getBackgroundColor());
@@ -42,7 +42,7 @@ class AbstractDrawModifier
return $color;
}
protected function getBorderColor(): ?ColorInterface
protected function getBorderColor(): ColorInterface
{
try {
$color = $this->handleInput($this->drawable->getBorderColor());

View File

@@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Gd;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder;
use Intervention\Image\Colors\Rgb\Decoders\TransparentColorDecoder;
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
use Intervention\Image\Drivers\Gd\Decoders\ImageObjectDecoder;
use Intervention\Image\Drivers\Gd\Decoders\ColorObjectDecoder;
@@ -21,7 +22,7 @@ class InputHandler extends AbstractInputHandler
ColorObjectDecoder::class,
HexColorDecoder::class,
StringColorDecoder::class,
// Decoders\TransparentColorDecoder::class,
TransparentColorDecoder::class,
HtmlColornameDecoder::class,
FilePointerImageDecoder::class,
FilePathImageDecoder::class,

View File

@@ -3,11 +3,14 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
return $image->eachFrame(function ($frame) {
@@ -20,7 +23,7 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
$this->position->getY(),
$this->ellipse()->getWidth() - 1,
$this->ellipse()->getHeight() - 1,
$this->getBackgroundColor()->toInt()
$this->colorToInteger($this->getBackgroundColor())
);
}
@@ -36,7 +39,7 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
$this->ellipse()->getHeight(),
0,
360,
$this->getBorderColor()->toInt()
$this->colorToInteger($this->getBorderColor())
);
} else {
imagefilledellipse(
@@ -45,7 +48,7 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf
$this->position->getY(),
$this->ellipse()->getWidth(),
$this->ellipse()->getHeight(),
$this->getBackgroundColor()->toInt()
$this->colorToInteger($this->getBackgroundColor())
);
}
});

View File

@@ -3,11 +3,14 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
return $image->eachFrame(function ($frame) {
@@ -17,7 +20,7 @@ class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
$this->line()->getStart()->getY(),
$this->line()->getEnd()->getX(),
$this->line()->getEnd()->getY(),
$this->getBackgroundColor()->toInt()
$this->colorToInteger($this->getBackgroundColor())
);
});
}

View File

@@ -3,12 +3,15 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function __construct(
protected DrawableInterface $drawable
) {
@@ -22,7 +25,7 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf
imagefilledpolygon(
$frame->getCore(),
$this->polygon()->toArray(),
$this->getBackgroundColor()->toInt()
$this->colorToInteger($this->getBackgroundColor())
);
}
@@ -32,7 +35,7 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf
$frame->getCore(),
$this->polygon()->toArray(),
$this->polygon()->count(),
$this->getBorderColor()->toInt()
$this->colorToInteger($this->getBorderColor())
);
}
});

View File

@@ -3,11 +3,14 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
$image->eachFrame(function ($frame) {
@@ -19,7 +22,7 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte
$this->position->getY(),
$this->position->getX() + $this->rectangle()->bottomRightPoint()->getX(),
$this->position->getY() + $this->rectangle()->bottomRightPoint()->getY(),
$this->getBackgroundColor()->toInt()
$this->colorToInteger($this->getBackgroundColor())
);
}
@@ -32,7 +35,7 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte
$this->position->getY(),
$this->position->getX() + $this->rectangle()->bottomRightPoint()->getX(),
$this->position->getY() + $this->rectangle()->bottomRightPoint()->getY(),
$this->getBorderColor()->toInt()
$this->colorToInteger($this->getBorderColor())
);
}
});

View File

@@ -3,7 +3,7 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractPadModifier;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\FrameInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
@@ -13,12 +13,13 @@ use Intervention\Image\Traits\CanHandleInput;
class PadModifier extends AbstractPadModifier implements ModifierInterface
{
use CanHandleInput;
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
$crop = $this->getCropSize($image);
$resize = $this->getResizeSize($image);
$background = $this->handleInput($this->background);
$background = $this->colorToInteger($this->handleInput($this->background));
foreach ($image as $frame) {
$this->modify($frame, $crop, $resize, $background);
@@ -31,7 +32,7 @@ class PadModifier extends AbstractPadModifier implements ModifierInterface
FrameInterface $frame,
SizeInterface $crop,
SizeInterface $resize,
ColorInterface $background
int $background
): void {
// create new image
$modified = imagecreatetruecolor(
@@ -39,7 +40,7 @@ class PadModifier extends AbstractPadModifier implements ModifierInterface
$resize->getHeight()
);
imagefill($modified, 0, 0, $background->toInt());
imagefill($modified, 0, 0, $background);
// get current image
$current = $frame->getCore();

View File

@@ -3,11 +3,14 @@
namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractRotateModifier;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class RotateModifier extends AbstractRotateModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
foreach ($image as $frame) {
@@ -15,7 +18,7 @@ class RotateModifier extends AbstractRotateModifier implements ModifierInterface
imagerotate(
$frame->getCore(),
$this->rotationAngle(),
$this->backgroundColor()->toInt()
$this->colorToInteger($this->backgroundColor())
)
);
}

View File

@@ -4,14 +4,18 @@ namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Drivers\Abstract\AbstractTextWriter;
use Intervention\Image\Drivers\Gd\Font;
use Intervention\Image\Drivers\Gd\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
class TextWriter extends AbstractTextWriter
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
$lines = $this->getAlignedTextBlock();
$font = $this->failIfNotClass($this->getFont(), Font::class);
$color = $this->colorToInteger($font->getColor());
foreach ($image as $frame) {
if ($this->font->hasFilename()) {
@@ -22,7 +26,7 @@ class TextWriter extends AbstractTextWriter
$font->getAngle() * (-1),
$line->getPosition()->getX(),
$line->getPosition()->getY(),
$font->getColor()->toInt(),
$color,
$font->getFilename(),
$line
);
@@ -35,7 +39,7 @@ class TextWriter extends AbstractTextWriter
$line->getPosition()->getX(),
$line->getPosition()->getY(),
$line,
$this->font->getColor()->toInt()
$color
);
}
}

View File

@@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Imagick;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\StringColorDecoder;
use Intervention\Image\Colors\Rgb\Decoders\HtmlColornameDecoder;
use Intervention\Image\Colors\Rgb\Decoders\TransparentColorDecoder;
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
use Intervention\Image\Drivers\Imagick\Decoders\ImageObjectDecoder;
use Intervention\Image\Drivers\Imagick\Decoders\ColorObjectDecoder;
@@ -21,7 +22,7 @@ class InputHandler extends AbstractInputHandler
ColorObjectDecoder::class,
HexColorDecoder::class,
StringColorDecoder::class,
// Decoders\TransparentColorDecoder::class,
TransparentColorDecoder::class,
HtmlColornameDecoder::class,
FilePointerImageDecoder::class,
FilePathImageDecoder::class,

View File

@@ -4,24 +4,26 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
$background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
$border_color = $this->failIfNotClass($this->getBorderColor(), Color::class);
$background_color = $this->colorToPixel($this->getBackgroundColor());
$border_color = $this->colorToPixel($this->getBorderColor());
return $image->eachFrame(function ($frame) use ($background_color, $border_color) {
$drawing = new ImagickDraw();
$drawing->setFillColor($background_color->getPixel());
$drawing->setFillColor($background_color);
if ($this->ellipse()->hasBorder()) {
$drawing->setStrokeWidth($this->ellipse()->getBorderSize());
$drawing->setStrokeColor($border_color->getPixel());
$drawing->setStrokeColor($border_color);
}
$drawing->ellipse(

View File

@@ -4,18 +4,22 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
$drawing = new ImagickDraw();
$color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
$drawing->setStrokeColor($color->getPixel());
$drawing->setStrokeWidth($this->line()->getWidth());
$drawing->setStrokeColor(
$this->colorToPixel($this->getBackgroundColor())
);
$drawing->line(
$this->line()->getStart()->getX(),
$this->line()->getStart()->getY(),

View File

@@ -4,13 +4,15 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Interfaces\DrawableInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function __construct(
protected DrawableInterface $drawable
) {
@@ -20,15 +22,15 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf
public function apply(ImageInterface $image): ImageInterface
{
$drawing = new ImagickDraw();
$background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
$border_color = $this->failIfNotClass($this->getBorderColor(), Color::class);
$background_color = $this->colorToPixel($this->getBackgroundColor());
$border_color = $this->colorToPixel($this->getBorderColor());
if ($this->polygon()->hasBackgroundColor()) {
$drawing->setFillColor($background_color->getPixel());
$drawing->setFillColor($background_color);
}
if ($this->polygon()->hasBorder()) {
$drawing->setStrokeColor($border_color->getPixel());
$drawing->setStrokeColor($border_color);
$drawing->setStrokeWidth($this->polygon()->getBorderSize());
}

View File

@@ -4,22 +4,23 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers;
use ImagickDraw;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
// setup
$drawing = new ImagickDraw();
$background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class);
$border_color = $this->failIfNotClass($this->getBorderColor(), Color::class);
$background_color = $this->colorToPixel($this->getBackgroundColor());
$border_color = $this->colorToPixel($this->getBorderColor());
$drawing->setFillColor($background_color->getPixel());
$drawing->setFillColor($background_color);
if ($this->rectangle()->hasBorder()) {
$drawing->setStrokeColor($border_color->getPixel());
$drawing->setStrokeColor($border_color);
$drawing->setStrokeWidth($this->rectangle()->getBorderSize());
}

View File

@@ -3,19 +3,19 @@
namespace Intervention\Image\Drivers\Imagick\Modifiers;
use Intervention\Image\Drivers\Abstract\Modifiers\AbstractRotateModifier;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Drivers\Imagick\Traits\CanHandleColors;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
class RotateModifier extends AbstractRotateModifier implements ModifierInterface
{
use CanHandleColors;
public function apply(ImageInterface $image): ImageInterface
{
$background = $this->failIfNotClass($this->backgroundColor(), Color::class);
foreach ($image as $frame) {
$frame->getCore()->rotateImage(
$background->getPixel(),
$this->colorToPixel($this->backgroundColor()),
$this->rotationAngle()
);
}

View File

@@ -105,11 +105,12 @@ class GdInputHandlerTest extends TestCase
$this->assertEquals([10, 20, 30, 255], $result->toArray());
}
// public function testHandleTransparent(): void
// {
// $handler = new InputHandler();
// $input = 'transparent';
// $result = $handler->handle($input);
// $this->assertInstanceOf(Color::class, $result);
// }
public function testHandleTransparent(): void
{
$handler = new InputHandler();
$input = 'transparent';
$result = $handler->handle($input);
$this->assertInstanceOf(Color::class, $result);
$this->assertEquals([0, 0, 0, 0], $result->toArray());
}
}

View File

@@ -104,4 +104,13 @@ class InputHandlerTest extends TestCase
$this->assertInstanceOf(Color::class, $result);
$this->assertEquals([10, 20, 30, 255], $result->toArray());
}
public function testHandleTransparent(): void
{
$handler = new InputHandler();
$input = 'transparent';
$result = $handler->handle($input);
$this->assertInstanceOf(Color::class, $result);
$this->assertEquals([0, 0, 0, 0], $result->toArray());
}
}