skip to Main Content

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?

Example 1 Example 2 Example 3

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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:

    $ head -c 10000 shark.jpg > truncated.jpg
    $ vipsthumbnail truncated.jpg
    (vipsthumbnail:9391): VIPS-WARNING **: 11:24:50.439: read gave 2 warnings
    (vipsthumbnail:9391): VIPS-WARNING **: 11:24:50.439: VipsJpeg: Premature end of JPEG file
    $ echo $?
    0
    

    I made a truncated jpg file, then ran thumbnail. It gave a warning, but did not fail. If I run:

    $ vipsthumbnail truncated.jpg[fail]
    VipsJpeg: Premature end of input file
    $ echo $?
    1
    

    Or in php:

    $thumb = VipsImage::thumbnail('truncated.jpg[fail]', 128);
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search