This seems to be an issue that has been talked about in a number of places, however I found it very hard to find the correct solution, which is why I have documented it here.
Often as part of the build process for a webapp you’ll want to take original images and shrink them down to be the correct dimensions (either because they require certain dimensions to be accepted, such as icons, or because you want to save space by stripping out unnecessary data). For JPGs you can do this pretty easily like
convert orig.jpg -strip -resize 500x500 build/out.jpg
-strip removes any EXIF header information both anonymizing the image and saving potentially a few Kb of asset size.
This process is ‘stable’ because if you repeat it (within the same version of ImageMagick), the resulting file’s data will be identical. This means that you won’t get a new version of the built image in your (git) repository each time you run this command.
However recently when trying to do the same for PNGs (because I required transparency) I noticed that each time they were being built, git was committing a new version into the repository. This is bad news because it both grows the size of the repository by storing pointless identical versions of the file, and also makes it a lot harder tracking through history to see what changed because you have loads of PNG images being committed each time you do a build.
Looking at the output of
identify -verbose I could see that the part that was changing each time was below:
Properties: date:create: 2016-12-01T19:24:13+03:00 date:modify: 2016-12-01T19:24:13+03:00 png:tIME: 2016-12-01T16:24:13Z
So it appears that PNG format wants to store the update/create time in the image’s header itself. That was what was changing each time.
Searching on the internet I found a number of suggestions about how to strip these out with the
convert command, and I saw that the header changed a bit but I couldn’t find any that were also removing the ‘png:tIME’ element. Finally I managed to come up with the following flags which convert the image stably:
convert orig.png -strip -define png:exclude-chunks=date,time build/out.png
identify command still outputs the
date: property sections but these are now being taken from the create time (ctime) and modify time (mtime) of the file itself rather than from the header and so are not stored in version control.
You might be wondering why I don’t just create a lazy build system that only updates the asset if the mod time of the source asset is greater than that of the built asset – if I was doing this on a bigger project that would be the best way, but as this was just for a small project I wanted to do quickly I thought that doing this would be the easiest way!