How to upload image in laravel 8

How to upload image in laravel 8

Laravel8 12-07-2021 Saheb Sutradhar

In this article I will show you how to upload image in laravel 8. How we add image validation and how to store it in the server directory.

 

Steps we will follow :

Step 1 : Create laravel 8  project.

Step 2 : Create Image upload controller.

Step 3 : Create route.

Step 4 : Create View using blade.

Step 5 : Run Image upload application.

 

Step 1 : Create laravel 8  project.

Open the terminal or command prompt and run the below command , this command will create a new laravel 8 project .

composer create-project laravel/laravel image-upload

Step 2 : Create Image upload controller.

First we are going to create a controller file where we are going to write logic for image validation , storing the image in server directory. Run the below command in terminal or command prompt to create Image Controller.

php artisan make:controller ImageController

Now let's declare two functions storeImage in the controller to write logic for image validation and storing the image in server directory and another function imageUploadUI will return the view for image upload form.

Create a uploades folder under public directory.

ImageController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageController extends Controller
{

    public function imageUploadUI(){
     return view('image-upload');
    }

    public function storeImage(Request $request){
        /*
        in the view form the image field will have below validations
        ** image filed is required
        ** only pg,jpeg,png image files you can upload
        ** maximum size would be 5000 KB
        */
        $request->validate([
            'image'=>'required|mimes:jpg,jpeg,png|max:5000',
        ]);
        /*
        Once the image is validated , create the name on the image
        */
       $imageName = time().'.'.$request->file('image')->guessExtension();
       $request->image->move(public_path('uploades'),$imageName);
       return back()->with('success','You have successfully upload image.');
    }
}
 
time() method return the current time , guessExtension() returns the extension of the file. In the view form the image field will have validations image filed is required, only pg,jpeg,png image files you can upload, maximum size would be 5000 KB.

 

Step 3 : Create route

In web.php declare two routes one GET route for the viwe and another POST route for storing image.

 

web.php
use App\Http\Controllers\ImageController;
use Illuminate\Support\Facades\Route;

//Routes for image upload

Route::get('/image-upload',[ImageController::class,'imageUploadUI']);
Route::post('/image-upload',[ImageController::class,'storeImage']);

 

Step 4 : Create View using blade.

Let's create a view  file image-upload.blade.php for image upload form under resources/views  and use the enctype enctype=”multipart/form-data” in the form

image-upload.blade.php
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Upload Codelearningpoint</title>
    <style>
        .wreapper {
            width: 40%;
            margin: 20px auto;
            border: 1px solid;
            padding: 10px;
        }

        .error {
            color: red;
            font-size: 12px;
        }

        .uploadBtn {
            display: block;
            margin: auto;
            padding: 15px;
        }

        .successMsg {
            color: green;
            font-size: 12px;
        }
    </style>
</head>

<body>

    <div class="wreapper">

        @if(session('success'))
        <p class="successMsg">{{session('success')}}</p>
        @endif

        <form action="/image-upload" method="POST" enctype="multipart/form-data">

            @csrf
            <label for="img">Image Upload</label>
            <input type="file" name="image" id="img">
            @error('image')
            <p class="error">{{$message}}</p>
            @enderror

            <button class="uploadBtn">Upload</button>
        </form>
    </div>

</body>

</html>

Step 5 : Run Image upload application

In the command prompt or in terminal run beow cammand to start the laravel local server

php artisan serve

Once the server is up on the browser hit the URl to load the application.

http://127.0.0.1:8000/image-upload

How to upload image in laravel 8

 

 

Thanks for reading the artical , really glad to help you.