PHP Laravel 5 Intervention image upload and resize tutorial

PHP Laravel 5 Intervention image upload and resize tutorial

PHP Laravel 5 Intervention image upload and resize tutorial

In this post i will tell you how to resize image in laravel application using Intervention library. You can manipulate and handle PHP image by using Intervention library which is open source. You can take help of this library in your application whenever you need thumbnail image or want to resize after uploading images in PHP Laravel.

Benefits of including library intervention/image in your Laravel application, it maintain the image quality that means you can easily resize your images without losing quality.

You will have to simply follow this step to use intervention/image library in your application.

Here in this example, I define a form to submit and on submit i resize images and move them in destination folder with original image too.

It will display both images after successfully moved in target folder.

Step 1: Installation

In this step you will have to configure intervention/image library in your application.

You will have to run below command in your terminal promt.

composer require intervention/image

Now i assume that you have successfully installed by above command.

OK, now i will configure service provider with their aliases name in following path config/app.php .


Add this service provider in provider array :


Now add facade to aliases array.

'Image' => 'Intervention\Image\Facades\Image'
Step 2: Add Route

In this step add routes to handle request in following path app/Http/routes.php

  1. Route::get('intervention-resizeImage',['as'=>'intervention.getresizeimage','uses'=>'FileController@getResizeImage']);
  2. Route::post('intervention-resizeImage',['as'=>'intervention.postresizeimage','uses'=>'FileController@postResizeImage']);
Step 2: Add Controller

In this step add FileController for handling and manipulate image.

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Image;
  6. class FileController extends Controller {
  7. public function getResizeImage()
  8. {
  9. return view('files.resizeimage');
  10. }
  11. public function postResizeImage(Request $request)
  12. {
  13. $this->validate($request, [
  14. 'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
  15. ]);
  16. $photo = $request->file('photo');
  17. $imagename = time().'.'.$photo->getClientOriginalExtension();
  18. $destinationPath = public_path('/thumbnail_images');
  19. $thumb_img = Image::make($photo->getRealPath())->resize(100, 100);
  20. $thumb_img->save($destinationPath.'/'.$imagename,80);
  21. $destinationPath = public_path('/normal_images');
  22. $photo->move($destinationPath, $imagename);
  23. return back()
  24. ->with('success','Image Upload successful')
  25. ->with('imagename',$imagename);
  26. }
  27. }

In postResizeImage method we validate file as image type with max file size.

getRealPath() method is used take a file from the form request.

You will notice that i move image in two directory within public folder so you will first create this directory in your public folder.

  1. thumbnail_images
  2. normal_images

If files is not writable then give permission to folder.

Step 3: Blade file

Now in this step you will create resizeimage.blade.php in following path resources/views/files/resizeimage.blade.php

  1. @extends('layouts.default')
  2. @section('content')
  3. <div class="panel panel-primary">
  4. <div class="panel-heading">Laravel Intervention upload image after resize</div>
  5. <div class="panel-body">
  6.     @if (count($errors) > 0)
  7.     <div class="alert alert-danger">
  8.      @foreach ($errors->all() as $error)
  9.      <p class="error_item">{{ $error }}</p>
  10.      @endforeach
  11.     </div>
  12.     @endif
  13.     @if (Session::get('success'))
  15.     <div class="row">
  16.         <div class="col-md-12">
  17.         <div class="col-md-4">
  18.             <strong>Image Before Resize:</strong>
  19.         </div>
  20.         <div class="col-md-8">    
  21.             <img src="{{asset('normal_images/'.Session::get('imagename')) }}" />
  22.         </div>
  23.         </div>
  24.         <div class="col-md-12" style="margin-top:10px;">
  25.         <div class="col-md-4">
  26.             <strong>Image after Resize:</strong>
  27.         </div>
  28.         <div class="col-md-8">    
  29.             <img src="{{asset('thumbnail_images/'.Session::get('imagename')) }}" />
  30.         </div>
  31.         </div>
  32.     </div>
  33.     @endif
  34.     {!! Form::open(array('route' => 'intervention.postresizeimage','files'=>true)) !!}
  35.         <div class="row">
  37.             <div class="col-md-6">
  38.                 <br/>
  39.                 {!! Form::file('photo', array('class' => 'form-control')) !!}
  40.             </div>
  41.             <div class="col-md-6">
  42.                 <br/>
  43.                 <button type="submit" class="btn btn-primary">Upload Image</button>
  44.             </div>
  45.         </div>
  46.     {!! Form::close() !!}
  47. </div>
  48. </div>
  49. @endsection

Using Laravel Intervention you can easily resize images on the fly.

Click here to see demo how it works..

Phone: (+91) 8800417876
Noida, 201301
sakarya escort sakarya escort bayan sakarya escort serdivan escort sakarya escort sakarya escort serdivan escort
karasu escort akyazı escort serdivan escort akyazı escort ferizli escort sapanca escort serdivan escort söğütlü escort sakarya escort izmit escort arifiye escort erenler escort hendek escort kocaali escort pamukova escort taraklı escort geyve escort