Laravel Resize Image Before Upload

In this example i will show laravel resize image before upload. In this post we will use intervention/image package for resize image in laravel. Intervention provide a resize image function that will take a three parameters.

I explain step by step laravel image resize and crop. we will show laravel image resize and upload. we will generate thumbnail image on upload in laravel.

In this post i will show laravel resize image example. we will discuss on laravel image resize file size. how to resize image in laravel.

Step 1: Install Intervention Image Package

In this step we will install intervention image package for resize image upload in laravel.

composer require intervention/image

Step 2 : Config app file

$provides => [
        ......
        ......,
        'Intervention\Image\ImageServiceProvider'
    ],

    $aliases => [
        .....
        .....,
        'Image' => 'Intervention\Image\Facades\Image'
    ]

Step 3: Create Routes

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;


Route::get('resizeImage', [UserController::class, 'resizeImage']);
Route::post('resizeImagePost', [UserController::class, 'resizeImagePost'])->name('resizeImagePost');

Step 4: Create Controller File

In this step we will create UserController.php file in your app/Http/Controllers directory.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Image;

class UserController extends Controller
{
    public function resizeImage()
    {
        return view('users');
    }
  
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function resizeImagePost(Request $request)
    {
        $this->validate($request, [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
  
        $image = $request->file('image');
        $input['imagename'] = time().'.'.$image->extension();
     
        $destinationPath = public_path('/thumbnail');
        $img = Image::make($image->path());
        $img->resize(250, 250, function ($constraint) {
            $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['imagename']);
   
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $input['imagename']);
   
        return back()
            ->with('success','Image Upload successful')
            ->with('imageName',$input['imagename']);
    }
}

Step 5: Create View File 

In this step we will create users.blade.php file for resize image in laravel.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Resize Image Tutorial - web-tuts.com</title>
    <link rel="stylesheet" href="https://www.nicesnippets.com/adminTheme/bower_components/bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
  
<div class="container">
    <h1>Laravel Resize Image Tutorial - web-tuts.com</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('imageName') }}"  width="100%"/>
        </div>
        <div class="col-md-4">
            <strong>Thumbnail Image:</strong>
            <br/>
            <img src="/thumbnail/{{ Session::get('imageName') }}" />
        </div>
    </div>
    @endif
         
    <form action="{{ route('resizeImagePost') }}" method="post" enctype="multipart/form-data">
        @csrf
        <div class="row">
            
            <div class="col-md-12">
                <br/>
                <input type="file" name="image" 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>

Step 6: Upload directory

In this step we will create images and thumbnail directory inside your public directory.

I hope you understand of laravel resize image upload and it can help you…