想象一下动画GIF的透明度

时间:2013-09-28 16:14:57

标签: php imagick animated-gif dispose

最近,我尝试了基于http://www.sunpig.com/martin/archives/2010/02/10/marching-ants-in-css.html的基本jQuery“行军蚂蚁”效果

jQuery代码(它不是一个插件)在JSFiddle上:http://jsfiddle.net/mwvdlee/xTZuZ/并且很好。

该网站上提供的动画GIF具有固定的大小,我想创建一个简单的动画GIF生成器来创建同一主题的多个大小,颜色和样式。为此,我破解了以下PHP代码:

// 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());
}

这使用了几乎没有文档的Imagick类,而不是GD,因为我需要将Imagick用于其他代码,并且宁愿坚持使用一个框架来进行所有编码。

问题是它似乎在动画期间切断了两侧。我怀疑它与GIF处理方法有关。使$bgcolor不透明并使用处理方法0或1也可以修复它,但现在GIF没有透明度。

是否有人知道如何修复此代码,以获得具有透明度的动画GIF,并且两侧没有不必要的剪裁?

0 个答案:

没有答案
相关问题