Convert Image into Webp format and create a downloadable link

Convert Image into Webp format and create a downloadable link

| |
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 Contents-topics
1 Install Intervention Image
2 Add into Providers and alias
3 Create blade file
4 Create and setup ImageController
5 Setup route file
6 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 : 

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 */
       'aliases' => Facade::defaultAliases()->merge([
        'Image' => Intervention\Image\Facades\Image::class,

3:Create blade file

Now create a blade file resources/views/convertImage.blade.php and paste the below code
blade file code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Convert image by DeveloperCodez</title>
    <script src="" crossorigin="anonymous"></script>
<form action="{{url('convertImage')}}" id="formSubmit" method="POST" enctype="multipart/form-data">
    <input type="file" name="image">
    <button type="submit">Convert to WebP</button>
    $(document).ready(function(e) {
            $('#formSubmit').on('submit', (function(e) {
                var formData = new FormData(this);
                    type: 'POST',
                    url: $(this).attr('action'),
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(result) {
                        if (result.status == 'success') {
                        } else {

 5:Create and setup ImageController

 Now for setting controller create a controller ImageCotroller 

php artisan make:controlle ImageController

Copy the given below code

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
class ImageController extends Controller
    public function convertImage(Request $request)
            'image' => 'required|image',
        $imagePath = $request->file('image')->getPathname();
        $webpPath = $this->convertToWebp($imagePath);
        return response()->download($webpPath)->deleteFileAfterSend(true);
    private function convertToWebp($imagePath)
        $webpPath = tempnam(sys_get_temp_dir(), 'webp');
        $image = Image::make($imagePath)->encode('webp')->save($webpPath);
        return $webpPath;

6:Setup route file

Building new routes for converting image
code : 

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::post('convertImage', [ImageController::class, 'convertImage']);

7:Convert image into Webp format

php artisan serve


Tags: Image to WebP conversion , WebP image converter , Convert image to downloadable WebP , WebP format for images , Laravel image conversion to WebP , WebP download link generator , Laravel image conversion to WebP,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