【问题标题】:PHP Warning : imagewebp(): Palette image not supported by webpPHP警告:imagewebp():webp不支持调色板图像
【发布时间】:2021-11-21 11:39:28
【问题描述】:

我正在尝试使用 PHP 从 jpeg 和 png 图像创建 webp 图像,但我有几个错误,例如:
警告:imagewebp():/var/www/html/ 中的 webp 不支持调色板图像img/

我已经咨询了资源Fatal error: Paletter image not supported by webp,但除非我弄错了,否则在我看来我已经应用了这些建议。

这是我的功能:

        function convertToWebP($url) {
    
        $extension = pathinfo($url, PATHINFO_EXTENSION);
        $lstat = lstat($url);
        $mtime = date('d/m/Y H:i:s', $lstat['mtime']);
    
        switch ($extension) {
            case 'jpeg' :
            case 'jpg' :
                $image = imagecreatefromjpeg($url);
                break;
    
            case 'png' :
                $image = imagecreatefrompng($url);
                imagepalettetotruecolor($image);
                imagealphablending($image, true);
                imagesavealpha($image, true);
                break;
    
            case 'gif' :
                $image = imagecreatefromgif($url);
                break;
    
            default : 
                return false;
            }
    
            imagewebp($image, 'php.webp', 80);
            imagedestroy($image);
}

如果能得到一些帮助,我将不胜感激

PHP 7.4.3 服务器版本:Apache/2.4.41 (Ubuntu) Ubuntu 20.04.2 LTS

【问题讨论】:

  • 自己尝试了代码并且没有问题,直到我尝试转换.gif,添加imagepalettetotruecolor 函数调用为我解决了错误。解决方案似乎是在尝试将图像转换为.webp 文件之前确保将图像转换为真彩色。
  • 它确实有效!非常感谢

标签: php webp


【解决方案1】:

感谢 Kim Hallberg 的评论,我找到了解决方案。添加imagepalettetotruecolor 函数来转换.gif 文件就足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-03
    • 2021-01-15
    • 2015-07-16
    • 2019-04-11
    • 1970-01-01
    • 2015-07-23
    • 2022-09-27
    • 2014-12-06
    相关资源
    最近更新 更多