Skip to content

Commit

Permalink
Implement __debugInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 11, 2024
1 parent 1493a2b commit 032266b
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Colors/AbstractColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -85,6 +86,20 @@ public function convertTo(string|ColorspaceInterface $colorspace): ColorInterfac
return $colorspace->importColor($this);
}

/**
* Show debug info for the current color
*
* @return array<string, int>
*/
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}
*
Expand Down
25 changes: 25 additions & 0 deletions src/Drivers/AbstractFrame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers;

use Intervention\Image\Interfaces\FrameInterface;

abstract class AbstractFrame implements FrameInterface
{
/**
* Show debug info for the current image
*
* @return array<string, mixed>
*/
public function __debugInfo(): array
{
return [
'delay' => $this->delay(),
'left' => $this->offsetLeft(),
'top' => $this->offsetTop(),
'dispose' => $this->dispose(),
];
}
}
3 changes: 2 additions & 1 deletion src/Drivers/Gd/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Drivers/Imagick/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/EncodedImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
public function __debugInfo(): array
{
return [
'mimetype' => $this->mimetype(),
'size' => $this->size(),
];
}
}
13 changes: 13 additions & 0 deletions src/Geometry/Rectangle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, int>
*/
public function __debugInfo(): array
{
return [
'width' => $this->width(),
'height' => $this->height(),
];
}
}
19 changes: 18 additions & 1 deletion src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ public function drawLine(callable|Closure|Line $init): ImageInterface
);
}

/**
/**
* {@inheritdoc}
*
* @see ImageInterface::drawBezier()
Expand Down Expand Up @@ -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<string, int>
*/
public function __debugInfo(): array
{
try {
return [
'width' => $this->width(),
'height' => $this->height(),
];
} catch (RuntimeException) {
return [];
}
}

/**
* Clone image
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Colors/Cmyk/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Colors/Hsl/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Colors/Hsv/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Colors/Rgb/ColorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
9 changes: 9 additions & 0 deletions tests/Unit/Drivers/Gd/FrameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Drivers/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Drivers/Imagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/EncodedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Geometry/RectangleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit 032266b

Please sign in to comment.