Skip to content

Commit

Permalink
Add tests for PNG indexed options
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Aug 3, 2024
1 parent 2ee997d commit 63990a8
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/Drivers/Gd/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function encode(ImageInterface $image): EncodedImage
* Prepare given image instance for PNG format output according to encoder settings
*
* @param ImageInterface $image
* @param bool $indexed
* @throws RuntimeException
* @throws ColorException
* @throws AnimationException
Expand All @@ -51,7 +50,7 @@ private function prepareOutput(ImageInterface $image): GdImage
}

// get blending color
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->driver()->config()->blendingColor)
);

Expand Down
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private function prepareOutput(ImageInterface $image): Imagick
}

// get blending color
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->driver()->config()->blendingColor)
);

Expand Down
2 changes: 1 addition & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function getTestResourceData($filename = 'test.jpg'): string
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));
Expand Down
8 changes: 4 additions & 4 deletions tests/GdTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

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));
Expand All @@ -32,7 +32,7 @@ public function createTestImage(int $width, int $height): Image
);
}

public function createTestAnimation(): Image
public static function createTestAnimation(): Image
{
$gd1 = imagecreatetruecolor(3, 2);
imagefill($gd1, 0, 0, imagecolorallocate($gd1, 255, 0, 0));
Expand Down
8 changes: 4 additions & 4 deletions tests/ImagickTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

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();
Expand All @@ -36,7 +36,7 @@ public function createTestImage(int $width, int $height): Image
);
}

public function createTestAnimation(): Image
public static function createTestAnimation(): Image
{
$imagick = new Imagick();
$imagick->setFormat('gif');
Expand Down
57 changes: 57 additions & 0 deletions tests/Unit/Drivers/Gd/Encoders/PngEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
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)]
Expand All @@ -34,4 +36,59 @@ public function testEncodeInterlaced(): void
$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',
],
];
}
}
57 changes: 57 additions & 0 deletions tests/Unit/Drivers/Imagick/Encoders/PngEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
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)]
Expand All @@ -34,4 +36,59 @@ public function testEncodeInterlaced(): void
$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',
],
];
}
}

0 comments on commit 63990a8

Please sign in to comment.