Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Last active March 12, 2023 13:12
Show Gist options
  • Save ivanhoe011/daf06192aadb21af24e2fa35ead9c360 to your computer and use it in GitHub Desktop.
Save ivanhoe011/daf06192aadb21af24e2fa35ead9c360 to your computer and use it in GitHub Desktop.
Proper way to remove all metadata from an image
<?php
$img = new Imagick($img);
// code by Max Eremin http://php.net/manual/en/imagick.stripimage.php#120380
// we need to keep the ICC profiles
$profiles = $img->getImageProfiles('icc', true);
// but remove everything else
$img->stripImage();
// re-attach the color profiles
if ( ! empty($profiles)) {
$img->profileImage('icc', $profiles['icc']);
}
@QTseb
Copy link

QTseb commented Jan 23, 2023

Dont know why, but half my images increase in size after stripping the meta data

@ivanhoe011
Copy link
Author

In file size, or dimensions?

If you can, post a link to one of the images so that we can inspect it closer. Also post the version of your php/imagemagick as this code snippet is very old, perhaps something changed recently...

@ivanhoe011
Copy link
Author

ivanhoe011 commented Mar 11, 2023

Just thought of something: This process is re-compressing the image, and the default quality is set pretty high, 92 IIRC, so that could be the reason why you're getting bigger images. It's best that you set the quality down to the actual quality of the original image or the value that you want:

$img->setImageCompressionQuality($realQuality);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment