From 77da6ca5c4a0c93039a4661bd10caab788b451ba Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 14 Dec 2024 08:27:56 +0100 Subject: [PATCH] Implement __debugInfo() (#1406) --- src/Colors/AbstractColor.php | 15 ++++++++++++++ src/Drivers/AbstractFrame.php | 25 ++++++++++++++++++++++++ src/Drivers/Gd/Frame.php | 3 ++- src/Drivers/Imagick/Frame.php | 3 ++- src/EncodedImage.php | 13 ++++++++++++ src/Geometry/Rectangle.php | 13 ++++++++++++ src/Image.php | 19 +++++++++++++++++- tests/Unit/Colors/Cmyk/ColorTest.php | 9 +++++++++ tests/Unit/Colors/Hsl/ColorTest.php | 8 ++++++++ tests/Unit/Colors/Hsv/ColorTest.php | 8 ++++++++ tests/Unit/Colors/Rgb/ColorTest.php | 9 +++++++++ tests/Unit/Drivers/Gd/FrameTest.php | 9 +++++++++ tests/Unit/Drivers/Gd/ImageTest.php | 7 +++++++ tests/Unit/Drivers/Imagick/ImageTest.php | 7 +++++++ tests/Unit/EncodedImageTest.php | 7 +++++++ tests/Unit/Geometry/RectangleTest.php | 7 +++++++ 16 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/Drivers/AbstractFrame.php diff --git a/src/Colors/AbstractColor.php b/src/Colors/AbstractColor.php index e69278858..ef9d7be72 100644 --- a/src/Colors/AbstractColor.php +++ b/src/Colors/AbstractColor.php @@ -8,6 +8,7 @@ use Intervention\Image\Interfaces\ColorChannelInterface; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ColorspaceInterface; +use ReflectionClass; abstract class AbstractColor implements ColorInterface { @@ -85,6 +86,20 @@ public function convertTo(string|ColorspaceInterface $colorspace): ColorInterfac return $colorspace->importColor($this); } + /** + * Show debug info for the current color + * + * @return array + */ + public function __debugInfo(): array + { + return array_reduce($this->channels(), function (array $result, ColorChannelInterface $item) { + $key = strtolower((new ReflectionClass($item))->getShortName()); + $result[$key] = $item->value(); + return $result; + }, []); + } + /** * {@inheritdoc} * diff --git a/src/Drivers/AbstractFrame.php b/src/Drivers/AbstractFrame.php new file mode 100644 index 000000000..706ddf529 --- /dev/null +++ b/src/Drivers/AbstractFrame.php @@ -0,0 +1,25 @@ + + */ + public function __debugInfo(): array + { + return [ + 'delay' => $this->delay(), + 'left' => $this->offsetLeft(), + 'top' => $this->offsetTop(), + 'dispose' => $this->dispose(), + ]; + } +} diff --git a/src/Drivers/Gd/Frame.php b/src/Drivers/Gd/Frame.php index c0ff3e293..d5e22750d 100644 --- a/src/Drivers/Gd/Frame.php +++ b/src/Drivers/Gd/Frame.php @@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Gd; use GdImage; +use Intervention\Image\Drivers\AbstractFrame; use Intervention\Image\Exceptions\ColorException; use Intervention\Image\Exceptions\InputException; use Intervention\Image\Geometry\Rectangle; @@ -14,7 +15,7 @@ use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; -class Frame implements FrameInterface +class Frame extends AbstractFrame implements FrameInterface { /** * Create new frame instance diff --git a/src/Drivers/Imagick/Frame.php b/src/Drivers/Imagick/Frame.php index a957ba5ad..94281323f 100644 --- a/src/Drivers/Imagick/Frame.php +++ b/src/Drivers/Imagick/Frame.php @@ -7,6 +7,7 @@ use Imagick; use ImagickException; use ImagickPixel; +use Intervention\Image\Drivers\AbstractFrame; use Intervention\Image\Exceptions\InputException; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Image; @@ -15,7 +16,7 @@ use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; -class Frame implements FrameInterface +class Frame extends AbstractFrame implements FrameInterface { /** * Create new frame object diff --git a/src/EncodedImage.php b/src/EncodedImage.php index a4c81fbf7..b2383b215 100644 --- a/src/EncodedImage.php +++ b/src/EncodedImage.php @@ -50,4 +50,17 @@ public function toDataUri(): string { return sprintf('data:%s;base64,%s', $this->mediaType(), base64_encode((string) $this)); } + + /** + * Show debug info for the current image + * + * @return array + */ + public function __debugInfo(): array + { + return [ + 'mimetype' => $this->mimetype(), + 'size' => $this->size(), + ]; + } } diff --git a/src/Geometry/Rectangle.php b/src/Geometry/Rectangle.php index de7194b6c..66937cc93 100644 --- a/src/Geometry/Rectangle.php +++ b/src/Geometry/Rectangle.php @@ -364,4 +364,17 @@ protected function resizer(?int $width = null, ?int $height = null): RectangleRe { return new RectangleResizer($width, $height); } + + /** + * Show debug info for the current rectangle + * + * @return array + */ + public function __debugInfo(): array + { + return [ + 'width' => $this->width(), + 'height' => $this->height(), + ]; + } } diff --git a/src/Image.php b/src/Image.php index 126df1702..21199ddf7 100644 --- a/src/Image.php +++ b/src/Image.php @@ -882,7 +882,7 @@ public function drawLine(callable|Closure|Line $init): ImageInterface ); } - /** + /** * {@inheritdoc} * * @see ImageInterface::drawBezier() @@ -1065,6 +1065,23 @@ public function toHeic(mixed ...$options): EncodedImageInterface return $this->encode(new HeicEncoder(...$options)); } + /** + * Show debug info for the current image + * + * @return array + */ + public function __debugInfo(): array + { + try { + return [ + 'width' => $this->width(), + 'height' => $this->height(), + ]; + } catch (RuntimeException) { + return []; + } + } + /** * Clone image * diff --git a/tests/Unit/Colors/Cmyk/ColorTest.php b/tests/Unit/Colors/Cmyk/ColorTest.php index f860bba8f..08a028a2f 100644 --- a/tests/Unit/Colors/Cmyk/ColorTest.php +++ b/tests/Unit/Colors/Cmyk/ColorTest.php @@ -115,4 +115,13 @@ public function testIsClear(): void $color = new Color(0, 0, 0, 0); $this->assertFalse($color->isClear()); } + + public function testDebugInfo(): void + { + $info = (new Color(10, 20, 30, 40))->__debugInfo(); + $this->assertEquals(10, $info['cyan']); + $this->assertEquals(20, $info['magenta']); + $this->assertEquals(30, $info['yellow']); + $this->assertEquals(40, $info['key']); + } } diff --git a/tests/Unit/Colors/Hsl/ColorTest.php b/tests/Unit/Colors/Hsl/ColorTest.php index 82dda47e6..c0d9e4234 100644 --- a/tests/Unit/Colors/Hsl/ColorTest.php +++ b/tests/Unit/Colors/Hsl/ColorTest.php @@ -114,4 +114,12 @@ public function testIsClear(): void $color = new Color(0, 1, 0); $this->assertFalse($color->isClear()); } + + public function testDebugInfo(): void + { + $info = (new Color(10, 20, 30))->__debugInfo(); + $this->assertEquals(10, $info['hue']); + $this->assertEquals(20, $info['saturation']); + $this->assertEquals(30, $info['luminance']); + } } diff --git a/tests/Unit/Colors/Hsv/ColorTest.php b/tests/Unit/Colors/Hsv/ColorTest.php index 8827f59c0..1d900e28e 100644 --- a/tests/Unit/Colors/Hsv/ColorTest.php +++ b/tests/Unit/Colors/Hsv/ColorTest.php @@ -114,4 +114,12 @@ public function testIsClear(): void $color = new Color(0, 1, 0); $this->assertFalse($color->isClear()); } + + public function testDebugInfo(): void + { + $info = (new Color(10, 20, 30))->__debugInfo(); + $this->assertEquals(10, $info['hue']); + $this->assertEquals(20, $info['saturation']); + $this->assertEquals(30, $info['value']); + } } diff --git a/tests/Unit/Colors/Rgb/ColorTest.php b/tests/Unit/Colors/Rgb/ColorTest.php index c1d159a4d..28579ffe8 100644 --- a/tests/Unit/Colors/Rgb/ColorTest.php +++ b/tests/Unit/Colors/Rgb/ColorTest.php @@ -176,4 +176,13 @@ public function testIsClear(): void $color = new Color(255, 255, 255, 0); $this->assertTrue($color->isClear()); } + + public function testDebugInfo(): void + { + $info = (new Color(10, 20, 30, 40))->__debugInfo(); + $this->assertEquals(10, $info['red']); + $this->assertEquals(20, $info['green']); + $this->assertEquals(30, $info['blue']); + $this->assertEquals(40, $info['alpha']); + } } diff --git a/tests/Unit/Drivers/Gd/FrameTest.php b/tests/Unit/Drivers/Gd/FrameTest.php index ba7ae15f1..b70cdb96a 100644 --- a/tests/Unit/Drivers/Gd/FrameTest.php +++ b/tests/Unit/Drivers/Gd/FrameTest.php @@ -108,4 +108,13 @@ public function testToImage(): void $frame = $this->getTestFrame(); $this->assertInstanceOf(Image::class, $frame->toImage(new Driver())); } + + public function testDebugInfo(): void + { + $info = $this->getTestFrame()->__debugInfo(); + $this->assertEquals(0, $info['delay']); + $this->assertEquals(0, $info['left']); + $this->assertEquals(0, $info['top']); + $this->assertEquals(1, $info['dispose']); + } } diff --git a/tests/Unit/Drivers/Gd/ImageTest.php b/tests/Unit/Drivers/Gd/ImageTest.php index 14e17f440..0da171b57 100644 --- a/tests/Unit/Drivers/Gd/ImageTest.php +++ b/tests/Unit/Drivers/Gd/ImageTest.php @@ -390,4 +390,11 @@ public function testBrightness(): void $this->assertInstanceOf(ImageInterface::class, $result); $this->assertEquals('4cfaff', $image->pickColor(14, 14)->toHex()); } + + public function testDebugInfo(): void + { + $info = $this->readTestImage('trim.png')->__debugInfo(); + $this->assertArrayHasKey('width', $info); + $this->assertArrayHasKey('height', $info); + } } diff --git a/tests/Unit/Drivers/Imagick/ImageTest.php b/tests/Unit/Drivers/Imagick/ImageTest.php index 311bbf5bf..99ca2612c 100644 --- a/tests/Unit/Drivers/Imagick/ImageTest.php +++ b/tests/Unit/Drivers/Imagick/ImageTest.php @@ -392,4 +392,11 @@ public function testBrightness(): void $this->assertInstanceOf(ImageInterface::class, $result); $this->assertEquals('39c9ff', $image->pickColor(14, 14)->toHex()); } + + public function testDebugInfo(): void + { + $info = $this->readTestImage('trim.png')->__debugInfo(); + $this->assertArrayHasKey('width', $info); + $this->assertArrayHasKey('height', $info); + } } diff --git a/tests/Unit/EncodedImageTest.php b/tests/Unit/EncodedImageTest.php index 51fe2bf7c..c430dae64 100644 --- a/tests/Unit/EncodedImageTest.php +++ b/tests/Unit/EncodedImageTest.php @@ -57,4 +57,11 @@ public function testMimetype(): void $image = new EncodedImage($this->getTestResourceData(), 'image/jpeg'); $this->assertEquals('image/jpeg', $image->mimetype()); } + + public function testDebugInfo(): void + { + $info = (new EncodedImage('foo', 'image/png'))->__debugInfo(); + $this->assertEquals('image/png', $info['mimetype']); + $this->assertEquals(3, $info['size']); + } } diff --git a/tests/Unit/Geometry/RectangleTest.php b/tests/Unit/Geometry/RectangleTest.php index 636987339..fd1312447 100644 --- a/tests/Unit/Geometry/RectangleTest.php +++ b/tests/Unit/Geometry/RectangleTest.php @@ -325,4 +325,11 @@ public function testContainMax(): void $this->assertEquals(800, $result->width()); $this->assertEquals(600, $result->height()); } + + public function testDebugInfo(): void + { + $info = (new Rectangle(800, 600))->__debugInfo(); + $this->assertEquals(800, $info['width']); + $this->assertEquals(600, $info['height']); + } }