How to convert image from png, jpg to webp image in Laravel

How to convert image from png, jpg to webp image in Laravel

How to convert image from png, jpg to webp image in Laravel

Hello everyone, this video will demonstrate how to convert any image to a webp image.

Why Webp

This year, WebP is making progress in the image format space. The Google-developed image format employs both lossy and lossless compression techniques, thanks to technology acquired from On2 Technologies.

WebP has been around for a long, and it originally made headlines at a Google press conference in 2010. However, as with any new technology, it had some early problems. Because WebP is now at version 1.0.0, we thought it would be a good opportunity to discuss what makes it so strong and why it’s such a terrific choice for web designers and developers.

a

Before I proceed to describe what WebP actually is, let us focus on why it matters—it’s not only about making images smaller, but it’s also about why and how the images need to be made smaller.

You may have heard about design mistakes killing your SEO and conversion rates. While there are some aspects that CSS or JavaScript are responsible for, like blocking rendering, the images nowadays are a huge part of any website’s weight. Therefore, users on slower connections will have trouble interacting with your website (unless it’s an AMP).

Just recently, It’s an interesting area of expertise, where you take care of lots of small things to deliver one big thing—a superfast, superlight website that reads on any device and looks amazing at the same time.

How to convert image from png, jpg to webp image in Laravel

Chances are, you’ve seen WebP images before. If you open Naijacrawl in Google Chrome, all of those thumbnails are going to be WebP thumbnails. If you were to open the Facebook app on your android phone, all of the images that you see would be WebP. It’s definitely being utilized by various companies around the world to increase performance.

So let’s see how you can do the same using laravel to convert your images to webp image.

Note this works in all laravel versions

I assumed you have a project already or you can create one by using this command

composer create-project 
laravel/laravel webimage
 --prefer-dist

You must have installed a composer before you can run the command.

We need a controller so let’s create one

php artisan make:controller 
ImageWebpController

Inside the controller, we will write a code that will process our image and convert it to webp inside a folder

How to convert image from png, jpg to webp image in Laravel

What it Requires

file extension

file name

folder


namespace
 App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ImageWebpController 
extends Controller {
public function webpImage
(Request $request) 
{
if ($request->hasFile('file')) {
$file = $request->file('file');
$extension = $file->getClientOriginalExtension(); 
$rand = '-' . strtolower(Str::random(10));  
$name = $rand . '.' . $extension;   
return $this->processImage($file, $name);  
 }  
  }    
private function processImage
($file, $name) 
{  
$file->move(public_path('/test/image'), 
$name);  
      //convert to webp         
$webp = public_path() . '/test/image/' .
 $name;      
  $im = imagecreatefromstring
(file_get_contents($webp)); 
 $new_webp =
 preg_replace('"\.(jpg|jpeg|png|webp)$"',
 '.webp', $webp);  
imagewebp($im, $new_webp, 50);  
 return'message'
 => 'image successfully 
converted to webp.
 check your 
folder to see it'
];   
 }
}
?>

from what i did, i check if the request is a file before proceeding , have to get extension of the file and use it make a slug name for the image, because i don’t want it to carry original name.

$extension = $file->
getClientOriginalExtension();

I have to use str function to form a characters for the name of the image

 $rand = '-' . 
strtolower(Str::random(10));

//final name
$name = $rand . '.' . $extension;

I needed another private function because i want to separate my code for easy to read using processImage function.


 private function processImage
($file, $name) {
$file->move(public_path('/test/image'),
 $name); 
       //convert to webp         
$webp = public_path() . '/test/image/' .
 $name;
 $im = imagecreatefromstring
(file_get_contents($webp));  
$new_webp =
 preg_replace('"\.(jpg|jpeg|png|webp)$"', '.webp',
 $webp);
imagewebp($im, $new_webp, 50);        
return'message' 
=> 'image successfully 
converted to webp. 
check your folder 
to see it' ];  
  }
?>

After i called it to the main function which

is webpImage function .

Inside processImage function ,

the file is moved to my public folder called test/image before converting it to webp image

//get image that
 we want to convert 
$webp = public_path() .
 '/test/image/' . $name;
//create the image        
$im = imagecreatefromstring
(file_get_contents($webp));
//accept image extensions of jpg,png,webp 
$im = imagecreatefromstring
(file_get_contents($webp));
 $new_webp = 
preg_replace('"\.(jpg|jpeg|png|webp)$"', 
'.webp', $webp);
//make the webp image and set the quality to 50 
//the more quality the more it becomes broght and increase mb
// you can set to 100 which means no quality will be reduced 
 imagewebp($im, $new_webp,50);
?>

From my code i didn’t add validation rule which you can do it if you needed it , just to make it allow only files and extension type

Be the first to comment

Leave a Reply

Your email address will not be published.


*