We are using ImageMagick for resizing/thumbnailing JPGs to a specific size. The source file is loaded via HTTP. It’s working as expected, but from time to time some images are partially broken.
We already tried different software like GraphicsMagick or VIPS, but the problem is still there. It also only seems to happen if there are parallel processes. So the whole script is locked via sempahores, but it also does not help
We found multiple similar problems, but all without any solution: https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=22506
We also wonder, why it is the same behaviour in all these softwares. We also tried different PHP versions. It seems to happen more often on source images with a huge dimension/filesize.
Any idea what to do here?
2
Answers
After some additional investigation we discovered that indeed the sourceimage was already damaged. It was downloaded via a vpn connection which was not stable enough. Sometimes the download stopped, so the JPG was only half written.
I would guess the source image has been truncated for some reason. Perhaps something timed out during the download?
libvips is normally permissive, meaning that it’ll try to give you something, even if the input is damaged. You can make it strict with the
fail
flag (ie. fail on the first warning).For example:
I made a truncated jpg file, then ran thumbnail. It gave a warning, but did not fail. If I run:
Or in php:
Now there’s no output, and there’s an error code. I’m sure there’s an imagemagick equivalent, though I don’t know it.
There’s a downside: thumbnailing will now fail if there’s anything wrong with the image, and it might be something you don’t care about, like invalid resolution.