Hi, folks will show how you can convert any image to webp image.
WebP is making headway in the image format space this year. The image format — which is developed by Google — uses both lossy and lossless compression by utilizing technology that Google purchased from On2 Technologies.
WebP has been around a while now, actually, and it first made headlines in a 2010 Google press conference. But, like any new technology, it had some initial rough patches. Today, WebP is on version 1.0.0, so we thought it would be a great time to talk about what makes WebP so powerful, and why it’s a fantastic option for web designers and developers.
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.
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.
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
What it Requires
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.
$file-> getClientOriginalExtension();extension =
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.
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
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
Find this article helpful kindly share it.