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

Do not write empty or duplicate metadata blocks #95

Merged
merged 1 commit into from
Mar 18, 2023
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
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