Skip to content

Commit

Permalink
Add avif encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 3, 2023
1 parent 8eb1394 commit af2ee6c
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ RUN apt update \
&& apt install -y \
libpng-dev \
libicu-dev \
libavif-dev \
libpq-dev \
libzip-dev \
zip \
Expand All @@ -13,7 +14,7 @@ RUN apt update \
libmagickwand-dev \
libwebp-dev \
&& pecl install imagick \
&& docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp \
&& docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp --with-avif \
&& docker-php-ext-enable imagick \
&& docker-php-ext-install \
intl \
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "^8.1",
"intervention/gif": "^3.0",
"intervention/mimesniffer": "^0.4.2"
"intervention/mimesniffer": "^0.5"
},
"require-dev": {
"phpunit/phpunit": "^9",
Expand Down
7 changes: 7 additions & 0 deletions src/Drivers/Abstract/AbstractImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public function toBmp(): EncodedImage
return $this->toBitmap();
}

public function toAvif(): EncodedImage
{
return $this->encode(
$this->resolveDriverClass('Encoders\AvifEncoder')
);
}

public function greyscale(): ImageInterface
{
return $this->modify(
Expand Down
25 changes: 25 additions & 0 deletions src/Drivers/Gd/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Intervention\Image\Drivers\Gd\Encoders;

use Intervention\Image\Drivers\Abstract\Encoders\AbstractEncoder;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;

class AvifEncoder extends AbstractEncoder implements EncoderInterface
{
public function __construct(int $quality)
{
$this->quality = $quality;
}

public function encode(ImageInterface $image): EncodedImage
{
$data = $this->getBuffered(function () use ($image) {
imageavif($image->getFrame()->getCore(), null, $this->quality);
});

return new EncodedImage($data, 'image/avif');
}
}
26 changes: 26 additions & 0 deletions src/Drivers/Imagick/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Intervention\Image\Drivers\Imagick\Encoders;

use Imagick;
use Intervention\Image\Drivers\Abstract\Encoders\AbstractEncoder;
use Intervention\Image\EncodedImage;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;

class AvifEncoder extends AbstractEncoder implements EncoderInterface
{
public function encode(ImageInterface $image): EncodedImage
{
$format = 'avif';
$compression = Imagick::COMPRESSION_ZIP;

$imagick = $image->getFrame()->getCore();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);

return new EncodedImage($imagick->getImagesBlob(), 'image/avif');
}
}
6 changes: 6 additions & 0 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public function toWebp(int $quality = 75): EncodedImage;
*/
public function toGif(): EncodedImage;

/**
* Encode image to avif format
*
* @return EncodedImage
*/
public function toAvif(): EncodedImage;

/**
* Encode image to png format
Expand Down
33 changes: 33 additions & 0 deletions tests/Drivers/Gd/Encoders/AvifEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

use Intervention\Image\Collection;
use Intervention\Image\Drivers\Gd\Encoders\AvifEncoder;
use Intervention\Image\Drivers\Gd\Frame;
use Intervention\Image\Drivers\Gd\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageAvif;

/**
* @requires extension gd
* @covers \Intervention\Image\Drivers\Gd\Encoders\AvifEncoder
*/
class AvifEncoderTest extends TestCase
{
protected function getTestImage(): Image
{
return new Image(new Collection([
new Frame(imagecreatetruecolor(3, 2))
]));
}

public function testEncode(): void
{
$image = $this->getTestImage();
$encoder = new AvifEncoder(10);
$result = $encoder->encode($image);
$this->assertTrue(MimeSniffer::createFromString($result)->matches(new ImageAvif()));
}
}
34 changes: 34 additions & 0 deletions tests/Drivers/Imagick/Encoders/AvifEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Intervention\Image\Tests\Drivers\Imagick\Encoders;

use Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Encoders\AvifEncoder;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Tests\TestCase;
use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageAvif;

/**
* @requires extension imagick
* @covers \Intervention\Image\Drivers\Imagick\Encoders\JpegEncoder
*/
class AvifEncoderTest extends TestCase
{
protected function getTestImage(): Image
{
$imagick = new Imagick();
$imagick->newImage(3, 2, new ImagickPixel('red'), 'png');

return new Image($imagick);
}

public function testEncode(): void
{
$image = $this->getTestImage();
$encoder = new AvifEncoder(10);
$result = $encoder->encode($image);
$this->assertTrue(MimeSniffer::createFromString($result)->matches(new ImageAvif()));
}
}

0 comments on commit af2ee6c

Please sign in to comment.