How to Resize Image before Upload in Laravel 11

Websolutionstuff | May-13-2024 | Categories : Laravel

Hello, laravel web developers! In this article, we'll see how to resize images before uploading in laravel 11. Here, we'll use the intervention/image composer package in laravel 11. Intervention Image is the most popular open source PHP image processing library.

It provides an easy and expressive way to edit images and supports PHP's two most common image processing libraries GD Library and Imagick.

Laravel 11 Resize Image Before Upload and Store in Database

Laravel 11 Resize Image Before Upload


Step 1: Install Laravel 11 Application

In this step, we'll install the laravel 11 application using the following composer command.

composer create-project laravel/laravel example-app


Step 2: Install Intervention Image Package

Then, we'll install the intervention/image composer package using the following command.

Before you begin with the installation make sure that your server environment supports the following requirements.

  • PHP >= 8.1
  • Mbstring PHP Extension
  • Image Processing PHP Extension
$ composer require intervention/image


Step 3: Define Routes

Next, we'll define routes into the web.php file.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
Route::get('image-upload', [ImageController::class, 'index']);
Route::post('image-upload', [ImageController::class, 'store'])->name('');


Step 4: Create Controller File

Then, we'll create a new ImageController for image upload and resizing using the following command.

php artisan make:controller ImageController

Create images and thumbnails (inside the images folder) folder in the public folder.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use Intervention\Image\Laravel\Facades\Image;
class ImageController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(): View
        return view('image-upload');
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function store(Request $request): RedirectResponse
            'image' => 'required|image|mimes:jpeg,png,jpg|max:2048',
        $image = $request->file('image');
        $imageName = time().'.'.$image->extension();
        $destinationPathThumbnail = public_path('images/thumbnail');
        $img = Image::read($image->path());
        $img->resize(100, 100, function ($constraint) {
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $imageName);
        return back()->with('success', 'Image Uploaded successfully!')
                     ->with('imageName', $imageName);


Step 5: Create Blade File

Next, we'll create a blade file image-upload.blade.php to create a thumbnail.


<!DOCTYPE html>
    <title>How to Resize Image before Upload in Laravel 11 - Techsolutionstuff</title>
    <link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link rel="stylesheet" href="" />
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3"><i class="fa fa-star"></i> How to Resize Image before Upload in Laravel 11 - Techsolutionstuff</h3>
        <div class="card-body">

            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <strong>Whoops!</strong> There were some problems with your input.<br><br>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}

                <div class="row">
                    <div class="col-md-4">
                        <strong>Original Image:</strong>
                        <img src="/images/{{ Session::get('imageName') }}" width="300px" />
                    <div class="col-md-4">
                        <strong>Thumbnail Image:</strong>
                        <img src="/images/thumbnail/{{ Session::get('imageName') }}" />
            <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                <div class="mb-3">
                    <label class="form-label" for="inputImage">Image:</label>
                        class="form-control @error('image') is-invalid @enderror">
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>


Step 6: Run the Laravel  11 Application

Now, run the laravel 11 application using the following command.

php artisan serve


You might also like:

Recommended Post
Featured Post
How to Create Form Request Validation in Laravel 10
How to Create Form Request Val...

Hey there! Today, I want to talk to you about a super useful feature in Laravel 10 called form request validation. If yo...

Read More


Laravel 9 to_route() and str() Helper Function
Laravel 9 to_route() and str()...

In this article, we will see laravel 9 to_route() and str() helper function. The to_route function g...

Read More


How to File Upload in Angular 17 Tutorial
How to File Upload in Angular...

In this article, we'll see how to file upload in the angular 17 tutorial. Here, we'll learn about file uplo...

Read More


Laravel whereMonth and whereYear Example
Laravel whereMonth and whereYe...

In this article, we will show you laravel whereMonth and whereYear examples. whereMonth and whereYear are...

Read More