Laravel 8 Image Validation Detailed Example

codeanddeploy

Code And Deploy

Posted on April 22, 2022

Laravel 8 Image Validation Detailed Example

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:

<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>
Enter fullscreen mode Exit fullscreen mode

Example 1: Simple Laravel Image Validation

As you can see below we added a simple Laravel image validation for our request.

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

Enter fullscreen mode Exit fullscreen mode

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

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'
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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

<?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'
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

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

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'
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

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 :)

💖 💪 🙅 🚩
codeanddeploy
Code And Deploy

Posted on April 22, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related