Skip to content

Commit

Permalink
Support JXL in Imagick driver
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi authored and mlocati committed Sep 30, 2021
1 parent a66bee6 commit 6de2844
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,22 @@ private function applyImageOptions(\Imagick $image, array $options, $path)
}
}
break;
case 'jxl':
if (!isset($options[$format . '_quality'])) {
if (isset($options['quality'])) {
$options[$format . '_quality'] = $options['quality'];
}
}
if (isset($options[$format . '_quality'])) {
$options[$format . '_quality'] = max(9, min(99, $options[$format . '_quality']));
$image->setimagecompressionquality($options[$format . '_quality']);
$image->setcompressionquality($options[$format . '_quality']);
}
if (!empty($options[$format . '_lossless'])) {
$image->setimagecompressionquality(100);
$image->setcompressionquality(100);
}
break;
}
if (isset($options['resolution-units']) && isset($options['resolution-x']) && isset($options['resolution-y'])) {
if (empty($options['resampling-filter'])) {
Expand Down Expand Up @@ -953,6 +969,7 @@ private function getMimeType($format)
static $mimeTypes = array(
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jxl' => 'image/jxl',
'gif' => 'image/gif',
'png' => 'image/png',
'wbmp' => 'image/vnd.wap.wbmp',
Expand Down
Binary file added tests/fixtures/jxl-image.jxl
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/tests/Gd/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ 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') {
if ($format === 'avif' || $format === 'heic' || $format === 'jxl') {
$this->markTestSkipped('GD does not support ' . strtoupper($format));
}

Expand Down
10 changes: 10 additions & 0 deletions tests/tests/Gd/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public function testShouldOpenAHeicImage()
$this->markTestSkipped('GD does not support HEIC');
}

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

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

Expand Down
15 changes: 15 additions & 0 deletions tests/tests/Gmagick/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ public function testShouldOpenAHeicImage()
return parent::testShouldOpenAHeicImage();
}

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

return parent::testShouldOpenAJxlImage();
}

/**
* {@inheritdoc}
*
Expand Down
2 changes: 2 additions & 0 deletions tests/tests/Image/AbstractImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,8 @@ public function imageCompressionQualityProvider()
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)),
array('jxl', array('jxl_quality' => 0), array('jxl_quality' => 100)),
array('jxl', array('jxl_quality' => 0), array('jxl_lossless' => true)),
);
}

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

public function testShouldOpenAJxlImage()
{
$source = IMAGINE_TEST_FIXTURESFOLDER . '/jxl-image.jxl';
$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
2 changes: 1 addition & 1 deletion tests/tests/Imagick/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function testOptimize()
*/
public function testSaveCompressionQuality($format, array $smallSizeOptions, array $bigSizeOptions)
{
if (in_array($format, array('webp', 'avif', 'heic'), true) && !in_array(strtoupper($format), \Imagick::queryFormats(strtoupper($format)), true)) {
if (in_array($format, array('webp', 'avif', 'heic', 'jxl'), true) && !in_array(strtoupper($format), \Imagick::queryFormats(strtoupper($format)), true)) {
$this->markTestSkipped('Imagick ' . $format . ' support is not enabled');
}

Expand Down
14 changes: 14 additions & 0 deletions tests/tests/Imagick/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ public function testShouldOpenAHeicImage()
return parent::testShouldOpenAHeicImage();
}

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

return parent::testShouldOpenAJxlImage();
}

/**
* {@inheritdoc}
*
Expand Down

0 comments on commit 6de2844

Please sign in to comment.