ImageMagick动画GIF压缩

时间:2019-01-29 13:12:34

标签: php imagemagick

我有以下代码从图像列表中生成动画GIF。它工作正常,但我想对其进行压缩。我尝试了许多不同的策略,但结果大小总是完全相同。

我在做什么错了?

<?php

namespace AppBundle\Service;

class GifCreator
{
    public function createGifFromImages(array $images)
    {
        // https://stackoverflow.com/a/10095594
        $gif = new \Imagick();
        $gif->setFormat("gif");

        // adds all the images
        foreach ($images as $image) {
            $frame = new \Imagick();
            $frame->readImage($image->getRealPath());
            // THE FOLLOWING 2 LINES ARE NOT WORKING
            //$frame->setImageCompression(\Imagick::COMPRESSION_JPEG);
            //$frame->setImageCompressionQuality(5);
            $frame->setImageDelay(50);

            $gif->addImage($frame);
        }

        // optimizes the GIF
        $gif->optimizeImageLayers();

        // https://hotexamples.com/examples/-/Imagick/optimizeImageLayers/php-imagick-optimizeimagelayers-method-examples.html
        // THE FOLLOWING 2 LINES ARE NOT WORKING
        //$gif->setImageCompression(\Imagick::COMPRESSION_LZW);
        //$gif->setImageCompressionQuality(30);

        // writes the GIF
        $tempPath = tempnam(sys_get_temp_dir(), 'GIF');
        $result = $gif->writeImages($tempPath, true); 

        return $tempPath;
    }
}

似乎addImage没有应用适当的过滤器。

0 个答案:

没有答案