Skip to content
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

feature/strip encoder parameter #1421

Merged
merged 14 commits into from
Jan 18, 2025
Merged
3 changes: 3 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ class Config
* @param bool $autoOrientation
* @param bool $decodeAnimation
* @param mixed $blendingColor
* @param bool $strip
* @return void
*/
public function __construct(
public bool $autoOrientation = true,
public bool $decodeAnimation = true,
public mixed $blendingColor = 'ffffff',
public bool $strip = false,
) {
//
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Imagick/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Intervention\Image\Drivers\Imagick\Encoders;

use Imagick;
use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\AvifEncoder as GenericAvifEncoder;
use Intervention\Image\Interfaces\EncodedImageInterface;
Expand All @@ -18,6 +19,11 @@ public function encode(ImageInterface $image): EncodedImageInterface
$format = 'AVIF';
$compression = Imagick::COMPRESSION_ZIP;

// strip meta data
if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) {
$image->modify(new StripMetaModifier());
}

$imagick = $image->core()->native();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Imagick/Encoders/HeicEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Drivers\Imagick\Encoders;

use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\HeicEncoder as GenericHeicEncoder;
use Intervention\Image\Interfaces\EncodedImageInterface;
Expand All @@ -16,6 +17,11 @@ public function encode(ImageInterface $image): EncodedImageInterface
{
$format = 'HEIC';

// strip meta data
if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) {
$image->modify(new StripMetaModifier());
}

$imagick = $image->core()->native();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Imagick/Encoders/Jpeg2000Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Intervention\Image\Drivers\Imagick\Encoders;

use Imagick;
use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\Jpeg2000Encoder as GenericJpeg2000Encoder;
use Intervention\Image\Interfaces\ImageInterface;
Expand All @@ -18,6 +19,11 @@ public function encode(ImageInterface $image): EncodedImageInterface
$format = 'JP2';
$compression = Imagick::COMPRESSION_JPEG;

// strip meta data
if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) {
$image->modify(new StripMetaModifier());
}

$imagick = $image->core()->native();
$imagick->setImageBackgroundColor('white');
$imagick->setBackgroundColor('white');
Expand Down
7 changes: 7 additions & 0 deletions src/Drivers/Imagick/Encoders/JpegEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Intervention\Image\Drivers\Imagick\Encoders;

use Imagick;
use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\JpegEncoder as GenericJpegEncoder;
use Intervention\Image\Interfaces\EncodedImageInterface;
Expand All @@ -30,6 +31,12 @@ public function encode(ImageInterface $image): EncodedImageInterface
// possible full transparent colors as black
$background->setColorValue(Imagick::COLOR_ALPHA, 1);

// strip meta data
if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) {
$image->modify(new StripMetaModifier());
}

/** @var Imagick $imagick */
$imagick = $image->core()->native();
$imagick->setImageBackgroundColor($background);
$imagick->setBackgroundColor($background);
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Imagick/Encoders/TiffEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image\Drivers\Imagick\Encoders;

use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\TiffEncoder as GenericTiffEncoder;
use Intervention\Image\Interfaces\ImageInterface;
Expand All @@ -16,6 +17,11 @@ public function encode(ImageInterface $image): EncodedImageInterface
{
$format = 'TIFF';

// strip meta data
if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) {
$image->modify(new StripMetaModifier());
}

$imagick = $image->core()->native();
$imagick->setFormat($format);
$imagick->setImageFormat($format);
Expand Down
6 changes: 6 additions & 0 deletions src/Drivers/Imagick/Encoders/WebpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Imagick;
use ImagickPixel;
use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\WebpEncoder as GenericWebpEncoder;
use Intervention\Image\Interfaces\EncodedImageInterface;
Expand All @@ -19,6 +20,11 @@ public function encode(ImageInterface $image): EncodedImageInterface
$format = 'WEBP';
$compression = Imagick::COMPRESSION_ZIP;

// strip meta data
if ($this->strip || (is_null($this->strip) && $this->driver()->config()->strip)) {
$image->modify(new StripMetaModifier());
}

$imagick = $image->core()->native();
$imagick->setImageBackgroundColor(new ImagickPixel('transparent'));

Expand Down
34 changes: 34 additions & 0 deletions src/Drivers/Imagick/Modifiers/StripMetaModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Imagick\Modifiers;

use Intervention\Image\Collection;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SpecializedInterface;

class StripMetaModifier implements ModifierInterface, SpecializedInterface
{
/**
* {@inheritdoc}
*
* @see Intervention\Image\Interfaces\ModifierInterface::apply()
*/
public function apply(ImageInterface $image): ImageInterface
{
// preserve icc profiles
$profiles = $image->core()->native()->getImageProfiles('icc');

// remove meta data
$image->core()->native()->stripImage();
$image->setExif(new Collection());

if ($profiles !== []) {
// re-apply icc profiles
$image->core()->native()->profileImage("icc", $profiles['icc']);
}
return $image;
}
}
8 changes: 6 additions & 2 deletions src/Encoders/AvifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class AvifEncoder extends SpecializableEncoder
* Create new encoder object
*
* @param int $quality
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(public int $quality = self::DEFAULT_QUALITY)
{
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}
1 change: 1 addition & 0 deletions src/Encoders/BmpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ class BmpEncoder extends SpecializableEncoder
{
public function __construct()
{
//
}
}
1 change: 1 addition & 0 deletions src/Encoders/GifEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ class GifEncoder extends SpecializableEncoder
*/
public function __construct(public bool $interlaced = false)
{
//
}
}
8 changes: 6 additions & 2 deletions src/Encoders/HeicEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class HeicEncoder extends SpecializableEncoder
* Create new encoder object
*
* @param int $quality
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(public int $quality = self::DEFAULT_QUALITY)
{
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}
8 changes: 6 additions & 2 deletions src/Encoders/Jpeg2000Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class Jpeg2000Encoder extends SpecializableEncoder
* Create new encoder object
*
* @param int $quality
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(public int $quality = self::DEFAULT_QUALITY)
{
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}
5 changes: 4 additions & 1 deletion src/Encoders/JpegEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class JpegEncoder extends SpecializableEncoder
*
* @param int $quality
* @param bool $progressive
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public bool $progressive = false
public bool $progressive = false,
public ?bool $strip = null
) {
//
}
}
1 change: 1 addition & 0 deletions src/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ class PngEncoder extends SpecializableEncoder
*/
public function __construct(public bool $interlaced = false, public bool $indexed = false)
{
//
}
}
8 changes: 6 additions & 2 deletions src/Encoders/TiffEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class TiffEncoder extends SpecializableEncoder
* Create new encoder object
*
* @param int $quality
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(public int $quality = self::DEFAULT_QUALITY)
{
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
//
}
}
7 changes: 5 additions & 2 deletions src/Encoders/WebpEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ class WebpEncoder extends SpecializableEncoder
* Create new encoder object
*
* @param int $quality
* @param null|bool $strip Strip EXIF metadata
* @return void
*/
public function __construct(public int $quality = self::DEFAULT_QUALITY)
{
public function __construct(
public int $quality = self::DEFAULT_QUALITY,
public ?bool $strip = null
) {
}
}
10 changes: 10 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ public function testConstructor(): void
autoOrientation: false,
decodeAnimation: false,
blendingColor: 'f00',
strip: true,
);
$this->assertInstanceOf(Config::class, $config);

$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertTrue($config->strip);
$this->assertEquals('f00', $config->blendingColor);
}

Expand All @@ -37,12 +39,14 @@ public function testGetSetOptions(): void
$config = new Config();
$this->assertTrue($config->autoOrientation);
$this->assertTrue($config->decodeAnimation);
$this->assertFalse($config->strip);
$this->assertEquals('ffffff', $config->blendingColor);

$result = $config->setOptions(
autoOrientation: false,
decodeAnimation: false,
blendingColor: 'f00',
strip: true,
);

$this->assertFalse($config->autoOrientation);
Expand All @@ -51,16 +55,19 @@ public function testGetSetOptions(): void

$this->assertFalse($result->autoOrientation);
$this->assertFalse($result->decodeAnimation);
$this->assertTrue($result->strip);
$this->assertEquals('f00', $result->blendingColor);

$result = $config->setOptions(blendingColor: '000');

$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertTrue($config->strip);
$this->assertEquals('000', $config->blendingColor);

$this->assertFalse($result->autoOrientation);
$this->assertFalse($result->decodeAnimation);
$this->assertTrue($result->strip);
$this->assertEquals('000', $result->blendingColor);
}

Expand All @@ -71,13 +78,16 @@ public function testSetOptionsWithArray(): void
'autoOrientation' => false,
'decodeAnimation' => false,
'blendingColor' => 'f00',
'strip' => true,
]);

$this->assertFalse($config->autoOrientation);
$this->assertFalse($config->decodeAnimation);
$this->assertTrue($config->strip);
$this->assertEquals('f00', $config->blendingColor);
$this->assertFalse($result->autoOrientation);
$this->assertFalse($result->decodeAnimation);
$this->assertTrue($result->strip);
$this->assertEquals('f00', $result->blendingColor);
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Drivers/Imagick/Encoders/AvifEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\Drivers\Imagick\Encoders\AvifEncoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
Expand All @@ -17,6 +18,7 @@ public function testEncode(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new AvifEncoder(10);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$this->assertMediaType('image/avif', $result);
$this->assertEquals('image/avif', $result->mimetype());
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Drivers/Imagick/Encoders/HeicEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\Drivers\Imagick\Encoders\HeicEncoder;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
Expand All @@ -17,6 +18,7 @@ public function testEncode(): void
{
$image = $this->createTestImage(3, 2);
$encoder = new HeicEncoder(75);
$encoder->setDriver(new Driver());
$result = $encoder->encode($image);
$this->assertMediaType('image/heic', $result);
$this->assertEquals('image/heic', $result->mimetype());
Expand Down
Loading