onlinecode

PHP Laravel 5 Intervention image upload and resize tutorial

PHP Laravel 5 Intervention image upload and resize tutorial

In this post we will give you information about PHP Laravel 5 Intervention image upload and resize tutorial. Hear we will give you detail about PHP Laravel 5 Intervention image upload and resize tutorialAnd how to use it also give you demo for it if it is necessary.

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 .

config/app.php

Add this service provider in provider array :

'InterventionImageImageServiceProvider'

Now add facade to aliases array.

'Image' => 'InterventionImageFacadesImage'

Step 2: Add Route

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

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']);
Route::get('intervention-resizeImage',['as'=>'intervention.getresizeimage','uses'=>'FileController@getResizeImage']);
Route::post('intervention-resizeImage',['as'=>'intervention.postresizeimage','uses'=>'FileController@postResizeImage']);

Step 2: Add Controller

In this step add FileController for handling and manipulate image.

app/Http/Controllers/FileController.php

  1. <?php
  2. namespace AppHttpControllers;
  3. use IlluminateHttpRequest;
  4. use AppHttpControllersController;
  5. use Image;
  6. class FileController extends Controller {
  7. public functiongetResizeImage()
  8. {
  9. returnview('files.resizeimage');
  10. }
  11. public functionpostResizeImage(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. returnback()
  24. ->with('success','Image Upload successful')
  25. ->with('imagename',$imagename);
  26. }
  27. }
<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpControllersController;
use Image;
class FileController extends Controller {

    public function getResizeImage()
    {
        return view('files.resizeimage');
    }
    public function postResizeImage(Request $request)
    {
        $this->validate($request, [
            'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
        ]);

        $photo = $request->file('photo');
        $imagename = time().'.'.$photo->getClientOriginalExtension(); 
   
        $destinationPath = public_path('/thumbnail_images');
        $thumb_img = Image::make($photo->getRealPath())->resize(100, 100);;
        $thumb_img->save($destinationPath.'/'.$imagename,80);
                    
        $destinationPath = public_path('/normal_images');
        $photo->move($destinationPath, $imagename);

        return back()
            ->with('success','Image Upload successful')
            ->with('imagename',$imagename);
    }
}

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

resources/views/files/resizeimage.blade.php

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

	{!! Form::open(array('route' => 'intervention.postresizeimage','files'=>true)) !!}
		<div >
		
			<div >
				<br/>
				{!! Form::file('photo', array('class' => 'form-control')) !!}
			</div>
			<div >
				<br/>
				<button type="submit" >Upload Image</button>
			</div>
		</div>
	{!! Form::close() !!}
 </div>
</div>
@endsection

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

Click here to see demo how it works..

Show Demo

Label :

PHP

Laravel PHP Framework

How To

API

MVC

Hope this code and post will helped you for implement PHP Laravel 5 Intervention image upload and resize tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version