In this tutorial you will learn about the Laravel 8 Resize Image Before Upload Example and its application with practical example.
In this Laravel 8 Resize Image Before Upload Example tutorial, I’ll show you how to resize image before upload in laravel. In this tutorial you will learn to resize image before upload in laravel using Intervention Image Package. In this article I’ll demonstrate to resize and upload image in laravel Intervention Image Package. In this example will store resized image in the folder and later save it into database.
Intervention Image Package :- Intervention Image Package is an open source laravel composer package used to upload and resize images. Intervention Image package allows you to easily upload and resize image. Intervention Image package make image uploading and resizing much easier.
Laravel 8 Resize Image Before Upload Example
In this step by step guide we will be using Intervention Image Package to resize the image before uploading.
Step 1: Install Laravel 8
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Install Intervention Image Package
In this step, we will install Image intervention Package via the composer dependency manager. Use the following command to install image intervention Package.
1 |
composer require intervention/image |
After Installing Image intervention package, we need to add service provider and alias in config/app.php file as following.
config/app.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
return [ ...... $provides => [ ...... ......, Intervention\Image\ImageServiceProvider::class ], $aliases => [ ..... ....., 'Image' => Intervention\Image\Facades\Image::class ] ] |
Step 3: Create Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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('resizeImage', [ImageController::class, 'resizeImage']); Route::post('resizeImagePost', [ImageController::class, 'resizeImagePost'])->name('resizeImagePost'); |
Step 4: Create Controller File
Now, lets create a controller named ImageController using command given below –
1 |
php artisan make:controller ImageController |
Once the above command executed, it will create a controller file ImageController.php in app/Http/Controllers/ directory. Open the ImageController.php file and put the following code in it.
app/Http/Controllers/ImageController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use Image; class ImageController extends Controller { /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function resizeImage() { return view('resizeImage'); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function resizeImagePost(Request $request) { $this->validate($request, [ 'title' => 'required', '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(100, 100, 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: View File and Create Upload directory
In this step, we will create view/blade file. Lets create a blade file “resizeImage.blade.php” in “resources/views/” directory and put the following code in it respectively.
resources/views/resizeImage.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<!DOCTYPE html> <html> <head> <title>Laravel Resize Image Tutorial</title> <link rel="stylesheet" href="http://demo.w3adda.com/plugin/bootstrap-3.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('imageName') }}" /> </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-4"> <br/> <input type="text" name="title" class="form-control" placeholder="Add Title"> </div> <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> |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/resizeImage |