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
1 change: 1 addition & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ jobs:
libheif-dev \
libheif-plugin-aomenc \
libheif-plugin-x265 \
libjxl-dev \
libcgif-dev \
libimagequant-dev \
libopenjp2-7-dev \
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RUN apt update \
libheif-dev \
libheif-plugin-aomenc \
libheif-plugin-x265 \
libjxl-dev \
libcgif-dev \
libimagequant-dev \
libopenjp2-7-dev \
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
],
"require": {
"php": "^8.3",
"intervention/image": "^4.0.2",
"intervention/image": "^4.2",
"jcupitt/vips": "^2.6"
},
"require-dev": {
Expand Down
71 changes: 71 additions & 0 deletions src/Encoders/JxlEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Vips\Encoders;

use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\JxlEncoder as GenericJxlEncoder;
use Intervention\Image\Exceptions\EncoderException;
use Intervention\Image\Exceptions\StreamException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\StateException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Intervention\Image\MediaType;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\Exception as VipsException;
use Jcupitt\Vips\ForeignKeep;

class JxlEncoder extends GenericJxlEncoder implements SpecializedInterface
{
/**
* {@inheritdoc}
*
* @see Intervention\Image\Interfaces\EncoderInterface::encode()
*
* @throws InvalidArgumentException
* @throws EncoderException
* @throws StreamException
* @throws StateException
*/
public function encode(ImageInterface $image): EncodedImage
{
try {
$result = $image->core()->native()->writeToBuffer('.jxl', $this->options());
} catch (VipsException $e) {
throw new EncoderException('Failed to encode JXL image format', previous: $e);
}

return new EncodedImage($result, MediaType::IMAGE_JXL->value);
}

/**
* libvips' jxlsave has a native lossless flag. JXL has no separate lossless
* setting beyond maximum quality, so it is enabled at Q 100, matching the
* AVIF and HEIC encoders of this driver.
*
* @throws StateException
* @return array{lossless: bool, Q: int, keep?: int, strip?: bool}
*/
private function options(): array
{
$options = [
'lossless' => $this->quality === 100,
'Q' => $this->quality,
];

$strip = $this->strip || $this->driver()->config()->strip;

if (VipsConfig::atLeast(8, 15)) {
$keepAll = VipsConfig::atLeast(8, 18)
? ForeignKeep::ALL
: ForeignKeep::ALL & ~ForeignKeep::GAINMAP;
$options['keep'] = $strip ? ForeignKeep::ICC : $keepAll;
} else {
$options['strip'] = $strip;
}

return $options;
}
}
4 changes: 4 additions & 0 deletions src/LoaderDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public function formats(): array
case 'jp2k':
$formats[] = Format::JP2;
break;

case 'jxl':
$formats[] = Format::JXL;
break;
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ public static function supportsDataProvider(): Generator
yield [true, 'image/heic'];
yield [true, 'image/heif'];

yield [true, Format::JXL];
yield [true, MediaType::IMAGE_JXL];
yield [true, MediaType::IMAGE_X_JXL];
yield [true, FileExtension::JXL];
yield [true, 'jxl'];
yield [true, 'image/jxl'];
yield [true, 'image/x-jxl'];

yield [false, 'tga'];
yield [false, 'image/tga'];
yield [false, 'image/x-targa'];
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Encoders/JxlEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Vips\Tests\Unit\Encoders;

use Intervention\Image\Drivers\Vips\Driver;
use Intervention\Image\Drivers\Vips\Encoders\JxlEncoder;
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(JxlEncoder::class)]
final class JxlEncoderTest extends BaseTestCase
{
public function testEncode(): void
{
$image = (new Driver())->createImage(3, 2);
$encoder = new JxlEncoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$this->assertMediaType(['image/jxl', 'image/x-jxl'], $result);
$this->assertEquals('image/jxl', $result->mimetype());
}
}