Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Intervention/Image/AbstractEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ abstract protected function processIco();
*/
abstract protected function processWebp();

/**
* Processes and returns image as Avif encoded string
*
* @return string
*/
abstract protected function processAvif();

/**
* Process a given image
*
Expand Down Expand Up @@ -168,6 +175,11 @@ public function process(Image $image, $format = null, $quality = null)
case 'image/x-webp':
$this->result = $this->processWebp();
break;

case 'avif':
case 'image/avif':
$this->result = $this->processAvif();
break;

default:
throw new NotSupportedException(
Expand Down
12 changes: 12 additions & 0 deletions src/Intervention/Image/Gd/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,16 @@ protected function processPsd()
"PSD format is not supported by Gd Driver."
);
}

/**
* Processes and returns encoded image as AVIF string
*
* @return string
*/
protected function processAvif()
{
throw new NotSupportedException(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@freshleafmedia could you update this file to include the GD support?

    protected function processAvif()
    {
        if ( ! function_exists('imageavif')) {
            throw new NotSupportedException(
                "Avif format is not supported by PHP installation."
            );
        }

        ob_start();
        imageavif($this->image->getCore(), null, $this->quality);
        $this->image->mime = defined('IMAGETYPE_AVIF') ? image_type_to_mime_type(IMAGETYPE_AVIF) : 'image/avif';
        $buffer = ob_get_contents();
        ob_end_clean();

        return $buffer;
    }

Copy link

@JoyceBabu JoyceBabu Jul 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AVIF Support is not yet available in PHP GD.

https://bugs.php.net/bug.php?id=80828

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not quite accurate, php/php-src#7026 is merged so GD with avif will be available in the next patch version (8.1)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're both right. AVIF is now supported in libgd. The support in PHP is now merged, and it's set to be released in PHP 8.1. ☺️

"AVIF format is not supported by Gd Driver."
);
}
}
22 changes: 22 additions & 0 deletions src/Intervention/Image/Imagick/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,26 @@ protected function processPsd()

return $imagick->getImagesBlob();
}

protected function processAvif()
{
if ( ! \Imagick::queryFormats('AVIF')) {
throw new NotSupportedException(
"AVIF format is not supported by Imagick installation."
);
}

$format = 'avif';
$compression = \Imagick::COMPRESSION_UNDEFINED;

$imagick = $this->image->getCore();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
$imagick->setCompression($compression);
$imagick->setImageCompression($compression);
$imagick->setCompressionQuality($this->quality);
$imagick->setImageCompressionQuality($this->quality);

return $imagick->getImagesBlob();
}
}
22 changes: 22 additions & 0 deletions tests/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ public function testProcessWebpGd()
}
}

/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
public function testProcessAvifGd()
{
$core = imagecreatefromjpeg(__DIR__.'/images/test.jpg');
$encoder = new GdEncoder;
$image = Mockery::mock('\Intervention\Image\Image');
$img = $encoder->process($image, 'avif', 90);
$this->assertInstanceOf('Intervention\Image\Image', $img);
}

/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
Expand Down Expand Up @@ -180,6 +192,16 @@ public function testProcessWebpImagick()
$img = $encoder->process($image, 'webp', 90);
}

/**
* @expectedException \Intervention\Image\Exception\NotSupportedException
*/
public function testProcessAvifImagick()
{
$encoder = new ImagickEncoder;
$image = Mockery::mock('\Intervention\Image\Image');
$img = $encoder->process($image, 'avif', 90);
}

public function testProcessTiffImagick()
{
$core = $this->getImagickMock('tiff');
Expand Down