Skip to content

Commit

Permalink
Add Encoder Options for progressive Jpeg & interlaced GIF format
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Mar 27, 2024
1 parent c261654 commit d982359
Show file tree
Hide file tree
Showing 18 changed files with 203 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -21,7 +21,7 @@
"require": {
"php": "^8.1",
"ext-mbstring": "*",
"intervention/gif": "^4.0.1"
"intervention/gif": "^4.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
Expand Down
7 changes: 5 additions & 2 deletions src/Drivers/Gd/Encoders/GifEncoder.php
Expand Up @@ -23,7 +23,9 @@ public function encode(ImageInterface $image): EncodedImage

$gd = $image->core()->native();
$data = $this->buffered(function () use ($gd) {
imageinterlace($gd, $this->interlaced);
imagegif($gd);
imageinterlace($gd, false);
});

return new EncodedImage($data, 'image/gif');
Expand All @@ -41,8 +43,9 @@ protected function encodeAnimated(ImageInterface $image): EncodedImage

foreach ($image as $frame) {
$builder->addFrame(
(string) $this->encode($frame->toImage($image->driver())),
$frame->delay()
source: (string) $this->encode($frame->toImage($image->driver())),
delay: $frame->delay(),
interlaced: $this->interlaced
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Drivers/Gd/Encoders/JpegEncoder.php
Expand Up @@ -17,6 +17,7 @@ public function encode(ImageInterface $image): EncodedImage
$output = Cloner::cloneBlended($image->core()->native(), background: $image->blendingColor());

$data = $this->buffered(function () use ($output) {
imageinterlace($output, $this->progressive);
imagejpeg($output, null, $this->quality);
});

Expand Down
4 changes: 4 additions & 0 deletions src/Drivers/Imagick/Encoders/GifEncoder.php
Expand Up @@ -24,6 +24,10 @@ public function encode(ImageInterface $image): EncodedImage
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);

if ($this->interlaced) {
$imagick->setInterlaceScheme(Imagick::INTERLACE_LINE);
}

return new EncodedImage($imagick->getImagesBlob(), 'image/gif');
}
}
4 changes: 4 additions & 0 deletions src/Drivers/Imagick/Encoders/JpegEncoder.php
Expand Up @@ -37,6 +37,10 @@ public function encode(ImageInterface $image): EncodedImage
$imagick->setImageCompressionQuality($this->quality);
$imagick->setImageAlphaChannel(Imagick::ALPHACHANNEL_REMOVE);

if ($this->progressive) {
$imagick->setInterlaceScheme(Imagick::INTERLACE_PLANE);
}

return new EncodedImage($imagick->getImagesBlob(), 'image/jpeg');
}
}
28 changes: 14 additions & 14 deletions src/Encoders/FileExtensionEncoder.php
Expand Up @@ -11,17 +11,17 @@

class FileExtensionEncoder extends AutoEncoder
{
protected array $options = [];

/**
* Create new encoder instance to encode to format of given file extension
*
* @param null|string $extension Target file extension for example "png"
* @param int $quality
* @return void
*/
public function __construct(
public ?string $extension = null,
public int $quality = self::DEFAULT_QUALITY
) {
public function __construct(public ?string $extension = null, mixed ...$options)
{
$this->options = $options;
}

/**
Expand Down Expand Up @@ -52,15 +52,15 @@ protected function encoderByFileExtension(?string $extension): EncoderInterface
}

return match (strtolower($extension)) {
'webp' => new WebpEncoder(quality: $this->quality),
'avif' => new AvifEncoder(quality: $this->quality),
'jpeg', 'jpg' => new JpegEncoder(quality: $this->quality),
'bmp' => new BmpEncoder(),
'gif' => new GifEncoder(),
'png' => new PngEncoder(),
'tiff', 'tif' => new TiffEncoder(quality: $this->quality),
'jp2', 'j2k', 'jpf', 'jpm', 'jpg2', 'j2c', 'jpc', 'jpx' => new Jpeg2000Encoder(quality: $this->quality),
'heic', 'heif' => new HeicEncoder(quality: $this->quality),
'webp' => new WebpEncoder(...$this->options),
'avif' => new AvifEncoder(...$this->options),
'jpeg', 'jpg' => new JpegEncoder(...$this->options),
'bmp' => new BmpEncoder(...$this->options),
'gif' => new GifEncoder(...$this->options),
'png' => new PngEncoder(...$this->options),
'tiff', 'tif' => new TiffEncoder(...$this->options),
'jp2', 'j2k', 'jpf', 'jpm', 'jpg2', 'j2c', 'jpc', 'jpx' => new Jpeg2000Encoder(...$this->options),
'heic', 'heif' => new HeicEncoder(...$this->options),
default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'),
};
}
Expand Down
5 changes: 2 additions & 3 deletions src/Encoders/FilePathEncoder.php
Expand Up @@ -13,14 +13,13 @@ class FilePathEncoder extends FileExtensionEncoder
* Create new encoder instance to encode to format of file extension in given path
*
* @param null|string $path
* @param int $quality
* @return void
*/
public function __construct(protected ?string $path = null, public int $quality = self::DEFAULT_QUALITY)
public function __construct(protected ?string $path = null, mixed ...$options)
{
parent::__construct(
is_null($path) ? $path : pathinfo($path, PATHINFO_EXTENSION),
$quality
...$options
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Encoders/GifEncoder.php
Expand Up @@ -8,7 +8,7 @@

class GifEncoder extends SpecializableEncoder
{
public function __construct()
public function __construct(public bool $interlaced = false)
{
}
}
6 changes: 4 additions & 2 deletions src/Encoders/JpegEncoder.php
Expand Up @@ -8,7 +8,9 @@

class JpegEncoder extends SpecializableEncoder
{
public function __construct(public int $quality = self::DEFAULT_QUALITY)
{
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public bool $progressive = false
) {
}
}
28 changes: 14 additions & 14 deletions src/Encoders/MediaTypeEncoder.php
Expand Up @@ -12,17 +12,17 @@

class MediaTypeEncoder extends AbstractEncoder
{
protected array $options = [];

/**
* Create new encoder instance
*
* @param null|string $mediaType Target media type for example "image/jpeg"
* @param int $quality
* @return void
*/
public function __construct(
public ?string $mediaType = null,
public int $quality = self::DEFAULT_QUALITY
) {
public function __construct(public ?string $mediaType = null, mixed ...$options)
{
$this->options = $options;
}

/**
Expand Down Expand Up @@ -50,29 +50,29 @@ protected function encoderByMediaType(string $mediaType): EncoderInterface
{
return match (strtolower($mediaType)) {
'image/webp',
'image/x-webp' => new WebpEncoder(quality: $this->quality),
'image/x-webp' => new WebpEncoder(...$this->options),
'image/avif',
'image/x-avif' => new AvifEncoder(quality: $this->quality),
'image/x-avif' => new AvifEncoder(...$this->options),
'image/jpeg',
'image/jpg',
'image/pjpeg' => new JpegEncoder(quality: $this->quality),
'image/pjpeg' => new JpegEncoder(...$this->options),
'image/bmp',
'image/ms-bmp',
'image/x-bitmap',
'image/x-bmp',
'image/x-ms-bmp',
'image/x-win-bitmap',
'image/x-windows-bmp',
'image/x-xbitmap' => new BmpEncoder(),
'image/gif' => new GifEncoder(),
'image/x-xbitmap' => new BmpEncoder(...$this->options),
'image/gif' => new GifEncoder(...$this->options),
'image/png',
'image/x-png' => new PngEncoder(),
'image/tiff' => new TiffEncoder(quality: $this->quality),
'image/x-png' => new PngEncoder(...$this->options),
'image/tiff' => new TiffEncoder(...$this->options),
'image/jp2',
'image/jpx',
'image/jpm' => new Jpeg2000Encoder(quality: $this->quality),
'image/jpm' => new Jpeg2000Encoder(...$this->options),
'image/heic',
'image/heif', => new HeicEncoder(quality: $this->quality),
'image/heif', => new HeicEncoder(...$this->options),
default => throw new EncoderException('No encoder found for media type (' . $mediaType . ').'),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Image.php
Expand Up @@ -299,7 +299,7 @@ public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedIm
*
* @see ImageInterface::save()
*/
public function save(?string $path = null, ...$options): ImageInterface
public function save(?string $path = null, mixed ...$options): ImageInterface
{
$path = is_null($path) ? $this->origin()->filePath() : $path;

Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/ImageInterface.php
Expand Up @@ -88,7 +88,7 @@ public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedIm
* @throws RuntimeException
* @return ImageInterface
*/
public function save(?string $path = null, ...$options): self;
public function save(?string $path = null, mixed ...$options): self;

/**
* Apply given modifier to current image
Expand Down
18 changes: 18 additions & 0 deletions tests/ImagickTestCase.php
Expand Up @@ -35,4 +35,22 @@ public function createTestImage(int $width, int $height): Image
new Core($imagick)
);
}

public function createTestAnimation(): Image
{
$imagick = new Imagick();
$imagick->setFormat('gif');

for ($i = 0; $i < 3; $i++) {
$frame = new Imagick();
$frame->newImage(3, 2, new ImagickPixel('rgb(255, 0, 0)'), 'gif');
$frame->setImageDelay(10);
$imagick->addImage($frame);
}

return new Image(
new Driver(),
new Core($imagick)
);
}
}
57 changes: 57 additions & 0 deletions tests/Traits/CanDetectProgressiveJpeg.php
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Tests\Traits;

use Intervention\Image\Traits\CanBuildFilePointer;

trait CanDetectProgressiveJpeg
{
use CanBuildFilePointer;

/**
* Checks if the given image data is progressive encoded Jpeg format
*
* @param string $imagedata
* @return bool
*/
private function isProgressiveJpeg(string $imagedata): bool
{
$f = $this->buildFilePointer($imagedata);

while (!feof($f)) {
if (unpack('C', fread($f, 1))[1] !== 0xff) {
return false;
}

$blockType = unpack('C', fread($f, 1))[1];

switch (true) {
case $blockType == 0xd8:
case $blockType >= 0xd0 && $blockType <= 0xd7:
break;

case $blockType == 0xc0:
fclose($f);
return false;

case $blockType == 0xc2:
fclose($f);
return true;

case $blockType == 0xd9:
break 2;

default:
$blockSize = unpack('n', fread($f, 2))[1];
fseek($f, $blockSize - 2, SEEK_CUR);
break;
}
}

fclose($f);

return false;
}
}
26 changes: 26 additions & 0 deletions tests/Unit/Drivers/Gd/Encoders/GifEncoderTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Tests\Unit\Drivers\Gd\Encoders;

use Intervention\Gif\Decoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Encoders\GifEncoder;
Expand All @@ -20,5 +21,30 @@ public function testEncode(): void
$encoder = new GifEncoder();
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertFalse(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}

public function testEncodeInterlaced(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new GifEncoder(interlaced: true);
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertTrue(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}

public function testEncodeInterlacedAnimation(): void
{
$image = $this->createTestAnimation(3, 2);
$encoder = new GifEncoder(interlaced: true);
$result = $encoder->encode($image);
$this->assertMediaType('image/gif', (string) $result);
$this->assertTrue(
Decoder::decode((string) $result)->getFirstFrame()->getImageDescriptor()->isInterlaced()
);
}
}
12 changes: 12 additions & 0 deletions tests/Unit/Drivers/Gd/Encoders/JpegEncoderTest.php
Expand Up @@ -8,17 +8,29 @@
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Encoders\JpegEncoder;
use Intervention\Image\Tests\GdTestCase;
use Intervention\Image\Tests\Traits\CanDetectProgressiveJpeg;

#[RequiresPhpExtension('gd')]
#[CoversClass(\Intervention\Image\Encoders\JpegEncoder::class)]
#[CoversClass(\Intervention\Image\Drivers\Gd\Encoders\JpegEncoder::class)]
final class JpegEncoderTest extends GdTestCase
{
use CanDetectProgressiveJpeg;

public function testEncode(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new JpegEncoder(75);
$result = $encoder->encode($image);
$this->assertMediaType('image/jpeg', (string) $result);
}

public function testEncodeProgressive(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new JpegEncoder(progressive: true);
$result = $encoder->encode($image);
$this->assertMediaType('image/jpeg', (string) $result);
$this->assertTrue($this->isProgressiveJpeg((string) $result));
}
}

0 comments on commit d982359

Please sign in to comment.