想象一下动画GIF的透明度

Recently, I tried a basic jQuery "marching ants" effect, based on http://www.sunpig.com/martin/archives/2010/02/10/marching-ants-in-css.html

The jQuery code (it's not quite a plugin) is on JSFiddle here: http://jsfiddle.net/mwvdlee/xTZuZ/ and is fine.

The animated GIFs supplied on that site are of a fixed size, and I would like to create a simple animated GIF generator to create multiple sizes, colors and styles of the same theme. In order to do so I hacked up the following PHP code:

// Marching ants

$width      = 16;
$height     = 16;
$horizontal = true;
$bgcolor    = '#00ff0000';
$fgcolor    = '#0000ffff';

$animation  = new Imagick();

$frame      = new Imagick();
$frame->newimage($width, $height, $bgcolor);
$frame->setimageformat('gif');
$frame->setimagedelay(10);  // 1/100th seconds
$frame->setimagedispose(2);

for ($f = 0; $f < $width; ++$f) {
    $rows = $frame->getPixelIterator();
    foreach($rows as $y => $columns) {
        foreach($columns as $x => $pixel) {
            $color = $bgcolor;

            // top half
            if ($y < $height * .5) {
                if (($x + $f) % $width < $width * .5) {
                    $color = $fgcolor;
                }
            } else {
                if (($width + $x - $f) % $width >= $width * .5) {
                    $color = $fgcolor;
                }
            }

            $pixel->setColor($color);

            $rows->syncIterator();
        }
    }

    $animation->addimage($frame);
}

$animation->setformat('gif');
$animation->setimageformat('gif');
$animation = $animation->deconstructimages();
$gif = $animation->getimagesblob();

if (!error_get_last()) {
    header('Content-Type: image/gif');
    echo $gif;
} else {
    var_dump(error_get_last());
}

This uses the nearly undocumented Imagick class, rather than GD since I need to use Imagick for other code and would rather stick with one framework for all my coding.

The problem is that it seems to chop off the sides during animation. I suspect it has something to do with the GIF disposal method. Making the $bgcolor opaque and using disposal method 0 or 1 also fixes it, but now the GIF has no transparency.

Does anybody know how to fix this code as to get an animated GIF with transparency and without the unwanted clipping at the sides?