Skip to content

Commit

Permalink
Do not write empty or duplicate metadata blocks (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Mar 18, 2023
1 parent 137d42d commit 059ec45
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Metadata/PngContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,25 @@ public function apply($inputStream, $outputStream, ImageMetadata $metadata, arra

if ('IDAT' === $type) {
fwrite($outputStream, $png);
fwrite($outputStream, $this->buildItxt('XML:com.adobe.xmp', $xmp));
fwrite($outputStream, $this->buildChunk('eXIf', $exif));
// Non-standard ImageMagick/exiftool/exiv2 format
fwrite($outputStream, $this->buildItxt('Raw profile type iptc', sprintf("\nIPTC profile\n%8d\n%s", \strlen($iptc), bin2hex($iptc))));

if ($xmp) {
fwrite($outputStream, $this->buildItxt('XML:com.adobe.xmp', $xmp));
}

if ($exif) {
fwrite($outputStream, $this->buildChunk('eXIf', $exif));
}

if ($iptc) {
// Non-standard ImageMagick/exiftool/exiv2 format
fwrite($outputStream, $this->buildItxt('Raw profile type iptc', sprintf("\nIPTC profile\n%8d\n%s", \strlen($iptc), bin2hex($iptc))));
}

// Copy the rest of the image
fwrite($outputStream, $marker);
stream_copy_to_stream($inputStream, $outputStream);

return;
}

fwrite($outputStream, $marker);
Expand Down
58 changes: 58 additions & 0 deletions tests/Metadata/MetadataReaderWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@

namespace Contao\Image\Tests\Metadata;

use Contao\Image\Metadata\ExifFormat;
use Contao\Image\Metadata\GifFormat;
use Contao\Image\Metadata\ImageMetadata;
use Contao\Image\Metadata\IptcFormat;
use Contao\Image\Metadata\MetadataReaderWriter;
use Contao\Image\Metadata\PngFormat;
use Contao\Image\Metadata\XmpFormat;
use Contao\Image\ResizeOptions;
use Imagine\Gd\Imagine as GdImagine;
use Imagine\Gmagick\Imagine as GmagickImagine;
use Imagine\Image\Box;
use Imagine\Imagick\Imagine as ImagickImagine;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;

Expand Down Expand Up @@ -101,6 +112,53 @@ public function getParse(): \Generator
}
}

/**
* @dataProvider getFormats
*/
public function testApplyDoesNotChangeForEmptyData(string $format): void
{
$supported = false;

foreach ([GdImagine::class, ImagickImagine::class, GmagickImagine::class] as $imagineClass) {
if (!($driverInfo = $imagineClass::getDriverInfo(false)) || !$driverInfo->isFormatSupported($format)) {
continue;
}

$supported = true;
$sourcePath = (new Filesystem())->tempnam(sys_get_temp_dir(), 'img');
$targetPath = (new Filesystem())->tempnam(sys_get_temp_dir(), 'img');
$imagine = new $imagineClass();

$imagine->create(new Box(100, 100))->save($sourcePath, ['format' => $format]);

(new MetadataReaderWriter())->applyCopyrightToFile(
$sourcePath,
$targetPath,
new ImageMetadata([
XmpFormat::NAME => [],
IptcFormat::NAME => [],
ExifFormat::NAME => [],
PngFormat::NAME => [],
GifFormat::NAME => [],
]),
(new ResizeOptions())->getPreserveCopyrightMetadata()
);

$this->assertFileEquals($sourcePath, $targetPath);

(new Filesystem())->remove([$sourcePath, $targetPath]);
}

if (!$supported) {
$this->markTestSkipped(sprintf('Format "%s" not supported on this system', $format));
}
}

public function getFormats(): \Generator
{
yield ['png', 'gif', 'jpg', 'webp'];
}

private function assertExpectedArrayRecursive(array $expected, array $actual): void
{
foreach ($expected as $key => $value) {
Expand Down

0 comments on commit 059ec45

Please sign in to comment.