In this tutorial you will learn about the Laravel 7/6 Ajax Image Upload With Preview Example Tutorial and its application with practical example.
In this Laravel 7/6 Ajax Image Upload With Preview Example Tutorial, I’ll show you how to upload image along with preview before upload using ajax in laravel. In this laravel ajax image upload with preview example, I’ll show you how to upload image into folder and then save it into database using ajax. In this tutorial before saving image into database we will show preview of the image and then save it into directory. Before uploading the image we will display preview of the image. After successfully image upload into the database and folder we will display success message on the screen.
Laravel 7/6 Ajax Image Upload With Preview Example Tutorial
- Install Laravel App
- Setup Database
- Create Image migration file and model
- Add Route For Image
- Create Controller & Methods
- Create Blade View
- Create Folder
- Run Development Server
1). Install Laravel App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
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.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
3). Create Image migration file and model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Photo -m |
The above command will create a model name Photo and also create a migration file for Photo table. After successfully run the command go to database/migrations file and modify function as following
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public function up() { Schema::create('photos', function (Blueprint $table) { $table->increments('id'); $table->string('photo_name'); $table->timestamps(); }); } |
Before you run php artisan migrate command go to app/providers/AppServiceProvider.php and add the below code into AppServiceProvider.php file:
1 2 3 4 5 6 7 8 9 |
... use Illuminate\Support\Facades\Schema; .... function boot() { Schema::defaultStringLength(191); } ... |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
4). Add Route For Image
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('photo', 'ImageController@index'); Route::post('save-photo', 'ImageController@save'); |
5). Create Controller
Now, lets create a controller named ImageController using command given below –
1 |
php artisan make:controller ImageController |
After creating controller go to app/controllers/ImageController.php and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<?php 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 index() { return view('image'); } public function save(Request $request) { request()->validate([ '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/'; $ImageUpload->save($originalPath.time().$files->getClientOriginalName()); // for save thumnail image $thumbnailPath = 'public/thumbnail/'; $ImageUpload->resize(250,125); $ImageUpload = $ImageUpload->save($thumbnailPath.time().$files->getClientOriginalName()); $photo = new Photo(); $photo->photo_name = time().$files->getClientOriginalName(); $photo->save(); } $image = Photo::latest()->first(['photo_name']); return Response()->json($image); } } |
6). Create Blade view
Now, we will create a blade file. Go to app/resources/views and create one file name image.blade.php :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!DOCTYPE html> <html> <head> <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 Tutorial</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <style> .avatar-pic { width: 300px; } </style> </head> <body> <div class="container"> <h3 style="margin-top: 12px;" class="text-center alert alert-success">Laravel Ajax Image Upload Tutorial</h3> <br> <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> </div> </div> <div class=" col-md-4 mb-4"> <img id="thumbImg" src="" class=" z-depth-1-half thumb-pic" alt=""> </div> </div> </form> </div> </div> </div> </body> </html> |
7). Create Folder
Now, Go to public folder and create a folder name images
1 2 |
Go to public folder => first create folder name images |
8). Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 |
http://127.0.0.1:8000/photo Or http://localhost/blog/public/photo |