【问题标题】:PHP Imagick Resizing Gif frames has weird issuesPHP Imagick Resizing Gif frames 有奇怪的问题
【发布时间】:2019-04-07 18:18:54
【问题描述】:

我正在尝试调整 gif 的所有帧的大小,但有时它们会变得非常奇怪。

我已经看到使用命令行的示例,我现在想尝试避免这种情况。

原文:

调整大小:

你可以清楚地看到问题。

现在我的代码:

     $imgBlob = file_get_contents(__DIR__ . '/../assets/test_gif.gif');

    if ($imgBlob === false) {
        echo 'img blob failed!' . PHP_EOL;
        return;
    }

    $img = new Imagick();
    $img->readImageBlob($imgBlob);
    $img->coalesceImages();
    $count = 0;
    foreach ($img as $_img) {
       // $_img->coalesceImages();
        $imgWidth = $_img->getImageWidth();
        $imgHeight = $_img->getImageHeight();

        $ratio = $imgHeight / $imgWidth;

        $resizeWidth = 200;
        $resizeHeight = 300;

        if ($ratio > 0) {
            $resizeWidth = round($resizeHeight / $ratio, 0);
        } else {
            $resizeHeight = round($resizeWidth / $ratio, 0);
        }
        //if ($_img->adaptiveResizeImage($resizeWidth, $resizeHeight) === false) {
        if ($_img->resizeImage($resizeWidth, $resizeHeight, Imagick::FILTER_CATROM, 1, 0) === false) {
            echo 'FAILED' . PHP_EOL;
        }
        $count++;
    }

    $thumbnailOnDisk = __DIR__ . '/../assets/test_resized.gif';
    if (file_exists($thumbnailOnDisk)) {
        unlink($thumbnailOnDisk);
    }

    $img = $img->deconstructImages();
    if ($count > 1) {
        $file = $img->writeImages($thumbnailOnDisk, true);
    } else {
        $file = $img->writeImage($thumbnailOnDisk);
    }
    echo 'DONE' . PHP_EOL;

不确定 coalesceImages 或 deconstructImages 到底在做什么,我很难在网上找到一个可以解决我的问题的示例。

【问题讨论】:

    标签: php imagick


    【解决方案1】:
    $img->coalesceImages();
    

    返回一个我正在丢弃的 imagick 对象。

    $img = $img->coalesceImages();
    

    工作。

    【讨论】:

      【解决方案2】:

      在调整图片大小时,还需要设置图片页面的大小。

      http://php.net/manual/en/imagick.setimagepage.php

      我曾经读过,在某些情况下,gif 的标题可能没有说明正确的图像大小。这就是为什么以任何方式设置图像页面都很有用的原因。

      例子:

      `$image->resizeImage(120, 110, imagick::FILTER_CATROM, 1);
      // Set the page size
      $image->setImagePage(120, 110, 0, 0);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-04
        • 2020-07-28
        • 2015-12-20
        • 2012-02-02
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多