Optimize an image once and avoid doing it again

拈花ヽ惹草 提交于 2019-12-13 02:49:18

问题


I have a large respository of images, mostly JPEG, which I'd like to optimize using a library like ImageMagick or a Linux CLI tool like jpegtran (as covered in JPG File Size Optimization - PHP, ImageMagick, & Google's Page Speed), but I don't want to have to track which ones have been optimized already and I don't want to re-optimize every one again later. Is there some sort of flag I could easily add to the file that would make it easy to detect and skip the optimization? Preferably one that would stay with the file when backed up to other filesystems?

E.g.: a small piece of exif data, a filesystem flag, some harmless null bytes added at the end of the file, a tool that is already intelligent enough to do this itself, etc..


回答1:


You could use "extended attributes" which are metadata and stored in the filesystem. Read and write them with xattr:

# Read existing attributes
xattr -l image.png

# Set an optimised flag of your own invention/description
xattr -w optimised true image.png

# Read attributes again
xattr -l image.png
optimised: true

The presence of an extended attribute can be detected in a long listing - it is the @ sign after the permissions:

-rw-r--r--@ 1 mark  staff  275 29 May 07:54 image.png

As you allude in your comments, make sure that any backup programs you use honour the attributes before committing to it as a strategy. FAT-32 filesystems are notoriously poor at this sort of thing - though tar file or similar may survive a trip to Windows-land and back.

As an alternative, just set a comment in the EXIF header - I have already covered that in this related answer...



来源:https://stackoverflow.com/questions/49569562/optimize-an-image-once-and-avoid-doing-it-again

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