Preserve EXIF info in attachments (ImageMagick)

Barry13

Member
Hello,

When Xenforo calls ImageMagick (PECL version, fwiw), it calls the 'thumbnailImage' function instead of resizeImage (or adaptiveResizeImage).
(resizeImage is only used by Xenforo for Scaling Up)

This can be seen in the php file
\upload\library\XenForo\Image\ImageMagick\Pecl.php:
Code:
foreach ($this->_image AS $frame)
{
   if ($scaleUp)
   {
      $frame->resizeImage($width, $height, Imagick::FILTER_QUADRATIC, .5, true);
   }
   else if ($oldImagick)
   {
      $frame->thumbnailImage($width, $height, true);
   }
   else
   {
      $frame->thumbnailImage($width, $height, true, true);
   }
   $frame->setImagePage($width, $height, 0, 0);
}
I believe this would be the correct change:
Code:
foreach ($this->_image AS $frame)
{
   if ($scaleUp)
   {
       $frame->resizeImage($width, $height, Imagick::FILTER_QUADRATIC, .5, true);
   }
   else if ($oldImagick)
   {
       $frame->resizeImage($width, $height, Imagick::FILTER_QUADRATIC, .5, true);  // keep EXIF
   }
   else
   {
       $frame->adaptiveResizeImage($width, $height, true);   // higher-quality scaling + EXIF retained
   }
   $frame->setImagePage($width, $height, 0, 0);
}
And make sure ImageMagick version >= 6.2.9 is installed.


references:
https://php.net/manual/en/imagick.adaptiveresizeimage.php (req. ImageMagick version >= 6.2.9)
PHP: Imagick::resizeImage - Manual
https://stackoverflow.com/questions/22534752/imagemagick-preserve-exif-data said:
You should not use the thumbnailImage image method if you want to preserve exif data... You should use the method resizeImage instead.
ImageMagick Preserve Exif Data
Thank you,
Barry
 

Amin Sabet

Well-known member
@Barry13 - I noticed that XenForo strips EXIF even when the image is not resized. Is that compatible with your understanding of what the issue is currently?
 

Chris D

XenForo developer
Staff member
XF doesn't deliberately strip anything, but we currently don't make any effort to retain anything, either.

So if EXIF disappears, it's simply because the image has been manipulated in some way. It doesn't have to be resizing, it can also be rotation. EXIF data stores the orientation of the camera when the photo is taken, during upload we look at that and rotate the image the correct way. If we didn't, then photos from certain cameras and devices would appear the wrong way round. It's likely that process which removes the EXIF data in some cases.
 

Amin Sabet

Well-known member
XF doesn't deliberately strip anything, but we currently don't make any effort to retain anything, either.

So if EXIF disappears, it's simply because the image has been manipulated in some way. It doesn't have to be resizing, it can also be rotation. EXIF data stores the orientation of the camera when the photo is taken, during upload we look at that and rotate the image the correct way. If we didn't, then photos from certain cameras and devices would appear the wrong way round. It's likely that process which removes the EXIF data in some cases.
Thanks, Chris. I can confirm that on my board XenForo does not strip EXIF if there is no manipulation. However, our members strongly want to retain EXIF even when there is manipulation.

I tried the edits @Barry13 suggested above, but I'm not seeing any difference on my test board with those edits made. EXIF is still maintained without resizing but stripped with resizing (I didn't try rotating).
 

Chris D

XenForo developer
Staff member
Those changes would only take effect if you are using Imagick which is set under Attachment Options.
 

Chris D

XenForo developer
Staff member
As far as I can tell, resizeImage, in recent versions of ImageMagick at least, destroys the EXIF.

So there may not actually be a solution for this, at all.
 

Amin Sabet

Well-known member
I've been researching this in the ImageMagick forum, and they say that -resize does not remoove EXIF.

In looking at pecl.php with the edits @Barry13 provided, I'm wondering if this part in orange is the reason EXIF is being stripped on resized images:

Rich (BB code):
/**
    * Outputs the image.
    *
    * @see XenForo_Image_Abstract::output()
    */
   public function output($outputType, $outputFile = null, $quality = 85)
   {
       $this->_image->stripImage();
 

Chris D

XenForo developer
Staff member
Actually, yes, I believe that does strip EXIF info. Comment out that line and perhaps the suggested edits may work.
 

Amin Sabet

Well-known member
I'm trying to figure out how to add a slight unsharp mask after resizing. Has anyone worked that part out?
 

Amin Sabet

Well-known member
Got it. The attached one does light sharpening after resize. Works really well. Best image quality yet IMO.
 

Attachments

Amin Sabet

Well-known member
@Chris D - the edited Pecl.php file above was working for a long time to preserve EXIF but recently seems to have stopped working. Was there a change in one of the recent Xenforo 1.x updates that would have stopped this from working?

My sites are photography forums, and it is very important to the members that we preserve EXIF (which often includes copyright information) in their images. Thanks!
 

Amin Sabet

Well-known member
Nevermind, for some reason when I transferred the edited file before, it didn't overwrite the one on the server.

I re-did it now, and all is well.

Would be great if the edited file could be included in core so that Xenforo doesn't strip EXIF on image resize. Also, the edited file does a higher quality image resize!
 
Top