Convert & resize all images inside public folder into webp format in Laravel 10

Convert & resize all images inside public folder into webp format in Laravel 10

14-Sep-2024
| |
Image Carousel

Hello developers in this tutorial we will discuss about how to Convert Image into Webp format and create a downloadable link

Table of Contents

S.no Contents-topics
1 Install Intervention Image
2 Add into Providers and alias
3 Create and setup ImageController
4 Setup route file
5 Convert image into Webp format

1:Install Intervention Image

For converting images into Webp format usnig laravel we need to install a package intervention Image with the help of composer copy the below command and paste it in the terminal
code for image package : 
Copy

composer require intervention/image

2:Add into Providers and alias

For setting up PDF in laravel go to config/app.php register pdf provider and alias in app.php file 
Copy for creating artisan commandCopy

 'providers' => [
        /* Application Service Providers */
        Intervention\Image\ImageServiceProvider::class,
       ],
       'aliases' => Facade::defaultAliases()->merge([
        'Image' => Intervention\Image\Facades\Image::class,
       ])->toArray(),
 

 3:Create and setup ImageController

 Now for setting controller create a controller ImageCotroller 

php artisan make:controlle ImageController

Copy the given below code
code 
Copy

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\File;
 
class ImageController extends Controller
{
    public function convertToWebP()
    {
        $imageDirectory = public_path('images'); // Replace with the actual directory path where the images are located
   
        // Get all files from the image directory
        $files = File::files($imageDirectory);
   
        // Loop through each file
        foreach ($files as $file) {
            $filePath = $file->getPathname();
   
            // Convert only if it's an image file (you can modify this condition based on your requirements)
            if (File::isFile($filePath) && in_array(strtolower($file->getExtension()), ['jpg', 'jpeg', 'png'])) {
                // Load the image using Intervention Image
                $image = Image::make($filePath);
   
                // Convert the image to WebP format with 75% quality
                $webpImagePath = $filePath . '.webp';
                $image->encode('webp', 75)->resize(200, 250)->save($webpImagePath);
   
                // Optionally, you can delete the original image
                // File::delete($filePath);
            }
        }
   
        return "Images converted to WebP format.";
    }
   
}

4:Setup route file

Building new routes for converting image
code : 
Copy

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('convertImage', [ImageController::class, 'convertToWebP']);

5:Convert image into Webp format

php artisan serve
http://127.0.0.1:8000/convertImage

 

Tags: Image conversion in Laravel , convert & resize all images inside public folder into webp format in Laravel , Performance optimization with WebP images in Laravel , Automatic image conversion to WebP in Laravel , Image processing with Laravel Intervention Image package , Bulk image conversion to WebP in Laravel,laravel , php ,laravel-php , mvc laravel, advance laravel , bugs in laravel , laravel advance level,
0 Comments (Please let us know your query)
Leave Comment
Leave Comment
Articles from other Categories
Load More

Newsletter