Laravel 7/6 Intervention Upload Image Using Ajax

In this tutorial you will learn about the Laravel 7/6 Intervention Upload Image Using Ajax and its application with practical example.

Laravel 7/6 Intervention Upload Image Using Ajax

In this Laravel Intervention Image Upload Using Ajax example, we will learn how to upload and resize image using jquery ajax. In this tutorial I have used Intervention Image Package to upload and resize the image using jquery ajax and then save image into the database. This laravel image upload example is works with laravel version7, 6, 5.8 & 5.7 .

In this laravel image upload example, I’ll show you how to upload image into folder and then save it into database. In this tutorial before saving image into database we will resize the image and create it’s thumbnail image and then save it into thumbnail directory using the image intervention package.

Before uploading the image we will validate it using server side validation. In this example we will be uploading the image using jquery ajax without page refresh and reload. After successfully image upload into the database and folder we will display original image along with its thumbnail image (resize image).

  • Install Laravel Fresh Setup
  • Setup Database
  • Install Image Intervention Package
  • Generate migration file and model
  • Make Route
  • Create Controller & Methods
  • Create Blade View
  • Make Folder
  • Run Development Server
  • Conclusion

1). Install Laravel Fresh Setup

First of all we need to create a fresh laravel project, download and install Laravel 6 using the below command

2). Setup Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

.env

3). Install Image Intervention Package

In this step, we will install Image intervention Package via the composer dependency manager. Use the following command to install image intervention Package.

After Installing Image intervention package, we need to add service provider and alias in config/app.php file as following.

config/app.php

4). Generate Migration & Model

It command will create one model name Photo and also create one migration file for the Photo table. After successfully run the command go to database/migrations file and replace function, below here :

Before we run PHP artisan migrate command go to app/providers/AppServiceProvider.php and put the below code :

Next, migrate the table using the below command :

5). Make Route

now, create two routes in the web.php file.

web.php

6). Create Controller

Next, we have to create a controller for image uploading and resizing. Create a controller named ImageController using command given below –

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

7). Create Blade view

In this step, we need to create a blade view file. Go to app/resources/views and create one file name image.blade.php :

Now we will implement a laravel ajax image upload script, put the below script after the closing of the body tag.

8). Make Folder

9). Run Development Server

Now we are ready to run our example so run bellow command to quick run.

In this tutorial we have learn about the Laravel 7/6 Intervention Upload Image Using Ajax and its application with practical example. I hope you will like this tutorial.