Laravel 8 Image Validation Detailed Example
Tags: laravel, tutorial, beginners
Published: 2022-04-22 06:36:12

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/laravel-8-image-validation-detailed-example

In this post, I will show you a detailed example of how to implement Laravel 8 Image validation. When developing a web application or any type of application one of the most important is to allow users to upload images or photos. But we need to validate it before saving it to our storage because it can cause a huge amount of size to our server. Luckily when working it with Laravel they provide the most necessary image validations checking.

 

In Laravel we can able to check the uploaded is image, mimes, minimum size and maximum size, image dimensions height and width, and image dimension by ratio.

 

As you can see above Laravel image validation comes with bundles that we need to check from the uploaded images.

 

## Form Blade Example:


```php
<form method="POST" enctype="multipart/form-data">
    @csrf
    <div class="mb-3">
        <label for="name" class="form-label">Image</label>
        <input type="file" class="form-control" id="image" placeholder="Image" name="image">
        @if ($errors->has('image'))
            <span class="text-danger">{{ $errors->first('image') }}</span>
        @endif
    </div>

    <button type="submit" class="btn btn-primary">Save</button>

</form>
```

## Example 1: Simple Laravel Image Validation
As you can see below we added a simple Laravel image validation for our request.

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreImage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image' => 'required|image'
        ];
    }
}

```

## Example 2: Laravel Image Validation with Mimes
Now, I will show you how to handle the Laravel image validation with mimes. See below code:

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreImage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image' => 'required|mimes:jpeg,png,jpg,gif'
        ];
    }
}
```

As you can see above we can only accept images with jpeg, png, jpg, and gif extensions.

 

## Example 3: Laravel Image Validation with Size

Let's add a Laravel image validation with size checking in this validation we will determine the user uploaded image is between the specified sizes allowed.

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreImage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image' => 'required|image|size:1024' // only 1MB is allowed
        ];
    }
}
```

 

## Example 4: Laravel Image Validation with Dimensions (Height/Width)


Now let's add image validation for the dimensions with height and width.

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreImage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image' => 'required|image|size:1024||dimensions:min_width=100,min_height=100,max_width=1000,max_height=1000'
        ];
    }
}
```

## Example 5: Laravel Image Validation with Dimensions (Ratio)

Since we added above the Laravel image validation for dimensions with height and width, now let's add the ratio.

```php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreImage extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'image' => 'required|image|size:1024|dimensions:ratio=3/2'
        ];
    }
}
```
Now you have a basic idea already on how to validate the image using Laravel 8.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-image-validation-detailed-example if you want to download this code.

Happy coding :)