php function to convert a jpg image to webp

PHP function to convert a jpg image to webp


Navigation


Please check the example below




<?php
$image= imagecreatefromjpeg($str_file_name);
ob_start();
imagejpeg($image,NULL,100);
$cont= ob_get_contents();
ob_end_clean();
imagedestroy($image);
$content = imagecreatefromstring($cont);
imagewebp($content,str_replace(".jpg",".webp",$str_file_name));

echo "Converted: ".$str_file_name." to: ".str_replace(".jpg",".webp",$str_file_name)."
";
imagedestroy($content);
?>