How do I stop Image Magick stripImage() from removing resolution data

风流意气都作罢 提交于 2019-12-08 06:05:22

问题


I'm uploading images to my website and optimising them by removing EXIF data using Image Magick's stripImage() function.

$img = new Imagick($image);
$img->stripImage();
$img->writeImage($image);
$img->destroy();

It works quite well, I get reduced file sizes as expected. However, if I open the image in Photoshop, photoshop reads the image as having a resolution of 1 Pixel per Inch.

It seems that stripImage is removing the EXIF data that photoshop uses to determine resolution.

How do I prevent this behaviour while stripping everything else?


回答1:


The short answer is that I don't think you can selectively remove parts of the EXIF data with ImageMagick, so you will probably need to extract the original density, clear the EXIF, then put back whatever Photoshop needs... not sure though, if Photoshop uses the standard density in a JPEG header or a value from the EXIF data.

Anyway, to work out the answer, you can get all density type settings in an image with EXIFtool like this:

exiftool "-*resolution*"  image.jpg
X Resolution                    : 72
Y Resolution                    : 72
Resolution Unit                 : inches

Exiftool is described here. Or, as you know, and love ImageMagick, you can use identify like this:

identify -verbose IMG_3942.JPG | grep -i reso
  Resolution: 72x72
    exif:ResolutionUnit: 2
    exif:thumbnail:ResolutionUnit: 2
    exif:thumbnail:XResolution: 72/1
    exif:thumbnail:YResolution: 72/1
    exif:XResolution: 72/1
    exif:YResolution: 72/1

You can also set the density with ImageMagick after you have stripped the EXIF data like this:

# Strip EXIF then add in resolution
convert IMG_3942.JPG -strip -density 180x180 180.jpg

# Check what happened
exiftool "-*resolution*"  180.JPG
Resolution Unit                 : inches
X Resolution                    : 180
Y Resolution                    : 180

You can also modify the EXIF data with libexif if you look here.



来源:https://stackoverflow.com/questions/35826775/how-do-i-stop-image-magick-stripimage-from-removing-resolution-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!