mirror of
https://github.com/Intervention/image.git
synced 2025-08-25 06:40:48 +02:00
Add tests for PNG indexed options
This commit is contained in:
@@ -38,7 +38,6 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
|
|||||||
* Prepare given image instance for PNG format output according to encoder settings
|
* Prepare given image instance for PNG format output according to encoder settings
|
||||||
*
|
*
|
||||||
* @param ImageInterface $image
|
* @param ImageInterface $image
|
||||||
* @param bool $indexed
|
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
* @throws ColorException
|
* @throws ColorException
|
||||||
* @throws AnimationException
|
* @throws AnimationException
|
||||||
@@ -51,7 +50,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get blending color
|
// get blending color
|
||||||
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
|
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
|
||||||
$this->driver()->handleInput($this->driver()->config()->blendingColor)
|
$this->driver()->handleInput($this->driver()->config()->blendingColor)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -58,7 +58,7 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get blending color
|
// get blending color
|
||||||
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
|
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
|
||||||
$this->driver()->handleInput($this->driver()->config()->blendingColor)
|
$this->driver()->handleInput($this->driver()->config()->blendingColor)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@@ -25,7 +25,7 @@ abstract class BaseTestCase extends MockeryTestCase
|
|||||||
return file_get_contents(self::getTestResourcePath($filename));
|
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');
|
$pointer = fopen('php://temp', 'rw');
|
||||||
fputs($pointer, self::getTestResourceData($filename));
|
fputs($pointer, self::getTestResourceData($filename));
|
||||||
|
@@ -12,14 +12,14 @@ use Intervention\Image\Image;
|
|||||||
|
|
||||||
abstract class GdTestCase extends BaseTestCase
|
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(
|
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);
|
$gd = imagecreatetruecolor($width, $height);
|
||||||
imagefill($gd, 0, 0, imagecolorallocate($gd, 255, 0, 0));
|
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);
|
$gd1 = imagecreatetruecolor(3, 2);
|
||||||
imagefill($gd1, 0, 0, imagecolorallocate($gd1, 255, 0, 0));
|
imagefill($gd1, 0, 0, imagecolorallocate($gd1, 255, 0, 0));
|
||||||
|
@@ -13,14 +13,14 @@ use Intervention\Image\Image;
|
|||||||
|
|
||||||
abstract class ImagickTestCase extends BaseTestCase
|
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(
|
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)');
|
$background = new ImagickPixel('rgb(255, 0, 0)');
|
||||||
$imagick = new Imagick();
|
$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 = new Imagick();
|
||||||
$imagick->setFormat('gif');
|
$imagick->setFormat('gif');
|
||||||
|
@@ -7,8 +7,10 @@ namespace Intervention\Image\Tests\Unit\Drivers\Gd\Encoders;
|
|||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||||
use Intervention\Image\Encoders\PngEncoder;
|
use Intervention\Image\Encoders\PngEncoder;
|
||||||
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Tests\GdTestCase;
|
use Intervention\Image\Tests\GdTestCase;
|
||||||
use Intervention\Image\Tests\Traits\CanInspectPngFormat;
|
use Intervention\Image\Tests\Traits\CanInspectPngFormat;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
#[RequiresPhpExtension('gd')]
|
#[RequiresPhpExtension('gd')]
|
||||||
#[CoversClass(\Intervention\Image\Encoders\PngEncoder::class)]
|
#[CoversClass(\Intervention\Image\Encoders\PngEncoder::class)]
|
||||||
@@ -34,4 +36,59 @@ final class PngEncoderTest extends GdTestCase
|
|||||||
$this->assertMediaType('image/png', (string) $result);
|
$this->assertMediaType('image/png', (string) $result);
|
||||||
$this->assertTrue($this->isInterlacedPng((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',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,8 +7,10 @@ namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders;
|
|||||||
use PHPUnit\Framework\Attributes\CoversClass;
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||||
use Intervention\Image\Encoders\PngEncoder;
|
use Intervention\Image\Encoders\PngEncoder;
|
||||||
|
use Intervention\Image\Interfaces\ImageInterface;
|
||||||
use Intervention\Image\Tests\ImagickTestCase;
|
use Intervention\Image\Tests\ImagickTestCase;
|
||||||
use Intervention\Image\Tests\Traits\CanInspectPngFormat;
|
use Intervention\Image\Tests\Traits\CanInspectPngFormat;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
#[RequiresPhpExtension('imagick')]
|
#[RequiresPhpExtension('imagick')]
|
||||||
#[CoversClass(\Intervention\Image\Encoders\PngEncoder::class)]
|
#[CoversClass(\Intervention\Image\Encoders\PngEncoder::class)]
|
||||||
@@ -34,4 +36,59 @@ final class PngEncoderTest extends ImagickTestCase
|
|||||||
$this->assertMediaType('image/png', (string) $result);
|
$this->assertMediaType('image/png', (string) $result);
|
||||||
$this->assertTrue($this->isInterlacedPng((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',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user