Skip to content

Commit

Permalink
Decide to use "keep" or "strip" depending on libvips version (#67)
Browse files Browse the repository at this point in the history
* keep option is only available from 8.15
* phpdoc
* PHPstan fixes
* typo
  • Loading branch information
deluxetom authored Jan 19, 2025
1 parent 40a0979 commit 7c1bd64
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 45 deletions.
30 changes: 22 additions & 8 deletions src/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Encoders\AvifEncoder as GenericAvifEncoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\ForeignKeep;

class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
Expand All @@ -19,16 +20,29 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$keep = $this->strip || (is_null($this->strip) &&
$this->driver()->config()->strip) ? ForeignKeep::ICC : ForeignKeep::ALL;
$result = $image->core()->native()->writeToBuffer('.avif', $this->getOptions());

$result = $image->core()->native()->writeToBuffer('.avif', [
return new EncodedImage($result, 'image/avif');
}

/**
* @return array{lossless: bool, Q: int, keep?: int, strip?: bool}
*/
protected function getOptions(): array
{
$options = [
'lossless' => $this->quality === 100,
'Q' => $this->quality,
'keep' => $keep,
// 'speed' => 6, // Speed (faster encoding)/*
// 'effort' => 4, // Compression effort*/
]);
];

return new EncodedImage($result, 'image/avif');
$strip = $this->strip || $this->driver()->config()->strip;

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

return $options;
}
}
28 changes: 22 additions & 6 deletions src/Encoders/HeicEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Encoders\HeicEncoder as GenericHeicEncoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\ForeignKeep;

class HeicEncoder extends GenericHeicEncoder implements SpecializedInterface
Expand All @@ -19,14 +20,29 @@ class HeicEncoder extends GenericHeicEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$keep = $this->strip || (is_null($this->strip) &&
$this->driver()->config()->strip) ? ForeignKeep::ICC : ForeignKeep::ALL;
$result = $image->core()->native()->writeToBuffer('.heic', $this->getOptions());

$result = $image->core()->native()->writeToBuffer('.heic', [
return new EncodedImage($result, 'image/heic');
}

/**
* @return array{lossless: bool, Q: int, keep?: int, strip?: bool}
*/
protected function getOptions(): array
{
$options = [
'lossless' => $this->quality === 100,
'Q' => $this->quality,
'keep' => $keep,
]);
];

return new EncodedImage($result, 'image/heic');
$strip = $this->strip || $this->driver()->config()->strip;

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

return $options;
}
}
27 changes: 21 additions & 6 deletions src/Encoders/Jpeg2000Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Encoders\Jpeg2000Encoder as GenericJpeg2000Encoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\ForeignKeep;

class Jpeg2000Encoder extends GenericJpeg2000Encoder implements SpecializedInterface
Expand All @@ -25,15 +26,29 @@ public function encode(ImageInterface $image): EncodedImage
$vipsImage = $image->core()->frame(0)->native();
}

$keep = $this->strip || (is_null($this->strip) &&
$this->driver()->config()->strip) ? ForeignKeep::ICC : ForeignKeep::ALL;
$result = $vipsImage->writeToBuffer('.j2k', $this->getOptions());

$result = $vipsImage->writeToBuffer('.j2k', [
return new EncodedImage($result, 'image/jp2');
}

/**
* @return array{lossless: bool, Q: int, keep?: int, strip?: bool}
*/
protected function getOptions(): array
{
$options = [
'lossless' => $this->quality === 100,
'Q' => $this->quality,
'keep' => $keep,
]);
];

return new EncodedImage($result, 'image/jp2');
$strip = $this->strip || $this->driver()->config()->strip;

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

return $options;
}
}
38 changes: 27 additions & 11 deletions src/Encoders/JpegEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Intervention\Image\Colors\Rgb\Colorspace as Rgb;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\JpegEncoder as GenericJpegEncoder;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\ForeignKeep;

class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
Expand All @@ -20,27 +22,41 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$blendingColor = $this->driver()->handleInput(
$this->driver()->config()->blendingColor
);

$vipsImage = $image->core()->native();

if ($image->isAnimated()) {
$vipsImage = $image->core()->frame(0)->native();
}

$keep = $this->strip || (is_null($this->strip) &&
$this->driver()->config()->strip) ? ForeignKeep::ICC : ForeignKeep::ALL;
$result = $vipsImage->writeToBuffer('.jpg', $this->getOptions());

return new EncodedImage($result, 'image/jpeg');
}

$result = $vipsImage->writeToBuffer('.jpg', [
/**
* @throws RuntimeException
*
* @return array{Q: int, optimize_coding: bool, background: array<int>, keep?: int, strip?: bool}
*/
protected function getOptions(): array
{
$options = [
'Q' => $this->quality,
'interlace' => $this->progressive,
'keep' => $keep,
'optimize_coding' => true,
'background' => array_slice($blendingColor->convertTo(Rgb::class)->toArray(), 0, 3),
]);
'background' => array_slice($this->driver()->handleInput(
$this->driver()->config()->blendingColor
)->convertTo(Rgb::class)->toArray(), 0, 3),
];

return new EncodedImage($result, 'image/jpeg');
$strip = $this->strip || $this->driver()->config()->strip;

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

return $options;
}
}
27 changes: 21 additions & 6 deletions src/Encoders/TiffEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Encoders\TiffEncoder as GenericTiffEncoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\ForeignKeep;

class TiffEncoder extends GenericTiffEncoder implements SpecializedInterface
Expand All @@ -19,15 +20,29 @@ class TiffEncoder extends GenericTiffEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$keep = $this->strip || (is_null($this->strip) &&
$this->driver()->config()->strip) ? ForeignKeep::ICC : ForeignKeep::ALL;
$result = $image->core()->native()->writeToBuffer('.tiff', $this->getOptions());

$result = $image->core()->native()->writeToBuffer('.tiff', [
return new EncodedImage($result, 'image/tiff');
}

/**
* @return array{lossless: bool, Q: int, keep?: int, strip?: bool}
*/
protected function getOptions(): array
{
$options = [
'lossless' => $this->quality === 100,
'Q' => $this->quality,
'keep' => $keep,
]);
];

return new EncodedImage($result, 'image/tiff');
$strip = $this->strip || $this->driver()->config()->strip;

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

return $options;
}
}
27 changes: 21 additions & 6 deletions src/Encoders/WebpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Intervention\Image\Encoders\WebpEncoder as GenericWebpEncoder;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\ForeignKeep;

class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
Expand All @@ -19,15 +20,29 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
*/
public function encode(ImageInterface $image): EncodedImage
{
$keep = $this->strip || (is_null($this->strip) &&
$this->driver()->config()->strip) ? ForeignKeep::ICC : ForeignKeep::ALL;
$result = $image->core()->native()->writeToBuffer('.webp', $this->getOptions());

$result = $image->core()->native()->writeToBuffer('.webp', [
return new EncodedImage($result, 'image/webp');
}

/**
* @return array{lossless: bool, Q: int, keep?: int, strip?: bool}
*/
protected function getOptions(): array
{
$options = [
'lossless' => $this->quality === 100,
'Q' => $this->quality,
'keep' => $keep,
]);
];

return new EncodedImage($result, 'image/webp');
$strip = $this->strip || $this->driver()->config()->strip;

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

return $options;
}
}
9 changes: 7 additions & 2 deletions src/Modifiers/StripMetaModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
use Jcupitt\Vips\Config as VipsConfig;
use Jcupitt\Vips\Exception as VipsException;
use Jcupitt\Vips\ForeignKeep;
use Jcupitt\Vips\Image as VipsImage;
Expand All @@ -24,9 +25,13 @@ class StripMetaModifier implements ModifierInterface, SpecializedInterface
*/
public function apply(ImageInterface $image): ImageInterface
{
$buf = $image->core()->native()->tiffsave_buffer([
$options = VipsConfig::atLeast(8, 15) ? [
'keep' => ForeignKeep::ICC,
]);
] : [
'strip' => true,
];

$buf = $image->core()->native()->tiffsave_buffer($options);

$image->setExif(new Collection());
$image->core()->setNative(
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Encoders/WebpEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Intervention\Image\Drivers\Vips\Driver;
use Intervention\Image\Drivers\Vips\Encoders\WebpEncoder;
use Intervention\Image\Drivers\Vips\Tests\BaseTestCase;
use Intervention\Image\ImageManager;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(WebpEncoder::class)]
Expand All @@ -30,4 +31,15 @@ public function testEncodeAnimated(): void
$result = $encoder->encode($image);
$this->assertImageSize($result, $image->width(), $image->height());
}

public function testEncoderStripExifData(): void
{
$image = $this->readTestImage('exif.jpg');
$this->assertEquals('Oliver Vogel', $image->exif('IFD0.Artist'));
$encoder = new WebpEncoder(strip: true);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$image = ImageManager::withDriver(Driver::class)->read($result);
$this->assertNull($image->exif('IFD0.Artist'));
}
}

0 comments on commit 7c1bd64

Please sign in to comment.