mirror of
https://github.com/Intervention/image.git
synced 2025-08-22 21:42:53 +02:00
feature/strip encoder parameter (#1421)
Add config option and encoder parameter to strip meta data in the encoding process. --------- Co-authored-by: Thomas <thomas@sctr.net>
This commit is contained in:
@@ -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,
|
||||
) {
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
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;
|
||||
@@ -18,6 +19,11 @@ class AvifEncoder extends GenericAvifEncoder implements SpecializedInterface
|
||||
$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);
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
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;
|
||||
@@ -16,6 +17,11 @@ class HeicEncoder extends GenericHeicEncoder implements SpecializedInterface
|
||||
{
|
||||
$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);
|
||||
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
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;
|
||||
@@ -18,6 +19,11 @@ class Jpeg2000Encoder extends GenericJpeg2000Encoder implements SpecializedInter
|
||||
$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');
|
||||
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
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;
|
||||
@@ -30,6 +31,12 @@ class JpegEncoder extends GenericJpegEncoder implements SpecializedInterface
|
||||
// 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);
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
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;
|
||||
@@ -16,6 +17,11 @@ class TiffEncoder extends GenericTiffEncoder implements SpecializedInterface
|
||||
{
|
||||
$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);
|
||||
|
@@ -6,6 +6,7 @@ namespace Intervention\Image\Drivers\Imagick\Encoders;
|
||||
|
||||
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;
|
||||
@@ -19,6 +20,11 @@ class WebpEncoder extends GenericWebpEncoder implements SpecializedInterface
|
||||
$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'));
|
||||
|
||||
|
34
src/Drivers/Imagick/Modifiers/StripMetaModifier.php
Normal file
34
src/Drivers/Imagick/Modifiers/StripMetaModifier.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
@@ -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
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -10,5 +10,6 @@ class BmpEncoder extends SpecializableEncoder
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -16,5 +16,6 @@ class GifEncoder extends SpecializableEncoder
|
||||
*/
|
||||
public function __construct(public bool $interlaced = false)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -17,5 +17,6 @@ class PngEncoder extends SpecializableEncoder
|
||||
*/
|
||||
public function __construct(public bool $interlaced = false, public bool $indexed = false)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
) {
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@@ -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
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
@@ -24,11 +24,13 @@ final class ConfigTest extends BaseTestCase
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -37,12 +39,14 @@ final class ConfigTest extends BaseTestCase
|
||||
$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);
|
||||
@@ -51,16 +55,19 @@ final class ConfigTest extends BaseTestCase
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
@@ -71,13 +78,16 @@ final class ConfigTest extends BaseTestCase
|
||||
'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);
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
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;
|
||||
@@ -17,6 +18,7 @@ final class AvifEncoderTest extends ImagickTestCase
|
||||
{
|
||||
$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());
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
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;
|
||||
@@ -17,6 +18,7 @@ final class HeicEncoderTest extends ImagickTestCase
|
||||
{
|
||||
$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());
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders;
|
||||
|
||||
use Intervention\Image\Drivers\Imagick\Driver;
|
||||
use Intervention\Image\Drivers\Imagick\Encoders\Jpeg2000Encoder;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||
@@ -17,6 +18,7 @@ final class Jpeg2000EncoderTest extends ImagickTestCase
|
||||
{
|
||||
$image = $this->createTestImage(3, 2);
|
||||
$encoder = new Jpeg2000Encoder(75);
|
||||
$encoder->setDriver(new Driver());
|
||||
$result = $encoder->encode($image);
|
||||
$this->assertMediaType('image/jp2', $result);
|
||||
$this->assertEquals('image/jp2', $result->mimetype());
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders;
|
||||
|
||||
use Intervention\Image\Drivers\Imagick\Decoders\FilePointerImageDecoder;
|
||||
use Intervention\Image\Drivers\Imagick\Driver;
|
||||
use Intervention\Image\Drivers\Imagick\Encoders\JpegEncoder;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
@@ -37,4 +38,34 @@ final class JpegEncoderTest extends ImagickTestCase
|
||||
$this->assertEquals('image/jpeg', $result->mimetype());
|
||||
$this->assertTrue($this->isProgressiveJpeg($result));
|
||||
}
|
||||
|
||||
public function testEncodeStripExif(): void
|
||||
{
|
||||
$image = $this->readTestImage('exif.jpg');
|
||||
$this->assertEquals('Oliver Vogel', $image->exif('IFD0.Artist'));
|
||||
|
||||
$encoder = new JpegEncoder(strip: true);
|
||||
$encoder->setDriver(new Driver());
|
||||
$result = $encoder->encode($image);
|
||||
$this->assertMediaType('image/jpeg', $result);
|
||||
$this->assertEquals('image/jpeg', $result->mimetype());
|
||||
|
||||
$this->assertEmpty(exif_read_data($result->toFilePointer())['IFD0.Artist'] ?? null);
|
||||
}
|
||||
|
||||
public function testEncodeStripExifKeepICCProfiles(): void
|
||||
{
|
||||
$image = $this->readTestImage('cmyk.jpg');
|
||||
$this->assertNotEmpty($image->core()->native()->getImageProfiles('icc'));
|
||||
|
||||
$encoder = new JpegEncoder(strip: true);
|
||||
$encoder->setDriver(new Driver());
|
||||
$result = $encoder->encode($image);
|
||||
|
||||
$decoder = new FilePointerImageDecoder();
|
||||
$decoder->setDriver(new Driver());
|
||||
|
||||
$image = $decoder->decode($result->toFilePointer());
|
||||
$this->assertNotEmpty($image->core()->native()->getImageProfiles('icc'));
|
||||
}
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders;
|
||||
|
||||
use Intervention\Image\Drivers\Imagick\Driver;
|
||||
use Intervention\Image\Drivers\Imagick\Encoders\TiffEncoder;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||
@@ -17,6 +18,7 @@ final class TiffEncoderTest extends ImagickTestCase
|
||||
{
|
||||
$image = $this->createTestImage(3, 2);
|
||||
$encoder = new TiffEncoder();
|
||||
$encoder->setDriver(new Driver());
|
||||
$result = $encoder->encode($image);
|
||||
$this->assertMediaType('image/tiff', $result);
|
||||
$this->assertEquals('image/tiff', $result->mimetype());
|
||||
|
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Encoders;
|
||||
|
||||
use Intervention\Image\Drivers\Imagick\Driver;
|
||||
use Intervention\Image\Drivers\Imagick\Encoders\WebpEncoder;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||
@@ -17,6 +18,7 @@ final class WebpEncoderTest extends ImagickTestCase
|
||||
{
|
||||
$image = $this->createTestImage(3, 2);
|
||||
$encoder = new WebpEncoder(75);
|
||||
$encoder->setDriver(new Driver());
|
||||
$result = $encoder->encode($image);
|
||||
$this->assertMediaType('image/webp', $result);
|
||||
$this->assertEquals('image/webp', $result->mimetype());
|
||||
|
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Modifiers;
|
||||
|
||||
use Intervention\Image\Drivers\Imagick\Modifiers\StripMetaModifier;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
|
||||
use Intervention\Image\Tests\ImagickTestCase;
|
||||
|
||||
#[RequiresPhpExtension('imagick')]
|
||||
#[CoversClass(StripMetaModifier::class)]
|
||||
final class StripMetaModifierTest extends ImagickTestCase
|
||||
{
|
||||
public function testApply(): void
|
||||
{
|
||||
$image = $this->readTestImage('exif.jpg');
|
||||
$this->assertEquals('Oliver Vogel', $image->exif('IFD0.Artist'));
|
||||
$image->modify(new StripMetaModifier());
|
||||
$this->assertNull($image->exif('IFD0.Artist'));
|
||||
$result = $image->toJpeg();
|
||||
$this->assertEmpty(exif_read_data($result->toFilePointer())['IFD0.Artist'] ?? null);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user