when an animated gif image is resized sometimes it breaks the gif. Its still animated but messes up the images in the sequence.
In imageapi_imagemagick.module line 115
function imageapi_imagemagick_image_resize(&$image, $width, $height)

$image->ops[] = '-resize '. (int) $width .'x'. (int) $height .'!';

changed it to
if($image->info['mime_type'] == 'image/gif'){
$image->ops[] = '-coalesce -resize '. (int) $width .'x'. (int) $height .'!';
$image->ops[] = '-resize '. (int) $width .'x'. (int) $height .'!';

Adding the -coalesce function, now the animated gif image resize correctly


morbiD’s picture

I just ran into this issue and the ImageMagick "-coalesce" switch is indeed required for correctly resizing animated gifs without messing them up. The above fix corrects the issue.

However, the ImageMagick documentation says:

Using "-coalesce" on a single image, will do exact the same job as using "-flatten" with a "-background" color of 'None' or 'Transparency'. That is it will 'fill out' the canvas of the image with transparent pixels.

When dealing with a image consisting on multiple layers, "-coalesce" can be used to generate a 'Progressive Layering' of the image. But to do this we need to take a few precautions, to disable any 'GIF animation' handling by the operator.

Does that mean it shouldn't really be applied to all gif files? Is there any way to specifically detect animated gifs in the PHP logic or something?

crea’s picture


NaX’s picture

Thanks, this worked for me. The animations are now showing correctly.

I hacked the resize function as follows.

function imageapi_imagemagick_image_resize(&$image, $width, $height) {
  $extra = '';
  if($image->info['mime_type'] == 'image/gif') {
    $extra = '-coalesce ';
  $image->ops[] = $extra . '-resize '. (int) $width .'x'. (int) $height .'!';
  $image->info['width'] = $width;
  $image->info['height'] = $height;
  return TRUE;

If I had the time I would consider creating a gif safe imagecache resize action.

sun’s picture

Corresponding ImageMagick issue: #1802534: ImageMagick scale breaks animated gifs

dboulet’s picture

Title: Animated GIF image gets broken on resize with imagemagic toolkit » Animated GIF image gets broken on resize with ImageMagick toolkit
Issue summary: View changes