From 516e9638837b8860ef03ba4bafe08d4f77c8a68f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 25 Nov 2023 14:30:59 +0100 Subject: [PATCH] Refactor driver method --- src/Drivers/Gd/Driver.php | 5 ----- src/Drivers/Gd/Modifiers/DrawEllipseModifier.php | 15 ++++++--------- src/Drivers/Gd/Modifiers/DrawLineModifier.php | 5 ++--- src/Drivers/Gd/Modifiers/DrawPixelModifier.php | 5 ++--- src/Drivers/Gd/Modifiers/DrawPolygonModifier.php | 10 ++++------ .../Gd/Modifiers/DrawRectangleModifier.php | 10 ++++------ src/Drivers/Gd/Modifiers/FillModifier.php | 10 ++++------ src/Drivers/Gd/Modifiers/TextModifier.php | 6 ++---- src/Drivers/Imagick/Driver.php | 5 ----- .../Imagick/Modifiers/DrawEllipseModifier.php | 10 ++++------ .../Imagick/Modifiers/DrawLineModifier.php | 5 ++--- .../Imagick/Modifiers/DrawPixelModifier.php | 5 ++--- .../Imagick/Modifiers/DrawPolygonModifier.php | 10 ++++------ .../Imagick/Modifiers/DrawRectangleModifier.php | 10 ++++------ src/Drivers/Imagick/Modifiers/FillModifier.php | 4 +++- src/Drivers/Imagick/Modifiers/RotateModifier.php | 5 ++--- src/Drivers/Imagick/Modifiers/TextModifier.php | 5 ++--- src/Interfaces/DriverInterface.php | 1 - 18 files changed, 47 insertions(+), 79 deletions(-) diff --git a/src/Drivers/Gd/Driver.php b/src/Drivers/Gd/Driver.php index 3781089e..9c815103 100644 --- a/src/Drivers/Gd/Driver.php +++ b/src/Drivers/Gd/Driver.php @@ -50,9 +50,4 @@ class Driver extends AbstractDriver { return new FontProcessor($font); } - - public function colorToNative(ColorInterface $color, ColorspaceInterface $colorspace): mixed - { - return (new ColorProcessor($colorspace))->colorToNative($color); - } } diff --git a/src/Drivers/Gd/Modifiers/DrawEllipseModifier.php b/src/Drivers/Gd/Modifiers/DrawEllipseModifier.php index cd6ff3a1..d200497b 100644 --- a/src/Drivers/Gd/Modifiers/DrawEllipseModifier.php +++ b/src/Drivers/Gd/Modifiers/DrawEllipseModifier.php @@ -19,9 +19,8 @@ class DrawEllipseModifier extends DrawModifier $this->position()->y(), $this->drawable->width() - 1, $this->drawable->height() - 1, - $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ) ); } @@ -41,9 +40,8 @@ class DrawEllipseModifier extends DrawModifier $this->drawable->height(), 0, 360, - $this->driver()->colorToNative( - $this->borderColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->borderColor() ) ); } else { @@ -53,9 +51,8 @@ class DrawEllipseModifier extends DrawModifier $this->position()->y(), $this->drawable->width(), $this->drawable->height(), - $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ) ); } diff --git a/src/Drivers/Gd/Modifiers/DrawLineModifier.php b/src/Drivers/Gd/Modifiers/DrawLineModifier.php index b05153fb..0e6b415a 100644 --- a/src/Drivers/Gd/Modifiers/DrawLineModifier.php +++ b/src/Drivers/Gd/Modifiers/DrawLineModifier.php @@ -16,9 +16,8 @@ class DrawLineModifier extends DrawModifier $this->drawable->start()->y(), $this->drawable->end()->x(), $this->drawable->end()->y(), - $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ) ); } diff --git a/src/Drivers/Gd/Modifiers/DrawPixelModifier.php b/src/Drivers/Gd/Modifiers/DrawPixelModifier.php index 24a94126..0476cde0 100644 --- a/src/Drivers/Gd/Modifiers/DrawPixelModifier.php +++ b/src/Drivers/Gd/Modifiers/DrawPixelModifier.php @@ -9,9 +9,8 @@ class DrawPixelModifier extends DriverModifier { public function apply(ImageInterface $image): ImageInterface { - $color = $this->driver()->colorToNative( - $this->driver()->handleInput($this->color), - $image->colorspace() + $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->driver()->handleInput($this->color) ); foreach ($image as $frame) { diff --git a/src/Drivers/Gd/Modifiers/DrawPolygonModifier.php b/src/Drivers/Gd/Modifiers/DrawPolygonModifier.php index 3a0d3f61..e8a2a5c0 100644 --- a/src/Drivers/Gd/Modifiers/DrawPolygonModifier.php +++ b/src/Drivers/Gd/Modifiers/DrawPolygonModifier.php @@ -14,9 +14,8 @@ class DrawPolygonModifier extends DrawModifier imagefilledpolygon( $frame->native(), $this->drawable->toArray(), - $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ) ); } @@ -26,9 +25,8 @@ class DrawPolygonModifier extends DrawModifier imagepolygon( $frame->native(), $this->drawable->toArray(), - $this->driver()->colorToNative( - $this->borderColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->borderColor() ) ); } diff --git a/src/Drivers/Gd/Modifiers/DrawRectangleModifier.php b/src/Drivers/Gd/Modifiers/DrawRectangleModifier.php index bc0627fa..03e8dd4f 100644 --- a/src/Drivers/Gd/Modifiers/DrawRectangleModifier.php +++ b/src/Drivers/Gd/Modifiers/DrawRectangleModifier.php @@ -18,9 +18,8 @@ class DrawRectangleModifier extends DrawModifier $this->position()->y(), $this->position()->x() + $this->drawable->width(), $this->position()->y() + $this->drawable->height(), - $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ) ); } @@ -34,9 +33,8 @@ class DrawRectangleModifier extends DrawModifier $this->position()->y(), $this->position()->x() + $this->drawable->width(), $this->position()->y() + $this->drawable->height(), - $this->driver()->colorToNative( - $this->borderColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->borderColor() ) ); } diff --git a/src/Drivers/Gd/Modifiers/FillModifier.php b/src/Drivers/Gd/Modifiers/FillModifier.php index 8f18833c..70271a78 100644 --- a/src/Drivers/Gd/Modifiers/FillModifier.php +++ b/src/Drivers/Gd/Modifiers/FillModifier.php @@ -2,7 +2,6 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; -use Intervention\Image\Colors\Rgb\Colorspace; use Intervention\Image\Drivers\DriverModifier; use Intervention\Image\Drivers\Gd\Frame; use Intervention\Image\Interfaces\ImageInterface; @@ -11,7 +10,7 @@ class FillModifier extends DriverModifier { public function apply(ImageInterface $image): ImageInterface { - $color = $this->color(); + $color = $this->color($image); foreach ($image as $frame) { if ($this->hasPosition()) { @@ -24,11 +23,10 @@ class FillModifier extends DriverModifier return $image; } - private function color(): int + private function color(ImageInterface $image): int { - return $this->driver()->colorToNative( - $this->driver()->handleInput($this->color), - new Colorspace() + return $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->driver()->handleInput($this->color) ); } diff --git a/src/Drivers/Gd/Modifiers/TextModifier.php b/src/Drivers/Gd/Modifiers/TextModifier.php index a01db204..452403b7 100644 --- a/src/Drivers/Gd/Modifiers/TextModifier.php +++ b/src/Drivers/Gd/Modifiers/TextModifier.php @@ -2,7 +2,6 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; -use Intervention\Image\Colors\Rgb\Colorspace; use Intervention\Image\Drivers\DriverModifier; use Intervention\Image\Drivers\Gd\FontProcessor; use Intervention\Image\Interfaces\ImageInterface; @@ -14,9 +13,8 @@ class TextModifier extends DriverModifier $processor = $this->fontProcessor(); $lines = $processor->alignedTextBlock($this->position, $this->text); - $color = $this->driver()->colorToNative( - $this->driver()->handleInput($this->font->color()), - new Colorspace() + $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->driver()->handleInput($this->font->color()) ); foreach ($image as $frame) { diff --git a/src/Drivers/Imagick/Driver.php b/src/Drivers/Imagick/Driver.php index 353dab70..080a6ff9 100644 --- a/src/Drivers/Imagick/Driver.php +++ b/src/Drivers/Imagick/Driver.php @@ -48,9 +48,4 @@ class Driver extends AbstractDriver { return new FontProcessor($font); } - - public function colorToNative(ColorInterface $color, ColorspaceInterface $colorspace): mixed - { - return (new ColorProcessor($colorspace))->colorToNative($color); - } } diff --git a/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php b/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php index ca6d1869..ac611ba3 100644 --- a/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php @@ -10,14 +10,12 @@ class DrawEllipseModifier extends DrawModifier { public function apply(ImageInterface $image): ImageInterface { - $background_color = $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ); - $border_color = $this->driver()->colorToNative( - $this->borderColor(), - $image->colorspace() + $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->borderColor() ); foreach ($image as $frame) { diff --git a/src/Drivers/Imagick/Modifiers/DrawLineModifier.php b/src/Drivers/Imagick/Modifiers/DrawLineModifier.php index c2ac0d8e..e4aec6c4 100644 --- a/src/Drivers/Imagick/Modifiers/DrawLineModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawLineModifier.php @@ -13,9 +13,8 @@ class DrawLineModifier extends DrawModifier $drawing = new ImagickDraw(); $drawing->setStrokeWidth($this->drawable->width()); $drawing->setStrokeColor( - $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ) ); diff --git a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php index 9f3dea52..7f115098 100644 --- a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php @@ -10,9 +10,8 @@ class DrawPixelModifier extends DriverModifier { public function apply(ImageInterface $image): ImageInterface { - $color = $this->driver()->colorToNative( - $this->driver()->handleInput($this->color), - $image->colorspace() + $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->driver()->handleInput($this->color) ); $pixel = new ImagickDraw(); diff --git a/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php b/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php index 91568fe3..9f07b531 100644 --- a/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php @@ -13,18 +13,16 @@ class DrawPolygonModifier extends DrawModifier $drawing = new ImagickDraw(); if ($this->drawable->hasBackgroundColor()) { - $background_color = $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace() + $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ); $drawing->setFillColor($background_color); } if ($this->drawable->hasBorder()) { - $border_color = $this->driver()->colorToNative( - $this->borderColor(), - $image->colorspace() + $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->borderColor() ); $drawing->setStrokeColor($border_color); diff --git a/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php b/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php index 7d167b34..f39350dd 100644 --- a/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php @@ -12,14 +12,12 @@ class DrawRectangleModifier extends DrawModifier { $drawing = new ImagickDraw(); - $background_color = $this->driver()->colorToNative( - $this->backgroundColor(), - $image->colorspace(), + $background_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->backgroundColor() ); - $border_color = $this->driver()->colorToNative( - $this->borderColor(), - $image->colorspace(), + $border_color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->borderColor() ); $drawing->setFillColor($background_color); diff --git a/src/Drivers/Imagick/Modifiers/FillModifier.php b/src/Drivers/Imagick/Modifiers/FillModifier.php index 46ce659b..a55feb84 100644 --- a/src/Drivers/Imagick/Modifiers/FillModifier.php +++ b/src/Drivers/Imagick/Modifiers/FillModifier.php @@ -14,7 +14,9 @@ class FillModifier extends DriverModifier public function apply(ImageInterface $image): ImageInterface { $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) { if ($this->hasPosition()) { diff --git a/src/Drivers/Imagick/Modifiers/RotateModifier.php b/src/Drivers/Imagick/Modifiers/RotateModifier.php index 680d42e8..ed5df58c 100644 --- a/src/Drivers/Imagick/Modifiers/RotateModifier.php +++ b/src/Drivers/Imagick/Modifiers/RotateModifier.php @@ -9,9 +9,8 @@ class RotateModifier extends DriverModifier { public function apply(ImageInterface $image): ImageInterface { - $background = $this->driver()->colorToNative( - $this->driver()->handleInput($this->background), - $image->colorspace() + $background = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->driver()->handleInput($this->background) ); foreach ($image as $frame) { diff --git a/src/Drivers/Imagick/Modifiers/TextModifier.php b/src/Drivers/Imagick/Modifiers/TextModifier.php index 24bbf9d3..f6dd3157 100644 --- a/src/Drivers/Imagick/Modifiers/TextModifier.php +++ b/src/Drivers/Imagick/Modifiers/TextModifier.php @@ -13,9 +13,8 @@ class TextModifier extends DriverModifier $processor = $this->fontProcessor(); $lines = $processor->alignedTextBlock($this->position, $this->text); - $color = $this->driver()->colorToNative( - $this->driver()->handleInput($this->font->color()), - $image->colorspace() + $color = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $this->driver()->handleInput($this->font->color()) ); $draw = $processor->toImagickDraw($color); diff --git a/src/Interfaces/DriverInterface.php b/src/Interfaces/DriverInterface.php index fefb3dbd..049770dc 100644 --- a/src/Interfaces/DriverInterface.php +++ b/src/Interfaces/DriverInterface.php @@ -10,5 +10,4 @@ interface DriverInterface public function handleInput(mixed $input): ImageInterface|ColorInterface; public function colorProcessor(ColorspaceInterface $colorspace): ColorProcessorInterface; public function fontProcessor(FontInterface $font): FontProcessorInterface; - public function colorToNative(ColorInterface $color, ColorspaceInterface $colorspace): mixed; }