Tuesday, 22 October 2019

How to convert png file to webp file

I need to convert an image (png) to (webp) file.

After uploading a png file, the webp image has been generated, but the webp file didn't copy the transparency of a png file, instead it creates a black background.

This is my php code:

$type = wp_check_filetype($file, null);
$ext = $type['ext'];
if ($ext === 'jpg' || $ext === 'jpeg') {
    $im = imagecreatefromjpeg($file);
    $webp = imagewebp($im, str_replace($ext, 'webp', $file), 70);
} elseif ($ext === 'png') {
    $im = imagecreatefrompng($file);
    imagepalettetotruecolor($im);
    $webp = imagewebp($file, str_replace('png', 'webp', $file));
}
imagedestroy($im);


from How to convert png file to webp file

No comments:

Post a Comment