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
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.