You know that Laravel provides an image validation rule and in this tutorial, I will show you how we can properly use this rule to validate images before uploading them to the server. We will validate image field is required so that the user can not input a null image to the server.

Then we will validate file types that, the uploaded file should be must image then we will validate extension types or laravel image validation mimes type like image should be png or jpg, etc. After that, we will validate the max size and min size of the image. And finally, we will validate the dimensions of the uploaded image like max width or min-width.

See the validation error message from the below image that we are going to make in this laravel image validation rules tutorial.

laravel-10-image-validation-example-with-extensionNow see the example code of laravel 10 image validation.

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\File;
use Illuminate\Support\Facades\Validator;

class TutorialController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function store(Request $request)
    {
        Validator::validate($request->all(), [
            'image' => [
                'required',
                File::image()
                    ->types(['png', 'jpg', 'jpeg'])
                    ->min(1024)
                    ->max(12 * 1024)
                    ->dimensions(
                        Rule::dimensions()
                            ->maxWidth(1000)
                            ->maxHeight(500)
                    )
            ]
        ]);
    }
}

 

Now show the error message in your blade file like:

resources/views/welcome.blade.php

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background: gray; color:#f1f7fa; font-weight:bold;">
                    Laravel 10 Image Validation Example - Laravelia
                </div>
                 <div class="card-body">                    
                    <form 
                        action="{{ route('user.store') }}" 
                        method="post" 
                        enctype="multipart/form-data"
                    >
                    @csrf
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label">
                                Image
                            </label>
                            <div class="col-sm-9">
                                <input type="file" class="form-control  @error('image') is-invalid @enderror" name="image">
                                @error('image')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <div class="row mb-3">
                            <label class="col-sm-3 col-form-label"></label>
                            <div class="col-sm-9">
                                <button type="submit" class="btn btn-success btn-block text-white">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

 

Read also: Laravel 10 File Validation Example Before Saving

 

Conclusion

Now we know how to validate images in laravel 10 properly. Hope this laravel image validation tutorial will help you. After completing laravel image validation max size, your concept will be laravel image validation mimes.