duanbipu7601 2016-03-06 12:23
浏览 97
已采纳

如何停止Image Magick stripImage()删除分辨率数据

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条回答 默认 最新

  • douzuqin3467 2016-03-06 13:02
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图