There is a beautiful validation rule class in laravel for validating images. In this tutorial, we will validate an image type that should be image and then we will validate image extension like the image must be a png file or jpg file like that.

File rule class gives us a method called types and using them we can pass a collection of file types that we want to accept to upload to the server. Let's see the error message of laravel image extension validation:

laravel-10-image-extension-validation-example

Now we will see how to validate file extension in laravel? Let's see the example code of laravel image validation must be an image.

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'])
            ]
        ]);
    }
}

 

If the uploaded file is not an image, then validate the extension like the below:

app/Http/Controllers/TutorialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
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(), [
            'attachment' => [
                'required',
                File::types(['mp3', 'mp4'])
                    ->min(1024)
                    ->max(12 * 1024),
            ]
        ]);
    }
}

 

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 Image Validation All You Need To Know

 

Conclusion

Now we know laravel image validation must be an image. Hope this laravel image validation jpg not working tutorial will help you. After completing image extension validation in laravel, your concept will be clear about laravel image extension validation.