PHP 警告:imagewebp():webp 不支持调色板图像

PHP Warning : imagewebp(): Palette image not supported by webp

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

中的 webp 不支持调色板图像

我已经查阅了资源 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);
}

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

PHP7.4.3 服务器版本:Apache/2.4.41 (Ubuntu) Ubuntu 20.04.2 LTS

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