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 committed Jun 12, 2021
1 parent b6b6315 commit fe602ae
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,22 @@ private function applyImageOptions(\Imagick $image, array $options, $path)
$image->setOption('webp:lossless', $options['webp_lossless']);
}
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 @@ -935,6 +951,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.
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 === 'jxl') {
$this->markTestSkipped('GD does not support ' . strtoupper($format));
}

return parent::testSaveCompressionQuality($format, $smallSizeOptions, $bigSizeOptions);
}
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 @@ -47,6 +47,16 @@ public function testShouldOpenAWebPImage()
return parent::testShouldOpenAWebPImage();
}

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

/**
* {@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', 'jxl'), 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
15 changes: 15 additions & 0 deletions tests/tests/Gmagick/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ public function testShouldOpenAWebPImage()
return parent::testShouldOpenAWebPImage();
}

/**
* {@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 @@ -981,6 +981,8 @@ 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('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 @@ -61,6 +61,20 @@ public function testShouldOpenAWebPImage()
$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
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', 'jxl'), 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
14 changes: 14 additions & 0 deletions tests/tests/Imagick/ImagineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public function testShouldOpenAWebPImage()
return parent::testShouldOpenAWebPImage();
}

/**
* {@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 fe602ae

Please sign in to comment.