Skip to content

Support AVIF and HEIC in Imagick driver #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 30, 2021
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
20 changes: 20 additions & 0 deletions src/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,24 @@ private function applyImageOptions(\Imagick $image, array $options, $path)
$image->setOption('webp:lossless', $options['webp_lossless']);
}
break;
case 'avif':
case 'heic':
if (!empty($options[$format . '_lossless'])) {
$image->setimagecompressionquality(100);
$image->setcompressionquality(100);
} else {
if (!isset($options[$format . '_quality'])) {
if (isset($options['quality'])) {
$options[$format . '_quality'] = $options['quality'];
}
}
if (isset($options[$format . '_quality'])) {
$options[$format . '_quality'] = max(1, min(99, $options[$format . '_quality']));
$image->setimagecompressionquality($options[$format . '_quality']);
$image->setcompressionquality($options[$format . '_quality']);
}
}
break;
}
if (isset($options['resolution-units']) && isset($options['resolution-x']) && isset($options['resolution-y'])) {
if (empty($options['resampling-filter'])) {
Expand Down Expand Up @@ -940,6 +958,8 @@ private function getMimeType($format)
'wbmp' => 'image/vnd.wap.wbmp',
'xbm' => 'image/xbm',
'webp' => 'image/webp',
'avif' => 'image/avif',
'heic' => 'image/heic',
'bmp' => 'image/bmp',
);

Expand Down
13 changes: 13 additions & 0 deletions src/Imagick/Imagine.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,19 @@ private function createPalette(\Imagick $imagick)
return new CMYK();
case \Imagick::COLORSPACE_GRAY:
return new Grayscale();
case \Imagick::COLORSPACE_YCBCR:
try {
$profile = $imagick->getImageProfile('icc');
} catch (\ImagickException $e) {
$profile = null;
}
$imagick->transformImageColorspace(\Imagick::COLORSPACE_SRGB);

if ($profile) {
$imagick->setImageProfile('icc', $profile);
}

return new RGB();
default:
throw new NotSupportedException('Only RGB and CMYK colorspace are currently supported');
}
Expand Down
Binary file added tests/fixtures/avif-image.avif
Binary file not shown.
Binary file added tests/fixtures/heic-image.heic
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/tests/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public function testSaveCompressionQuality($format, array $smallSizeOptions, arr
if ($format === 'webp' && !function_exists('imagewebp')) {
$this->markTestSkipped('GD webp support is not enabled');
}
if ($format === 'avif' || $format === 'heic') {
$this->markTestSkipped('GD does not support ' . strtoupper($format));
}

return parent::testSaveCompressionQuality($format, $smallSizeOptions, $bigSizeOptions);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/tests/Gd/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ public function testShouldOpenAWebPImage()
return parent::testShouldOpenAWebPImage();
}

/**
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAAvifImage()
*/
public function testShouldOpenAAvifImage()
{
$this->markTestSkipped('GD does not support AVIF');
}

/**
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAHeicImage()
*/
public function testShouldOpenAHeicImage()
{
$this->markTestSkipped('GD does not support HEIC');
}

/**
* {@inheritdoc}
*
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/Gmagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ public function pasteWithAlphaProvider()
public function testSaveCompressionQuality($format, array $smallSizeOptions, array $bigSizeOptions)
{
$gmagick = new \Gmagick();
if ($format === 'webp' && !in_array('WEBP', $gmagick->queryformats('WEBP'), true)) {
$this->markTestSkipped('Gmagick webp support is not enabled');
if (in_array($format, array('webp', 'avif', 'heic'), true) && !in_array(strtoupper($format), $gmagick->queryformats(strtoupper($format)), true)) {
$this->markTestSkipped('Gmagick ' . $format . ' support is not enabled');
}

return parent::testSaveCompressionQuality($format, $smallSizeOptions, $bigSizeOptions);
Expand Down
30 changes: 30 additions & 0 deletions tests/tests/Gmagick/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ public function testShouldOpenAWebPImage()
return parent::testShouldOpenAWebPImage();
}

/**
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAAvifImage()
*/
public function testShouldOpenAAvifImage()
{
$gmagick = new \Gmagick();
if (!in_array('AVIF', $gmagick->queryformats('AVIF'), true)) {
$this->markTestSkipped('Gmagick AVIF support is not enabled');
}

return parent::testShouldOpenAAvifImage();
}

/**
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAHeicImage()
*/
public function testShouldOpenAHeicImage()
{
$gmagick = new \Gmagick();
if (!in_array('HEIC', $gmagick->queryformats('HEIC'), true)) {
$this->markTestSkipped('Gmagick HEIC support is not enabled');
}

return parent::testShouldOpenAHeicImage();
}

/**
* {@inheritdoc}
*
Expand Down
4 changes: 4 additions & 0 deletions tests/tests/Image/AbstractImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,10 @@ public function imageCompressionQualityProvider()
array('jpg', array('jpeg_quality' => 0), array('jpeg_quality' => 100)),
array('png', array('png_compression_level' => 9), array('png_compression_level' => 0)),
array('webp', array('webp_quality' => 0), array('webp_quality' => 100)),
array('avif', array('avif_quality' => 0), array('avif_quality' => 100)),
array('avif', array('avif_quality' => 0), array('avif_lossless' => true)),
array('heic', array('heic_quality' => 0), array('heic_quality' => 100)),
array('heic', array('heic_quality' => 0), array('heic_lossless' => true)),
);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/tests/Image/AbstractImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ public function testShouldOpenAWebPImage()
$this->assertEquals(realpath($source), $metadata['filepath']);
}

public function testShouldOpenAAvifImage()
{
$source = IMAGINE_TEST_FIXTURESFOLDER . '/avif-image.avif';
$factory = $this->getImagine();
$image = $factory->open($source);
$size = $image->getSize();
$this->assertInstanceOf('Imagine\Image\ImageInterface', $image);
$this->assertEquals(100, $size->getWidth());
$this->assertEquals(100, $size->getHeight());
$metadata = $image->metadata();
$this->assertEquals($source, $metadata['uri']);
$this->assertEquals(realpath($source), $metadata['filepath']);
}

public function testShouldOpenAHeicImage()
{
$source = IMAGINE_TEST_FIXTURESFOLDER . '/heic-image.heic';
$factory = $this->getImagine();
$image = $factory->open($source);
$size = $image->getSize();
$this->assertInstanceOf('Imagine\Image\ImageInterface', $image);
$this->assertEquals(100, $size->getWidth());
$this->assertEquals(100, $size->getHeight());
$metadata = $image->metadata();
$this->assertEquals($source, $metadata['uri']);
$this->assertEquals(realpath($source), $metadata['filepath']);
}

public function testShouldOpenAnSplFileResource()
{
$source = IMAGINE_TEST_FIXTURESFOLDER . '/google.png';
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/Imagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public function testOptimize()
*/
public function testSaveCompressionQuality($format, array $smallSizeOptions, array $bigSizeOptions)
{
if ($format === 'webp' && !in_array('WEBP', \Imagick::queryFormats('WEBP'), true)) {
$this->markTestSkipped('Imagick WebP support is not enabled');
if (in_array($format, array('webp', 'avif', 'heic'), true) && !in_array(strtoupper($format), \Imagick::queryFormats(strtoupper($format)), true)) {
$this->markTestSkipped('Imagick ' . $format . ' support is not enabled');
}

return parent::testSaveCompressionQuality($format, $smallSizeOptions, $bigSizeOptions);
Expand Down
28 changes: 28 additions & 0 deletions tests/tests/Imagick/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ public function testShouldOpenAWebPImage()
return parent::testShouldOpenAWebPImage();
}

/**
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAAvifImage()
*/
public function testShouldOpenAAvifImage()
{
if (!in_array('AVIF', \Imagick::queryFormats('AVIF'), true)) {
$this->markTestSkipped('Imagick AVIF support is not enabled');
}

return parent::testShouldOpenAAvifImage();
}

/**
* {@inheritdoc}
*
* @see \Imagine\Test\Image\AbstractImagineTest::testShouldOpenAHeicImage()
*/
public function testShouldOpenAHeicImage()
{
if (!in_array('HEIC', \Imagick::queryFormats('HEIC'), true)) {
$this->markTestSkipped('Imagick HEIC support is not enabled');
}

return parent::testShouldOpenAHeicImage();
}

/**
* {@inheritdoc}
*
Expand Down