Download & Extend

Animated GIF image gets broken on resize with imagemagic toolkit

Project:ImageAPI
Version:6.x-1.6
Component:ImageAPI Imagick
Category:bug report
Priority:normal
Assigned:Unassigned
Status:needs review
Issue tags:animated gif, imagemagick, resize

Issue Summary

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)

Was
$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 .'!';
}else{
$image->ops[] = '-resize '. (int) $width .'x'. (int) $height .'!';
}

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

Comments

#1

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?

#2

Subscribing.

#3

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

I hacked the resize function as follows.

<?php
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.

#4

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