开发者

Reducing GIF filesize using ImageMagick and PHP

I am writing some code using PHP and Imagick which gathers multiple images into one animated GIF using the following code:

<?php
    error_reporting(E_ALL);
    ini_set('display_errors',1);

    $a开发者_开发知识库nim = new Imagick();


    for($i = 0; $i<=36; $i++) {
        $bgImage = new Imagick('Background.gif');

        $imagick = new Imagick("sw_layers-$i.gif");

        $bgImage->setImageColorspace($imagick->getImageColorspace());
        $bgImage->compositeImage($imagick, imagick::DISPOSE_PREVIOUS,0,0);

        $draw = new ImagickDraw();

        /*** set the fill color ***/
        $draw->setFillColor( new ImagickPixel( "orange" ) );

        $draw->annotation( 10, 10, 'Hello world');

        $bgImage->drawImage( $draw );

        $anim->addImage($bgImage);
        $anim->setFormat("gif");
        $anim->setImageDispose(3);
        $anim->setImageCompression(imagick::COMPRESSION_JPEG);
        $anim->setImageCompressionQuality(50);
    }

    echo $anim->writeImages('Result.gif', true);
?>

The background image and "Hello world" are supposed to appear for every frame. As you can see i am joing the text image, the background image and the layer into a single frame. the layer image contains some kind of a drawing with a transparent background so my supplied background and text should appear in that case. However, the problem lies in the filesize of the animated gif "result.gif" which turns out to be around 3 MB for a 30 frames image.

The question is, How can i reduce the filesize without affecting the quality that much? i am open to answers including running a commandline tool on the system.

Thanks


try using

$anim->setImageFormat('gif');

instead of setFormat.

some examples can be found here:

http://uk.php.net/manual/en/function.imagick-setimageformat.php


Have you tried this, it compares frames and removes replicated data across frames?

/* optimize the image layers */
$im->optimizeImageLayers();

/* write the image back */
$im->writeImages("test_optimized.gif", true);

Source: Imagick OptimizeLayers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜