Laravel 6 Intervention Image Upload Using Ajax

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

Laravel 6 Intervention Image Upload 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 version 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).

Basic Usage Of Intervention Image Package

Create Instance :-

Resize Image to Fixed Size :-

Resize Image Width:-

Resize Image Height:-

Resize Width with Aspect Ratio:-

Resize Height with Aspect Ratio:-

Prevent Possible Upsizing:-

Install Laravel 6

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

Configure Database In .env file

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

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.

Register Package

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

config/app.php

Generate Migration

Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration file to create photos table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the photos table will be created in database along with migrations, password_resets and users table.

Create Model

Next, we need to create a model called Photo using below command.

Once, the above command is executed it will create a model file Photo.php in app directory.

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

Here In the controller, we have following methods –

AjaxIndex() :- It displays Image Upload Form along with Uploaded Image and its thumbnail

AjaxStore() :- To Upload and Resize Image with Intervention Package using Ajax.

Note:- Before uploading any file make sure you have created following two directory in the public folder called profile_images and /profile_images/thumbnail.

If you want to resize the image proportionally and maintain image aspect ratio then you can use add aspectRatio constraint like this –

Example :-

Example 1:-

and image will not cut off. We are passing width as 400 and height as 150. You can change these values as per your requirement.

If you are looking for hard crop then replace below lines

Create Blade / View Files

In this step, we will create view/blade file to generate and display Image Upload Form. Lets create a blade file “index.blade.php” in “resources/views/AjaxInterventionImageUpload/” directory and put the following code in it respectively.

resources/views/AjaxInterventionImageUpload/index.blade.php

Create Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/intervention-ajax-image-upload

Output:-

laravel-5-intervention-image-upload-using-ajax-1

After Image Upload Screen Output:-

laravel-5-intervention-image-upload-using-ajax-2

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