Skip to content

Commit

Permalink
added test anf fixed encoders when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
deluxetom committed Dec 30, 2024
1 parent 0419e39 commit 139927c
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function encode(ImageInterface $image): EncodedImage
{
$result = $image->core()->native()->writeToBuffer('.avif', [
'Q' => $this->quality,
'strip' => true,
// 'speed' => 6, // Speed (faster encoding)/*
// 'effort' => 4, // Compression effort*/
]);
Expand Down
1 change: 1 addition & 0 deletions src/Encoders/HeicEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function encode(ImageInterface $image): EncodedImage
{
$result = $image->core()->native()->writeToBuffer('.heic', [
'Q' => $this->quality,
'strip' => true,
]);

return new EncodedImage($result, 'image/heic');
Expand Down
8 changes: 7 additions & 1 deletion src/Encoders/Jpeg2000Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class Jpeg2000Encoder extends GenericJpeg2000Encoder implements SpecializedInter
*/
public function encode(ImageInterface $image): EncodedImage
{
$result = $image->core()->native()->writeToBuffer('.j2k', [
$core = $image->core()->native();

if ($image->isAnimated()) {
$core = $image->core()->frame(1)->native();
}

$result = $core->writeToBuffer('.j2k', [
'Q' => $this->quality,
'strip' => true,
'lossless' => false,
Expand Down
8 changes: 7 additions & 1 deletion src/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$result = $image->core()->native()->writeToBuffer('.png', [
$core = $image->core()->native();

if ($image->isAnimated()) {
$core = $image->core()->frame(1)->native();
}

$result = $core->writeToBuffer('.png', [
'interlace' => $this->interlaced,
'palette' => $this->indexed,
'strip' => true,
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/AvifEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public function testEncode(): void
$this->assertMediaType('image/avif', $result);
$this->assertEquals('image/avif', $result->mimetype());
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new AvifEncoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/BmpEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public function testEncode(): void
$this->assertMediaType(['image/bmp', 'image/x-ms-bmp'], $result);
$this->assertEquals('image/bmp', $result->mimetype());
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new BmpEncoder();
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/HeicEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public function testEncode(): void
$this->assertMediaType('image/heic', $result);
$this->assertEquals('image/heic', $result->mimetype());
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new HeicEncoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/Jpeg200EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ public function testEncode(): void
$this->assertTrue($this->isJpeg2000($result), 'Encoding result is not in Jpeg 2000 format.');
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new Jpeg2000Encoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}

private function isJpeg2000(string|EncodedImage $input): bool
{
return 1 === preg_match(
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/PngEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ public function testEncodeInterlaced(): void
$this->assertTrue($this->isInterlacedPng($result));
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new PngEncoder();
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}

#[DataProvider('indexedDataProvider')]
public function testEncoderIndexed(ImageInterface $image, PngEncoder $encoder, string $result): void
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/TiffEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public function testEncode(): void
$this->assertMediaType('image/tiff', $result);
$this->assertEquals('image/tiff', $result->mimetype());
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new TiffEncoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}
}
11 changes: 11 additions & 0 deletions tests/Unit/Encoders/WebpEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,15 @@ public function testEncode(): void
$this->assertMediaType('image/webp', $result);
$this->assertEquals('image/webp', $result->mimetype());
}

public function testEncodeAnimated(): void
{
$image = $this->readTestImage('animation.gif');
$encoder = new WebpEncoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$encoded = $this->readFilePointer($result->toFilePointer());
$this->assertSame($image->width(), $encoded->width());
$this->assertSame($image->height(), $encoded->height());
}
}

0 comments on commit 139927c

Please sign in to comment.