Post Details

How to Make Laravel 10 Image Validation Rule Example
28 Oct

How to Make Laravel 10 Image Validation Rule Example

Hi Guys,

This tutorial will give you an example of Laravel 10 image validation rules. I explained simply about Laravel 10 image validation. We will use Laravel 10 image validation size. This article goes into detail on Laravel 10 image validation mimes. Let's see below example of Laravel 10 validation image width and height below.

Laravel 10 provides a list of default image validation rules to prevent users.

  • image
  • mimes
  • dimensions
  • max

using the above validation rules you can set file types like "jpg, png, jpeg, gif, svg", using max you can set the limit of file size. using dimensions you can set file height and width limit. so, let's see the following simple example code:

Step 1: Install Laravel 10 App

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Controller

In this step, we will create a new ImageController; in this file, we will add two methods index() and store() for render view and store image logic.

Let's create ImageController by following the command:

php artisan make:controller ImageController

next, let's update the following code to Controller File.

app/Http/Controllers/ImageController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
  
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(): View
    {
        return view('imageUpload');
    }
        
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
        $this->validate($request, [
            'image' => ['required',
                        'image',
                        'mimes:jpg,png,jpeg,gif,svg',
                        'dimensions:min_width=100,min_height=100,max_width=1000,max_height=1000',
                        'max:2048'],
        ]);
        
        $imageName = time().'.'.$request->image->extension();  
         
        $request->image->move(public_path('images'), $imageName);
      
        /* 
            Write Code Here for
            Store $imageName name in DATABASE from HERE 
        */
        
        return back()
                    ->with('success', 'You have successfully upload image.')
                    ->with('image', $imageName); 
    }
}

Step 3: Create and Add Routes

Furthermore, open the routes/web.php file and add the routes to manage GET and POST requests for render view and store image logic.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ImageController;
  
/* 
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::controller(ImageController::class)->group(function(){
    Route::get('image-upload', 'index');
    Route::post('image-upload', 'store')->name('image.store');
});

Step 4: Create Blade File

At the last step, we need to create an imageUpload.blade.php file and in this file, we will create a form with a file input button. So copy it below and put it on that file.

resources/views/imageUpload.blade.php

 

<!DOCTYPE html>
<html>
<head>
    <title>How to Laravel 10 Image Validation Rule Example - OmTutorial.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
      
<body>
<div class="container">
       
    <div class="panel panel-primary">
  
      <div class="panel-heading">
        <h2>How to Laravel 10 Image Validation Rule Example - OmTutorial.com</h2>
      </div>
 
      <div class="panel-body">
       
        @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
        </div>
        <img src="images/{{ Session::get('image') }}">
        @endif
      
        <form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data">
            @csrf
  
            <div class="mb-3">
                <label class="form-label" for="inputImage">Image:</label>
                <input 
                    type="file" 
                    name="image" 
                    id="inputImage"
                    class="form-control @error('image') is-invalid @enderror">
  
                @error('image')
                    <span class="text-danger">{{ $message }}</span>
                @enderror
            </div>
   
            <div class="mb-3">
                <button type="submit" class="btn btn-success">Upload</button>
            </div>
       
        </form>
      
      </div>
    </div>
</div>
</body>
    
</html>

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL, and view the app output:

http://localhost:8000/image-upload

I hope it can help you...

0 Comments

Leave a Comment