Skip to content

Commit

Permalink
Add possibility to erase metadata except ICC profile
Browse files Browse the repository at this point in the history
  • Loading branch information
romainneutron committed Aug 6, 2013
1 parent 250baa7 commit 5d70795
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,10 @@
CHANGELOG
---------

* 0.3.0 (2013-xx-xx)

* Add possibility to erase metadata except ICC profile.

* 0.2.2 (2013-04-17)

* Add missing files
Expand Down
12 changes: 8 additions & 4 deletions lib/PHPExiftool/Writer.php
Expand Up @@ -53,6 +53,7 @@ class Writer
protected $modules;
protected $erase;
private $exiftool;
private $eraseProfile;

public function __construct(Exiftool $exiftool)
{
Expand All @@ -65,6 +66,7 @@ public function reset()
$this->mode = 0;
$this->modules = 0;
$this->erase = false;
$this->eraseProfile = false;

return $this;
}
Expand Down Expand Up @@ -131,11 +133,13 @@ public function hasModule($module)
/**
* If set to true, erase all metadatas before write
*
* @param Boolean $boolean
* @param Boolean $boolean Whether to erase metadata or not before writing.
* @param Boolean $maintainICCProfile Whether to maintain or not ICC Profile in case of erasing metadata.
*/
public function erase($boolean)
public function erase($boolean, $maintainICCProfile = false)
{
$this->erase = (boolean) $boolean;
$this->eraseProfile = !$maintainICCProfile;
}

/**
Expand Down Expand Up @@ -170,7 +174,7 @@ public function write($file, MetadataBag $metadatas, $destination = null)
* anything else.
*/
if ( ! $destination) {
$command .= ' -all:all= ' . $file . ' -execute';
$command .= ' -all:all= ' . ($this->eraseProfile ? '' : '--icc_profile:all ') . '' . $file . ' -execute';

/**
* If no destination, all commands will overwrite in place
Expand All @@ -183,7 +187,7 @@ public function write($file, MetadataBag $metadatas, $destination = null)
* If destination was specified, we start by creating the blank
* destination, we will write in it at next step
*/
$command .= ' -all:all= -o ' . $destination . ' ' . $file . ' -execute';
$command .= ' -all:all= ' . ($this->eraseProfile ? '' : '--icc_profile:all ') . '-o ' . $destination . ' ' . $file . ' -execute';

$file = $destination;
$destination = null;
Expand Down
Binary file added tests/files/pixelWithIcc.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 59 additions & 3 deletions tests/lib/PHPExiftool/Test/AbstractWriterTest.php
Expand Up @@ -14,13 +14,15 @@ abstract class AbstractWriterTest extends \PHPUnit_Framework_TestCase
*/
protected $object;
protected $in;
protected $inWithICC;
protected $inPlace;
protected $out;

protected function setUp()
{
$this->object = new Writer($this->getExiftool());
$this->in = __DIR__ . '/../../../files/ExifTool.jpg';
$this->inWithICC = __DIR__ . '/../../../files/pixelWithIcc.jpg';
$this->out = __DIR__ . '/../../../files/ExifTool_erased.jpg';
$this->inPlace = __DIR__ . '/../../../files/ExifToolCopied.jpg';
copy($this->in, $this->inPlace);
Expand Down Expand Up @@ -69,16 +71,16 @@ public function testSetModule()
* @covers PHPExiftool\Writer::write
* @covers PHPExiftool\Writer::erase
*/
public function testErase()
public function testEraseWithoutICC()
{
$uniqueId = 'UNI-QUE-ID';

$metadatas = new Driver\Metadata\MetadataBag();
$metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\UniqueDocumentID(), new Driver\Value\Mono($uniqueId)));
$metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPExif\ImageUniqueID(), new Driver\Value\Mono($uniqueId)));

$this->object->erase(true);
$changedFiles = $this->object->write($this->in, $metadatas, $this->out);
$this->object->erase(true, false);
$changedFiles = $this->object->write($this->inWithICC, $metadatas, $this->out);
$this->assertEquals(1, $changedFiles);

$reader = new Reader($this->getExiftool(), new RDFParser());
Expand Down Expand Up @@ -123,6 +125,60 @@ public function testErase()
}
}

public function testEraseWithICC()
{
$uniqueId = 'UNI-QUE-ID';

$metadatas = new Driver\Metadata\MetadataBag();
$metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\IPTC\UniqueDocumentID(), new Driver\Value\Mono($uniqueId)));
$metadatas->add(new Driver\Metadata\Metadata(new Driver\Tag\XMPExif\ImageUniqueID(), new Driver\Value\Mono($uniqueId)));

$this->object->erase(true, true);
$changedFiles = $this->object->write($this->inWithICC, $metadatas, $this->out);
$this->assertEquals(1, $changedFiles);

$reader = new Reader($this->getExiftool(), new RDFParser());
$this->assertGreaterThan(200, count($reader->files($this->in)->first()->getMetadatas()));

$reader = new Reader($this->getExiftool(), new RDFParser());
$this->assertGreaterThan(4, count($reader->files($this->out)->first()->getMetadatas()));

$acceptedMetas = array(
'Exiftool:\w+',
'System:\w+',
'File:\w+',
'Composite:\w+',
'IPTC:CodedCharacterSet',
'ICC-header:\w+',
'IPTC:EnvelopeRecordVersion',
'IPTC:UniqueDocumentID',
'IPTC:ApplicationRecordVersion',
'Photoshop:IPTCDigest',
'XMP-x:XMPToolkit',
'XMP-exif:ImageUniqueID',
'Adobe:DCTEncodeVersion',
'Adobe:APP14Flags0',
'Adobe:APP14Flags1',
'Adobe:ColorTransform',
);

foreach ($reader->files($this->out)->first()->getMetadatas() as $meta) {

$found = false;

foreach ($acceptedMetas as $accepted) {
if (preg_match('/' . $accepted . '/i', $meta->getTag()->getTagname())) {
$found = true;
break;
}
}

if ( ! $found) {
$this->fail(sprintf('Unexpected meta %s found', $meta->getTag()->getTagname()));
}
}
}

/**
* @covers PHPExiftool\Writer::write
*/
Expand Down

0 comments on commit 5d70795

Please sign in to comment.