Search code examples
imagemagickimagemagick-convert

PNG-Preview of animated GIF with ImageMagick is distorted


I am creating preview images of all JPG/PNG/GIF that are uploaded to our server.

For this I use ImageMagick with:

convert -format jpg -quality 90 -strip -background white -flatten -alpha off +repage -resize '255x255>' ".$locationtmp." -write '".$preview."'");

The parameters added are to flatten the background (make it white instead of black).

However, when using the code to create a preview of an animated GIF, the result is distorted.

Example of a distorted result:

PNG distorted


I tried to add the +repage parameter but it did not help.

How to get a nice preview of the GIF as PNG? It could also be just the first frame.


Solution

  • In Imagemagick, if you want a preview image for all formats including gif and want just the first frame of the animation for the preview, then just add [0] to the filename, such as animation.gif[0]. This will take the first frame of the animation and the only frames of the other formats such as JPG and PNG that only allow one frame. The appended [0] does not hurt the other formats.

    So use

    image.suffix[0] in place of just image.suffix
    

    for generating a single image preview