From 50f7ebce783d8dda3282485cb1e665b562550b17 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 30 Sep 2023 10:56:30 +0200 Subject: [PATCH] Fix random bugs --- src/Drivers/Abstract/AbstractImage.php | 2 +- src/Drivers/Abstract/AbstractInputHandler.php | 2 +- src/Drivers/Gd/Modifiers/PadDownModifier.php | 2 +- .../Imagick/Modifiers/DrawPixelModifier.php | 21 ++++++++++++---- .../Imagick/Modifiers/DrawPolygonModifier.php | 24 +++++++++++++++++++ .../Modifiers/DrawRectangleModifier.php | 5 ++-- .../Imagick/Modifiers/PadDownModifier.php | 2 +- src/Drivers/Imagick/Modifiers/TextWriter.php | 18 +------------- src/Geometry/Rectangle.php | 4 ++-- src/Interfaces/ImageInterface.php | 17 +++++++++---- 10 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index 1bd0f078..25a6cf35 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -25,7 +25,7 @@ abstract class AbstractImage implements ImageInterface use CanHandleInput; use CanRunCallback; - public function eachFrame(callable $callback): self + public function eachFrame(callable $callback): ImageInterface { foreach ($this as $frame) { $callback($frame); diff --git a/src/Drivers/Abstract/AbstractInputHandler.php b/src/Drivers/Abstract/AbstractInputHandler.php index 0ed0e0bb..d0f25c9b 100644 --- a/src/Drivers/Abstract/AbstractInputHandler.php +++ b/src/Drivers/Abstract/AbstractInputHandler.php @@ -40,7 +40,7 @@ abstract class AbstractInputHandler /** * Try to decode the given input with each decoder of the the handler chain * - * @param mixed $var + * @param mixed $input * @return ImageInterface|ColorInterface */ public function handle($input): ImageInterface|ColorInterface diff --git a/src/Drivers/Gd/Modifiers/PadDownModifier.php b/src/Drivers/Gd/Modifiers/PadDownModifier.php index 53247d6c..a819158e 100644 --- a/src/Drivers/Gd/Modifiers/PadDownModifier.php +++ b/src/Drivers/Gd/Modifiers/PadDownModifier.php @@ -13,7 +13,7 @@ class PadDownModifier extends PadModifier $resize = $this->getResizeSize($image); return $image->getSize() - ->contain($resize->width(), $resize->height()) + ->contain($resize->getWidth(), $resize->getHeight()) ->alignPivotTo($resize, $this->position); } diff --git a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php index 1b5edc9d..d975f93f 100644 --- a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php @@ -3,6 +3,8 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; +use Intervention\Image\Drivers\Imagick\Color; +use Intervention\Image\Exceptions\TypeException; use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -12,16 +14,14 @@ class DrawPixelModifier implements ModifierInterface { use CanHandleInput; - public function __construct( - protected Point $position, - protected $color - ) { + public function __construct(protected Point $position, protected mixed $color) + { // } public function apply(ImageInterface $image): ImageInterface { - $color = $this->handleInput($this->color); + $color = $this->decodeColor(); $pixel = new ImagickDraw(); $pixel->setFillColor($color->getPixel()); $pixel->point($this->position->getX(), $this->position->getY()); @@ -30,4 +30,15 @@ class DrawPixelModifier implements ModifierInterface $frame->getCore()->drawImage($pixel); }); } + + private function decodeColor(): Color + { + $color = $this->handleInput($this->color); + + if (!is_a($color, Color::class)) { + throw new TypeException('Color is not compatible to current driver.'); + } + + return $color; + } } diff --git a/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php b/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php index 8f2f7fdb..8c39081b 100644 --- a/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php @@ -4,6 +4,8 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; +use Intervention\Image\Drivers\Imagick\Color; +use Intervention\Image\Exceptions\TypeException; use Intervention\Image\Interfaces\DrawableInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -44,4 +46,26 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf return $points; } + + protected function getBackgroundColor(): ?Color + { + $color = parent::getBackgroundColor(); + + if (!is_a($color, Color::class)) { + throw new TypeException('Color is not compatible to current driver.'); + } + + return $color; + } + + protected function getBorderColor(): ?Color + { + $color = parent::getBorderColor(); + + if (!is_a($color, Color::class)) { + throw new TypeException('Color is not compatible to current driver.'); + } + + return $color; + } } diff --git a/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php b/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php index 330f7874..8993de8a 100644 --- a/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php @@ -8,7 +8,6 @@ use Intervention\Image\Drivers\Imagick\Color; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; -use Intervention\Image\Interfaces\ColorInterface; class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInterface { @@ -37,7 +36,7 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte return $image; } - protected function getBackgroundColor(): ColorInterface + protected function getBackgroundColor(): Color { $color = parent::getBackgroundColor(); if (!is_a($color, Color::class)) { @@ -47,7 +46,7 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte return $color; } - protected function getBorderColor(): ColorInterface + protected function getBorderColor(): Color { $color = parent::getBorderColor(); if (!is_a($color, Color::class)) { diff --git a/src/Drivers/Imagick/Modifiers/PadDownModifier.php b/src/Drivers/Imagick/Modifiers/PadDownModifier.php index f6de2fc4..c212134a 100644 --- a/src/Drivers/Imagick/Modifiers/PadDownModifier.php +++ b/src/Drivers/Imagick/Modifiers/PadDownModifier.php @@ -13,7 +13,7 @@ class PadDownModifier extends PadModifier $resize = $this->getResizeSize($image); return $image->getSize() - ->contain($resize->width(), $resize->height()) + ->contain($resize->getWidth(), $resize->getHeight()) ->alignPivotTo($resize, $this->position); } diff --git a/src/Drivers/Imagick/Modifiers/TextWriter.php b/src/Drivers/Imagick/Modifiers/TextWriter.php index b0e1572c..90469983 100644 --- a/src/Drivers/Imagick/Modifiers/TextWriter.php +++ b/src/Drivers/Imagick/Modifiers/TextWriter.php @@ -5,7 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use Intervention\Image\Drivers\Abstract\AbstractTextWriter; use Intervention\Image\Drivers\Imagick\Font; use Intervention\Image\Exceptions\FontException; -use Intervention\Image\Interfaces\FontInterface; use Intervention\Image\Interfaces\ImageInterface; class TextWriter extends AbstractTextWriter @@ -23,27 +22,12 @@ class TextWriter extends AbstractTextWriter $line ); } - - // debug - // $lines = new TextBlock($this->text); - // $box = $lines->getBoundingBox($this->font, $this->position); - // $points = []; - // foreach (array_chunk($box->toArray(), 2) as $p) { - // $points[] = ['x' => $p[0], 'y' => $p[1]]; - // } - // $draw = new \ImagickDraw(); - // $draw->setStrokeOpacity(1); - // $draw->setStrokeColor('black'); - // $draw->setFillColor('transparent'); - // $draw->polygon($points); - // $frame->getCore()->drawImage($draw); - } return $image; } - protected function getFont(): FontInterface + protected function getFont(): Font { if (!is_a($this->font, Font::class)) { throw new FontException('Font is not compatible to current driver.'); diff --git a/src/Geometry/Rectangle.php b/src/Geometry/Rectangle.php index c7e5b1bb..b244a60c 100644 --- a/src/Geometry/Rectangle.php +++ b/src/Geometry/Rectangle.php @@ -177,8 +177,8 @@ class Rectangle extends Polygon implements SizeInterface, DrawableInterface * Calculate the relative position to another Size * based on the pivot point settings of both sizes. * - * @param Size $size - * @return Point + * @param SizeInterface $rectangle + * @return PointInterface */ public function getRelativePositionTo(SizeInterface $rectangle): PointInterface { diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index 7930a826..4b04f761 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -11,7 +11,7 @@ interface ImageInterface extends Traversable, Countable /** * Get frame of animation image at given position starting with zero * - * @param int $key + * @param int $position * @return null|FrameInterface */ public function getFrame(int $position = 0): ?FrameInterface; @@ -24,6 +24,15 @@ interface ImageInterface extends Traversable, Countable */ public function addFrame(FrameInterface $frame): ImageInterface; + + /** + * Apply given callback to each frame of the image + * + * @param callable $callback + * @return ImageInterface + */ + public function eachFrame(callable $callback): ImageInterface; + /** * Set loop count of animated image * @@ -118,7 +127,7 @@ interface ImageInterface extends Traversable, Countable /** * Turn image into a greyscale version * - * @return void + * @return ImageInterface */ public function greyscale(): ImageInterface; @@ -272,14 +281,14 @@ interface ImageInterface extends Traversable, Countable /** * Mirror the current image horizontally * - * @return void + * @return ImageInterface */ public function flip(): ImageInterface; /** * Mirror the current image vertically * - * @return void + * @return ImageInterface */ public function flop(): ImageInterface;