Laravel 9 Resize Image before Upload Example Tutorial

Using Laravel resize image before upload you will learn how to create resize image functionality in the Laravel application. In this tutorial, you will understand the logic of laravel 9 generate thumbnail images generation example step by step guide.

Intervention package follows the FIG standard PSR-2 to ensure a high level of interoperability between shared PHP code; this Image library is a powerful open-source PHP image handling and manipulation package.

It allows you to control PHP image manipulating easier; not just that, you can also create image thumbnails, watermarks, or format large image files. You can use image upload before compress or resize in laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.

Download Laravel App

First we need to download or install a new laravel application using the following command:

composer create-project laravel/laravel --prefer-dist laravel-resize-image

Install Intervention Image Package

Image resizing in Laravel application needs to have intervention/image library installed; simultaneously, it allows you to generate a thumbnail image.

composer require intervention/image

Now update the intervention class in providers and aliases array inside the config/app.php configuration file.

return [
    ......
    ......
    $provides => [
        ......,
        Intervention\Image\ImageServiceProvider::class
    ],
    $aliases => [
        .....,
        'Image' => Intervention\Image\Facades\Image::class
    ]
]

Create Controller

First you need to create a new controller using the below command;

php artisan make:controller FileController
Open the app/Http/Controllers/FileController.php file, put the following code on it;
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Image;
class FileController extends Controller
{
    /**
     * Init view.
     */
    public function index()
    {
        return view('resize-image');
    }
  
    /**
     * Image resize
     */
    public function resizeImage(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'path' => 'required|image|mimes:jpg,jpeg,png,svg,gif|max:2048',
        ]);
  
        $image = $request->file('imgFile');
        $input['path'] = time().'.'.$image->extension();
     
        $filePath = public_path('/thumbnails');
        $img = Image::make($image->path());
        $img->resize(110, 110, function ($const) {
            $const->aspectRatio();
        })->save($filePath.'/'.$input['path']);
   
        $filePath = public_path('/images');
        $image->move($filePath, $input['path']);
   
        return back()
            ->with('success','Image uploaded')
            ->with('fileName',$input['path']);
    }
}

Add Routes

<?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('resize-image', [ImageController::class, 'index']);
Route::post('resize-image', [ImageController::class, 'resizeImage'])->name('resize-image');

Create Blade File

Create, or you might also use the existing resize-image blade view file, afterward add the following code in resources/views/resize-image.blade.php file:

Also, create images equally important thumbnails folders in the public directory.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Resize Image Tutorial</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
</head>
<body>
  
<div class="container">
    <h1>Laravel Resize Image Tutorial</h1>
    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
         
    @if ($message = Session::get('success'))
    <div class="alert alert-success alert-block">
        <button type="button" class="close" data-dismiss="alert">×</button>    
        <strong>{{ $message }}</strong>
    </div>
    <div class="row">
        <div class="col-md-4">
            <strong>Original Image:</strong>
            <br/>
            <img src="/images/{{ Session::get('fileName') }}" />
        </div>
        <div class="col-md-4">
            <strong>Thumbnail Image:</strong>
            <br/>
            <img src="/thumbnail/{{ Session::get('fileName') }}" />
        </div>
    </div>
    @endif
         
    <form action="{{ route('resize-image') }}" method="post" enctype="multipart/form-data">
        @csrf
        <div class="row">
            <div class="col-md-4">
                <br/>
                <input type="text" name="name" class="form-control" placeholder="Add Title">
            </div>
            <div class="col-md-12">
                <br/>
                <input type="file" name="path" class="image">
            </div>
            <div class="col-md-12">
                <br/>
                <button type="submit" class="btn btn-success">Upload Image</button>
            </div>
        </div>
    </form>
</div>
  
</body>
</html>

Now run the command to start the project.

php artisan serve
http://localhost:8000/resize-image

Now create two directory in your public folder (1)images and (2)thumbnail and please give permission to that folder and test.

That’s over now, we will learn today how to resize an image and keep it in the images and thumbnails folder.

Leave a Comment