0

I am trying to create webp images from jpeg and png images using PHP but I have several errors like:
Warning: imagewebp(): Palette image not supported by webp in /var/www/html/img/

I have consulted the resource Fatal error: Paletter image not supported by webp but unless I am mistaken, it seems to me that I have already applied these recommendations.

Here is my function :

        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);
}

I would be very grateful to have some help

PHP 7.4.3 Server version: Apache/2.4.41 (Ubuntu) Ubuntu 20.04.2 LTS

  • Tried the code myself and had no issues until I tried to convert a `.gif`, adding the `imagepalettetotruecolor` function call to that solved the error for me. The solution seems to be to make sure your images are converted to true colors before you try and convert the image to a `.webp` file. – Kim Hallberg Sep 29 '21 at 15:44
  • It actually works ! Thank you very much – Interstella5555 Sep 30 '21 at 07:34

1 Answers1

2

Thanks to Kim Hallberg's comment, I found the solution. it was enough to add the imagepalettetotruecolor function to convert the .gif files as well.