Convert JPG/PNG Image to WEBP Using PHP

In this post, we are going to learn how to convert JPG/PNG images to WEBP images.

WebP is a modern image format that provides superior lossless and lossy compression for images on the web. Using WebP, webmasters and web developers can create smaller, richer images that make the web faster. The WebP format reduces file size more than standard JPEG compression while maintaining similar or better image quality.

WebP is natively supported in Google Chrome, Firefox, Edge, the Opera browser, and by many other tools and software libraries. Developers have also added support to a variety of image editing tools.

 

<h1 style="text-align:center;">Convert JPG/PNG Image to WEBP Using PHP</h1>

<?php
$file = '2.png';
$image = imagecreatefromstring(file_get_contents($file));
ob_start();
imagejpeg($image,NULL,100);
$cont = ob_get_contents();
ob_end_clean();
imagedestroy($image);
$content = imagecreatefromstring($cont);
$output = 'images/output.webp';
imagewebp($content,$output);
imagedestroy($content);
echo '<h4>Output Image Saved as '.$output.'</h4>';
?>

Follow this video for complete guidance :