2

I'm trying to remove existing EXIF tags and then apply a copyright string using this essential code below:

$im = new Imagick($file);
$im->stripImage();

// Embed new EXIF data
$im->setImageProperty('EXIF:ImageDescription', $copy_string);
$im->setImageProperty('EXIF:Copyright', $copy_string);
//$im->setImageProperty('Exif:ImageDescription', $copy_string);
//$im->setImageProperty('Exif:Copyright', $copy_string);
//$im->setImageProperty('ImageDescription', $copy_string);
//$im->setImageProperty('Copyright', $copy_string);

$im->writeImage($file);
$im->clear();
$im->destroy();

I'm having trouble finding example usage. The manual has CLI property strings here. None of the lines under "Embed" are having any effect. We're going to stay with Imagick for this solution. Thanks.

Drakes
  • 23,254
  • 3
  • 51
  • 94
  • Did you try `Exif:Copyright`? It's like that in the docs (not all uppercase). – Charlotte Dunois Jun 13 '16 at 13:50
  • @CharlotteDunois Just tried it now. No go. Thanks anyway. – Drakes Jun 13 '16 at 13:57
  • 1
    I don't believe IM can _write_ EXIF profile information. – emcconville Jun 13 '16 at 14:31
  • emcconville is correct https://github.com/ImageMagick/ImageMagick/issues/55#issuecomment-157114261 "Exif values are not persisted. We only read them and you can 'change' them but we don't update the exif profile when the file is being saved." - anyone can make a note of this in the manual, by clicking the 'edit' button. – Danack Jun 13 '16 at 16:24
  • @Danack Ok good, now I can get off Imagick because it's been explained it can't save Exif data. Thanks for that bit. – Drakes Jun 13 '16 at 21:10

1 Answers1

3

As already mentioned, Imagemagick does not allow you to write the EXIF data so there is no "correct" answer. You may, or may not like the idea I mentioned here as a work-around. Essentially, you could create a one-off image as a "template" for the EXIF data you want to put in your images (I'll explain how later on) and then use this as the basis for your actual images and composite your image data on top of the image with the correct EXIF data and, as Danack says, ImageMagick will persist the EXIF data.

So, in concrete terms:

   // Open the copyright image with the correct EXIF data
   $cr = new Imagick('copyright.jpg');

   // Open the image to alter and get its size
   $file = 'test.jpg';
   $im = new Imagick($file);
   $d = $im->getImageGeometry(); 
   $w = $d['width']; 
   $h = $d['height']; 

   // Resize the copyright and composite the image over the top
   $cr->resizeImage($w,$h,imagick::FILTER_POINT,0,0);
   $cr->compositeImage($im,imagick::COMPOSITE_SRCOVER ,0,0);
   $cr->writeImage($file);

So, I made the copyright image with the EXIF data I want as a one-off operation - albeit using exiftool but you only need to create that image once - you don't need to keep using exiftool or have it as part of your toolchain, rather, you just keep the template it produces:

# Make template copyright file
convert -size 1x1 xc:black copyright.jpg

# Put some EXIOF data in the template
exiftool -copyright="2015 Copyright Captain Hackalot" copyright.jpg
Community
  • 1
  • 1
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I gave this a try and it partially worked. With a very small 1x1 jpeg image with the desired copyright info, this 'copyright.jpg' image came out to about 3k in size which is fine. When I used the above code, the written files are much larger. (e,g, 164kB --> 192kB, 26kB --> 49kB). How can the Exif data be added with minimal file size increase (for the metadata), and not change the compression or filters? – Drakes Jun 14 '16 at 15:39
  • Try `getImageCompressionQuality()` of your source image and then set the same quality for the output image with `setImageCompressionQuality()` – Mark Setchell Jun 14 '16 at 15:48