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

Refactor driver method

This commit is contained in:
Oliver Vogel
2023-11-25 14:30:59 +01:00
parent 12ecc4b81f
commit 516e963883
18 changed files with 47 additions and 79 deletions

View File

@@ -50,9 +50,4 @@ class Driver extends AbstractDriver
{ {
return new FontProcessor($font); return new FontProcessor($font);
} }
public function colorToNative(ColorInterface $color, ColorspaceInterface $colorspace): mixed
{
return (new ColorProcessor($colorspace))->colorToNative($color);
}
} }

View File

@@ -19,9 +19,8 @@ class DrawEllipseModifier extends DrawModifier
$this->position()->y(), $this->position()->y(),
$this->drawable->width() - 1, $this->drawable->width() - 1,
$this->drawable->height() - 1, $this->drawable->height() - 1,
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
) )
); );
} }
@@ -41,9 +40,8 @@ class DrawEllipseModifier extends DrawModifier
$this->drawable->height(), $this->drawable->height(),
0, 0,
360, 360,
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->borderColor(), $this->borderColor()
$image->colorspace()
) )
); );
} else { } else {
@@ -53,9 +51,8 @@ class DrawEllipseModifier extends DrawModifier
$this->position()->y(), $this->position()->y(),
$this->drawable->width(), $this->drawable->width(),
$this->drawable->height(), $this->drawable->height(),
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
) )
); );
} }

View File

@@ -16,9 +16,8 @@ class DrawLineModifier extends DrawModifier
$this->drawable->start()->y(), $this->drawable->start()->y(),
$this->drawable->end()->x(), $this->drawable->end()->x(),
$this->drawable->end()->y(), $this->drawable->end()->y(),
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
) )
); );
} }

View File

@@ -9,9 +9,8 @@ class DrawPixelModifier extends DriverModifier
{ {
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
$color = $this->driver()->colorToNative( $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->color), $this->driver()->handleInput($this->color)
$image->colorspace()
); );
foreach ($image as $frame) { foreach ($image as $frame) {

View File

@@ -14,9 +14,8 @@ class DrawPolygonModifier extends DrawModifier
imagefilledpolygon( imagefilledpolygon(
$frame->native(), $frame->native(),
$this->drawable->toArray(), $this->drawable->toArray(),
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
) )
); );
} }
@@ -26,9 +25,8 @@ class DrawPolygonModifier extends DrawModifier
imagepolygon( imagepolygon(
$frame->native(), $frame->native(),
$this->drawable->toArray(), $this->drawable->toArray(),
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->borderColor(), $this->borderColor()
$image->colorspace()
) )
); );
} }

View File

@@ -18,9 +18,8 @@ class DrawRectangleModifier extends DrawModifier
$this->position()->y(), $this->position()->y(),
$this->position()->x() + $this->drawable->width(), $this->position()->x() + $this->drawable->width(),
$this->position()->y() + $this->drawable->height(), $this->position()->y() + $this->drawable->height(),
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
) )
); );
} }
@@ -34,9 +33,8 @@ class DrawRectangleModifier extends DrawModifier
$this->position()->y(), $this->position()->y(),
$this->position()->x() + $this->drawable->width(), $this->position()->x() + $this->drawable->width(),
$this->position()->y() + $this->drawable->height(), $this->position()->y() + $this->drawable->height(),
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->borderColor(), $this->borderColor()
$image->colorspace()
) )
); );
} }

View File

@@ -2,7 +2,6 @@
namespace Intervention\Image\Drivers\Gd\Modifiers; namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Colors\Rgb\Colorspace;
use Intervention\Image\Drivers\DriverModifier; use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Drivers\Gd\Frame; use Intervention\Image\Drivers\Gd\Frame;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
@@ -11,7 +10,7 @@ class FillModifier extends DriverModifier
{ {
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
$color = $this->color(); $color = $this->color($image);
foreach ($image as $frame) { foreach ($image as $frame) {
if ($this->hasPosition()) { if ($this->hasPosition()) {
@@ -24,11 +23,10 @@ class FillModifier extends DriverModifier
return $image; return $image;
} }
private function color(): int private function color(ImageInterface $image): int
{ {
return $this->driver()->colorToNative( return $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->color), $this->driver()->handleInput($this->color)
new Colorspace()
); );
} }

View File

@@ -2,7 +2,6 @@
namespace Intervention\Image\Drivers\Gd\Modifiers; namespace Intervention\Image\Drivers\Gd\Modifiers;
use Intervention\Image\Colors\Rgb\Colorspace;
use Intervention\Image\Drivers\DriverModifier; use Intervention\Image\Drivers\DriverModifier;
use Intervention\Image\Drivers\Gd\FontProcessor; use Intervention\Image\Drivers\Gd\FontProcessor;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
@@ -14,9 +13,8 @@ class TextModifier extends DriverModifier
$processor = $this->fontProcessor(); $processor = $this->fontProcessor();
$lines = $processor->alignedTextBlock($this->position, $this->text); $lines = $processor->alignedTextBlock($this->position, $this->text);
$color = $this->driver()->colorToNative( $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->font->color()), $this->driver()->handleInput($this->font->color())
new Colorspace()
); );
foreach ($image as $frame) { foreach ($image as $frame) {

View File

@@ -48,9 +48,4 @@ class Driver extends AbstractDriver
{ {
return new FontProcessor($font); return new FontProcessor($font);
} }
public function colorToNative(ColorInterface $color, ColorspaceInterface $colorspace): mixed
{
return (new ColorProcessor($colorspace))->colorToNative($color);
}
} }

View File

@@ -10,14 +10,12 @@ class DrawEllipseModifier extends DrawModifier
{ {
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
$background_color = $this->driver()->colorToNative( $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
); );
$border_color = $this->driver()->colorToNative( $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->borderColor(), $this->borderColor()
$image->colorspace()
); );
foreach ($image as $frame) { foreach ($image as $frame) {

View File

@@ -13,9 +13,8 @@ class DrawLineModifier extends DrawModifier
$drawing = new ImagickDraw(); $drawing = new ImagickDraw();
$drawing->setStrokeWidth($this->drawable->width()); $drawing->setStrokeWidth($this->drawable->width());
$drawing->setStrokeColor( $drawing->setStrokeColor(
$this->driver()->colorToNative( $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
) )
); );

View File

@@ -10,9 +10,8 @@ class DrawPixelModifier extends DriverModifier
{ {
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
$color = $this->driver()->colorToNative( $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->color), $this->driver()->handleInput($this->color)
$image->colorspace()
); );
$pixel = new ImagickDraw(); $pixel = new ImagickDraw();

View File

@@ -13,18 +13,16 @@ class DrawPolygonModifier extends DrawModifier
$drawing = new ImagickDraw(); $drawing = new ImagickDraw();
if ($this->drawable->hasBackgroundColor()) { if ($this->drawable->hasBackgroundColor()) {
$background_color = $this->driver()->colorToNative( $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace()
); );
$drawing->setFillColor($background_color); $drawing->setFillColor($background_color);
} }
if ($this->drawable->hasBorder()) { if ($this->drawable->hasBorder()) {
$border_color = $this->driver()->colorToNative( $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->borderColor(), $this->borderColor()
$image->colorspace()
); );
$drawing->setStrokeColor($border_color); $drawing->setStrokeColor($border_color);

View File

@@ -12,14 +12,12 @@ class DrawRectangleModifier extends DrawModifier
{ {
$drawing = new ImagickDraw(); $drawing = new ImagickDraw();
$background_color = $this->driver()->colorToNative( $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->backgroundColor(), $this->backgroundColor()
$image->colorspace(),
); );
$border_color = $this->driver()->colorToNative( $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->borderColor(), $this->borderColor()
$image->colorspace(),
); );
$drawing->setFillColor($background_color); $drawing->setFillColor($background_color);

View File

@@ -14,7 +14,9 @@ class FillModifier extends DriverModifier
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
$color = $this->driver()->handleInput($this->color); $color = $this->driver()->handleInput($this->color);
$pixel = $this->driver()->colorToNative($color, $image->colorspace()); $pixel = $this->driver()
->colorProcessor($image->colorspace())
->colorToNative($color, $image->colorspace());
foreach ($image as $frame) { foreach ($image as $frame) {
if ($this->hasPosition()) { if ($this->hasPosition()) {

View File

@@ -9,9 +9,8 @@ class RotateModifier extends DriverModifier
{ {
public function apply(ImageInterface $image): ImageInterface public function apply(ImageInterface $image): ImageInterface
{ {
$background = $this->driver()->colorToNative( $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->background), $this->driver()->handleInput($this->background)
$image->colorspace()
); );
foreach ($image as $frame) { foreach ($image as $frame) {

View File

@@ -13,9 +13,8 @@ class TextModifier extends DriverModifier
$processor = $this->fontProcessor(); $processor = $this->fontProcessor();
$lines = $processor->alignedTextBlock($this->position, $this->text); $lines = $processor->alignedTextBlock($this->position, $this->text);
$color = $this->driver()->colorToNative( $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->font->color()), $this->driver()->handleInput($this->font->color())
$image->colorspace()
); );
$draw = $processor->toImagickDraw($color); $draw = $processor->toImagickDraw($color);

View File

@@ -10,5 +10,4 @@ interface DriverInterface
public function handleInput(mixed $input): ImageInterface|ColorInterface; public function handleInput(mixed $input): ImageInterface|ColorInterface;
public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface; public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface;
public function fontProcessor(FontInterface $font): FontProcessorInterface; public function fontProcessor(FontInterface $font): FontProcessorInterface;
public function colorToNative(ColorInterface $color, ColorspaceInterface $colorspace): mixed;
} }