From 1b856c81bcc912d45dd1c4bb57a916a9c28d0960 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Fri, 5 Nov 2021 16:42:48 +0000 Subject: [PATCH] Added tests --- src/Drivers/Abstract/AbstractColor.php | 16 ++++++++++ .../Gd/Decoders/BinaryImageDecoder.php | 32 ++----------------- .../Gd/Modifiers/GreyscaleModifierTest.php | 21 ++++++++++++ .../Gd/Modifiers/ResizeModifierTest.php | 24 ++++++++++++++ .../Modifiers/GreyscaleModifierTest.php | 21 ++++++++++++ .../Imagick/Modifiers/ResizeModifierTest.php | 24 ++++++++++++++ 6 files changed, 109 insertions(+), 29 deletions(-) create mode 100644 tests/Drivers/Gd/Modifiers/GreyscaleModifierTest.php create mode 100644 tests/Drivers/Gd/Modifiers/ResizeModifierTest.php create mode 100644 tests/Drivers/Imagick/Modifiers/GreyscaleModifierTest.php create mode 100644 tests/Drivers/Imagick/Modifiers/ResizeModifierTest.php diff --git a/src/Drivers/Abstract/AbstractColor.php b/src/Drivers/Abstract/AbstractColor.php index 449e4747..f8ea601a 100644 --- a/src/Drivers/Abstract/AbstractColor.php +++ b/src/Drivers/Abstract/AbstractColor.php @@ -4,6 +4,12 @@ namespace Intervention\Image\Drivers\Abstract; abstract class AbstractColor { + /** + * Format color to hexadecimal color code + * + * @param string $prefix + * @return string + */ public function toHex(string $prefix = ''): string { return sprintf( @@ -14,4 +20,14 @@ abstract class AbstractColor $this->blue() ); } + + /** + * Determine if color is greyscale + * + * @return boolean + */ + public function isGreyscale(): bool + { + return ($this->red() === $this->green()) && ($this->green() === $this->blue()); + } } diff --git a/src/Drivers/Gd/Decoders/BinaryImageDecoder.php b/src/Drivers/Gd/Decoders/BinaryImageDecoder.php index 32dcc6b1..f60a9702 100644 --- a/src/Drivers/Gd/Decoders/BinaryImageDecoder.php +++ b/src/Drivers/Gd/Decoders/BinaryImageDecoder.php @@ -33,7 +33,9 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface $this->fail(); } - $gd = $this->gdImageToTruecolor($gd); + if (! imageistruecolor($gd)) { + imagepalettetotruecolor($gd); + } return new Image(new Collection([new Frame($gd)])); } @@ -58,32 +60,4 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface return $image; } - - /** - * Transform GD image into truecolor version - * - * @param GdImage $gd - * @return bool - */ - public function gdImageToTruecolor(GdImage $gd): GdImage - { - $width = imagesx($gd); - $height = imagesy($gd); - - // new canvas - $canvas = imagecreatetruecolor($width, $height); - - // fill with transparent color - imagealphablending($canvas, false); - $transparent = imagecolorallocatealpha($canvas, 255, 255, 255, 127); - imagefilledrectangle($canvas, 0, 0, $width, $height, $transparent); - imagecolortransparent($canvas, $transparent); - imagealphablending($canvas, true); - - // copy original - imagecopy($canvas, $gd, 0, 0, 0, 0, $width, $height); - imagedestroy($gd); - - return $canvas; - } } diff --git a/tests/Drivers/Gd/Modifiers/GreyscaleModifierTest.php b/tests/Drivers/Gd/Modifiers/GreyscaleModifierTest.php new file mode 100644 index 00000000..9302d460 --- /dev/null +++ b/tests/Drivers/Gd/Modifiers/GreyscaleModifierTest.php @@ -0,0 +1,21 @@ +createTestImage('trim.png'); + $this->assertFalse($image->pickColor(0, 0)->isGreyscale()); + $image->modify(new GreyscaleModifier()); + $this->assertTrue($image->pickColor(0, 0)->isGreyscale()); + } +} diff --git a/tests/Drivers/Gd/Modifiers/ResizeModifierTest.php b/tests/Drivers/Gd/Modifiers/ResizeModifierTest.php new file mode 100644 index 00000000..15e4c0dd --- /dev/null +++ b/tests/Drivers/Gd/Modifiers/ResizeModifierTest.php @@ -0,0 +1,24 @@ +createTestImage('trim.png'); + $this->assertEquals(50, $image->width()); + $this->assertEquals(50, $image->height()); + $image->modify(new ResizeModifier(new Size(30, 20))); + $this->assertEquals(30, $image->width()); + $this->assertEquals(20, $image->height()); + } +} diff --git a/tests/Drivers/Imagick/Modifiers/GreyscaleModifierTest.php b/tests/Drivers/Imagick/Modifiers/GreyscaleModifierTest.php new file mode 100644 index 00000000..64b5be61 --- /dev/null +++ b/tests/Drivers/Imagick/Modifiers/GreyscaleModifierTest.php @@ -0,0 +1,21 @@ +createTestImage('trim.png'); + $this->assertFalse($image->pickColor(0, 0)->isGreyscale()); + $image->modify(new GreyscaleModifier()); + $this->assertTrue($image->pickColor(0, 0)->isGreyscale()); + } +} diff --git a/tests/Drivers/Imagick/Modifiers/ResizeModifierTest.php b/tests/Drivers/Imagick/Modifiers/ResizeModifierTest.php new file mode 100644 index 00000000..af8bca07 --- /dev/null +++ b/tests/Drivers/Imagick/Modifiers/ResizeModifierTest.php @@ -0,0 +1,24 @@ +createTestImage('trim.png'); + $this->assertEquals(50, $image->width()); + $this->assertEquals(50, $image->height()); + $image->modify(new ResizeModifier(new Size(30, 20))); + $this->assertEquals(30, $image->width()); + $this->assertEquals(20, $image->height()); + } +}