diff --git a/src/Drivers/Gd/Encoders/PngEncoder.php b/src/Drivers/Gd/Encoders/PngEncoder.php index bd6a90c0..8368b7a6 100644 --- a/src/Drivers/Gd/Encoders/PngEncoder.php +++ b/src/Drivers/Gd/Encoders/PngEncoder.php @@ -38,7 +38,6 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface * Prepare given image instance for PNG format output according to encoder settings * * @param ImageInterface $image - * @param bool $indexed * @throws RuntimeException * @throws ColorException * @throws AnimationException @@ -51,7 +50,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface } // get blending color - $blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->driver()->config()->blendingColor) ); diff --git a/src/Drivers/Imagick/Encoders/PngEncoder.php b/src/Drivers/Imagick/Encoders/PngEncoder.php index 06a7a03d..c3889b18 100644 --- a/src/Drivers/Imagick/Encoders/PngEncoder.php +++ b/src/Drivers/Imagick/Encoders/PngEncoder.php @@ -58,7 +58,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface } // get blending color - $blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative( + $blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $this->driver()->handleInput($this->driver()->config()->blendingColor) ); diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 41caa2aa..9aa89c0c 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -25,7 +25,7 @@ abstract class BaseTestCase extends MockeryTestCase return file_get_contents(self::getTestResourcePath($filename)); } - public function getTestResourcePointer($filename = 'test.jpg') + public static function getTestResourcePointer($filename = 'test.jpg') { $pointer = fopen('php://temp', 'rw'); fputs($pointer, self::getTestResourceData($filename)); diff --git a/tests/GdTestCase.php b/tests/GdTestCase.php index 2cfa9527..ebe98a01 100644 --- a/tests/GdTestCase.php +++ b/tests/GdTestCase.php @@ -12,14 +12,14 @@ use Intervention\Image\Image; abstract class GdTestCase extends BaseTestCase { - public function readTestImage($filename = 'test.jpg'): Image + public static function readTestImage($filename = 'test.jpg'): Image { return (new Driver())->specialize(new FilePathImageDecoder())->decode( - $this->getTestResourcePath($filename) + static::getTestResourcePath($filename) ); } - public function createTestImage(int $width, int $height): Image + public static function createTestImage(int $width, int $height): Image { $gd = imagecreatetruecolor($width, $height); imagefill($gd, 0, 0, imagecolorallocate($gd, 255, 0, 0)); @@ -32,7 +32,7 @@ abstract class GdTestCase extends BaseTestCase ); } - public function createTestAnimation(): Image + public static function createTestAnimation(): Image { $gd1 = imagecreatetruecolor(3, 2); imagefill($gd1, 0, 0, imagecolorallocate($gd1, 255, 0, 0)); diff --git a/tests/ImagickTestCase.php b/tests/ImagickTestCase.php index cbd24dcc..bef7b674 100644 --- a/tests/ImagickTestCase.php +++ b/tests/ImagickTestCase.php @@ -13,14 +13,14 @@ use Intervention\Image\Image; abstract class ImagickTestCase extends BaseTestCase { - public function readTestImage($filename = 'test.jpg'): Image + public static function readTestImage($filename = 'test.jpg'): Image { return (new Driver())->specialize(new FilePathImageDecoder())->decode( - $this->getTestResourcePath($filename) + static::getTestResourcePath($filename) ); } - public function createTestImage(int $width, int $height): Image + public static function createTestImage(int $width, int $height): Image { $background = new ImagickPixel('rgb(255, 0, 0)'); $imagick = new Imagick(); @@ -36,7 +36,7 @@ abstract class ImagickTestCase extends BaseTestCase ); } - public function createTestAnimation(): Image + public static function createTestAnimation(): Image { $imagick = new Imagick(); $imagick->setFormat('gif'); diff --git a/tests/Unit/Drivers/Gd/Encoders/PngEncoderTest.php b/tests/Unit/Drivers/Gd/Encoders/PngEncoderTest.php index 69cc7d12..d9bf1da4 100644 --- a/tests/Unit/Drivers/Gd/Encoders/PngEncoderTest.php +++ b/tests/Unit/Drivers/Gd/Encoders/PngEncoderTest.php @@ -7,8 +7,10 @@ namespace Intervention\Image\Tests\Unit\Drivers\Gd\Encoders; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Encoders\PngEncoder; +use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Tests\GdTestCase; use Intervention\Image\Tests\Traits\CanInspectPngFormat; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('gd')] #[CoversClass(\Intervention\Image\Encoders\PngEncoder::class)] @@ -34,4 +36,59 @@ final class PngEncoderTest extends GdTestCase $this->assertMediaType('image/png', (string) $result); $this->assertTrue($this->isInterlacedPng((string) $result)); } + + #[DataProvider('indexedDataProvider')] + public function testEncoderIndexed(ImageInterface $image, PngEncoder $encoder, string $result): void + { + $this->assertEquals( + $result, + $this->pngColorType((string) $encoder->encode($image)), + ); + } + + public static function indexedDataProvider(): array + { + return [ + [ + static::createTestImage(3, 2), // new + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::createTestImage(3, 2), // new + new PngEncoder(indexed: true), + 'indexed', + ], + [ + static::readTestImage('circle.png'), // truecolor-alpha + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::readTestImage('circle.png'), // indexedcolor-alpha + new PngEncoder(indexed: true), + 'indexed', + ], + [ + static::readTestImage('tile.png'), // indexed + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::readTestImage('tile.png'), // indexed + new PngEncoder(indexed: true), + 'indexed', + ], + [ + static::readTestImage('test.jpg'), // jpeg + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::readTestImage('test.jpg'), // jpeg + new PngEncoder(indexed: true), + 'indexed', + ], + ]; + } } diff --git a/tests/Unit/Drivers/Imagick/Encoders/PngEncoderTest.php b/tests/Unit/Drivers/Imagick/Encoders/PngEncoderTest.php index 268ac830..b905bd28 100644 --- a/tests/Unit/Drivers/Imagick/Encoders/PngEncoderTest.php +++ b/tests/Unit/Drivers/Imagick/Encoders/PngEncoderTest.php @@ -7,8 +7,10 @@ namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\RequiresPhpExtension; use Intervention\Image\Encoders\PngEncoder; +use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Tests\ImagickTestCase; use Intervention\Image\Tests\Traits\CanInspectPngFormat; +use PHPUnit\Framework\Attributes\DataProvider; #[RequiresPhpExtension('imagick')] #[CoversClass(\Intervention\Image\Encoders\PngEncoder::class)] @@ -34,4 +36,59 @@ final class PngEncoderTest extends ImagickTestCase $this->assertMediaType('image/png', (string) $result); $this->assertTrue($this->isInterlacedPng((string) $result)); } + + #[DataProvider('indexedDataProvider')] + public function testEncoderIndexed(ImageInterface $image, PngEncoder $encoder, string $result): void + { + $this->assertEquals( + $result, + $this->pngColorType((string) $encoder->encode($image)), + ); + } + + public static function indexedDataProvider(): array + { + return [ + [ + static::createTestImage(3, 2), // new + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::createTestImage(3, 2), // new + new PngEncoder(indexed: true), + 'indexed', + ], + [ + static::readTestImage('circle.png'), // truecolor-alpha + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::readTestImage('circle.png'), // indexedcolor-alpha + new PngEncoder(indexed: true), + 'indexed', + ], + [ + static::readTestImage('tile.png'), // indexed + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::readTestImage('tile.png'), // indexed + new PngEncoder(indexed: true), + 'indexed', + ], + [ + static::readTestImage('test.jpg'), // jpeg + new PngEncoder(indexed: false), + 'truecolor-alpha', + ], + [ + static::readTestImage('test.jpg'), // jpeg + new PngEncoder(indexed: true), + 'indexed', + ], + ]; + } }