Ajax Image Upload using Intervention Package in Laravel


Today, I will learn you how to store file with Intervention Package in laravel 6. In This blog shows you how you can upload the image into the database and folder with example

we upload the image into the folder and store into database using jquery ajax in laravel 6 application.

We will Follow the bellow step.

Step 1 : Install Laravel project

In this step you can install fresh laravel project to using bellow command.

composer create-project --prefer-dist laravel/laravel ImageInvervention

Step 2 : Install Image Intervention Package

Now We need to install the laravel image intervention package for resizing the image size. use the below command and install it.

composer  require intervention/image 

After successfully install a laravel image intervention package. Register image intervention package to providers and aliases go to app/config/app.php register here.

'providers' => [

 'aliases' => [
    'Image' => Intervention\Image\Facades\Image::class

Step 3 : Create Migration & Model

Now you will bellow command to create migration and model in laravel app.

php artisan make:model Photo -m

Above command to create one table migration file and one model file.


use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePhotosTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('photos', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down()

Bellow command to use migrate your table.

php artisan migrate

Step 4 : Create Controller

In this step create controler file to use bellow command.

php artisan make:controller ImageController

Step 5 : Create Controller method

Create controller after you can put the bellow code in controller file.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Validator,Redirect,Response,File;
Use Image;
Use App\Photo;
use Intervention\Image\Exception\NotReadableException;
class ImageController extends Controller
    public function create()
        return view('image');
    public function store(Request $request)
           'photo_name' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        if ($files = $request->file('photo_name')) {
            // for save original image
            $ImageUpload = Image::make($files);
            $originalPath = 'public/images/';
            // for save thumnail image
            $thumbnailPath = 'public/thumbnail/';
            $ImageUpload = $ImageUpload->save($thumbnailPath.time().$files->getClientOriginalName());
            $photo = new Photo();
            $photo->photo_name = time().$files->getClientOriginalName();
        $image = Photo::latest()->first(['photo_name']);
        return Response()->json($image);

Step 6 : Create Routes

In this step you will create route in web.php file.



Step 7 : Create View File

In this step you can create blade file in laravel app.


<!DOCTYPE html>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Laravel Ajax Image Upload Using Intervention Package Example - LaravelCode</title>
    <link rel="stylesheet" href="" />
    <script src="//"></script>
        .avatar-pic {
             width: 300px;
    <div class="container">
        <h3 style="margin-top: 12px;" class="text-center alert alert-success">Laravel- Ajax Image Upload Using Intervention Package Example</h3>
        <div class="row justify-content-center">
            <div class="col-md-8">
                <form id="imageUploadForm" action="javascript:void(0)" enctype="multipart/form-data">
                    <div class="file-field">
                        <div class="row">
                            <div class=" col-md-8 mb-4">
                                <img id="original" src="" class=" z-depth-1-half avatar-pic" alt="">
                                <div class="d-flex justify-content-center mt-3">
                                    <div class="btn btn-mdb-color btn-rounded float-left">
                                        <input type="file" name="photo_name" id="photo_name" required=""> <br>
                                        <button type="submit" class="btn btn-secondary d-flex justify-content-center mt-3">submit</button>
                            <div class=" col-md-4 mb-4">
                                <img id="thumbImg" src="" class=" z-depth-1-half thumb-pic" alt="">
        $(document).ready(function (e) {
            $('#imageUploadForm').on('submit',(function(e) {
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                var formData = new FormData(this);
                    url: "{{ route('') }}",
                    contentType: false,
                    processData: false,
                        $('#original').attr('src', 'public/images/'+ response.image);
                        $('#thumbImg').attr('src', 'public/thumbnail/'+ response.image);
                    error: function(data){

i hope you like this article.