How to Resize image in Laravel Using Intervention Package
If you want to learn how to resize image before upload in Laravel , then this article is for you . In this article we will learn how to resize image in Laravel with Example . We will use intervention package for working with Image and this package is absolutely free to use .
The Intervention Package makes it very much easier for working with images in Laravel but you just need to install it and setup it which we will learn in this article very easily . So let's dive in and see how to resize image in Laravel .
Contents :
- Install Intervention Package
- Setup Provider class and Aliases
- Setup Routes
- Resize Code on Controller
- Output
Step 1 - Install Intervention Package :
First you need to install the intervention package to work with images in Laravel . To Install Intervention Package in your project use the following command .
composer require intervention/image
Step 2 - Add Providers and Aliases :
Open your Config folder and open app.php ( config\app.php ) and add the following.
Inside the providers array add the following line as shown below .
'providers' => [ ---------------------, ---------------------, ---------------------, ---------------------, Intervention\Image\ImageServiceProvider::class, ],
Inside the aliases array add the following line as shown below .
'aliases' => [ ---------------------, ---------------------, ---------------------, ---------------------, 'Image' => Intervention\Image\Facades\Image::class, ],
Step 3 - Setup Routes :
Route::get('/image','resizeController@index'); Route::post('/resize','resizeController@resize');
Step 4 - Image Resize code on Controller :
Use the following class on the top of your controller .
use Intervention\Image\ImageManagerStatic as Image;
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Intervention\Image\ImageManagerStatic as Image; class resizeController extends Controller { public function index() { return view('image'); } public function resize(Request $request) { if($request->hasfile('image')) { $file=$request->file('image'); $extension=$file->getClientOriginalExtension(); $filename=time().'.'.$extension; $image_resize = Image::make($file->getRealPath()); $image_resize->resize(100, 100); $file->move('public/upload/resized/',$filename); dd('resized'); } else { dd('not resized'); } } }
Image.blade.php :
@extends('layouts.app') @section('content') <div class="row"> <div class="col-12 text-center"> <form class="form-control" action="{{url('/resize')}}" method="post" enctype="multipart/form-data"> {{csrf_field()}} <input type="file" name="image" class="form-control"> <button type="submit" class="btn btn-success">submit</button> </form> </div> </div> @endsection
Output :
Hope you learned a lot from this article like how to use intervention package in laravel , how to resize image in Laravel and in future we will also cover lots of methods provided by Intervention package to work with images in Laravel .
Thank you for reading this article 😊
For any query do not hesitate to comment 💬