Image Validation In Laravel
Today, we will learn about Image Validation In Laravel. I will give you a detailed example of how to implement Laravel Image validation. When developing a web application or any type of web 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 large size, unsupported format, etc. But when we use Laravel they provide the most necessary image validations. So let’s start Image Validation In Laravel.
In this tutorial, we can able to check the uploaded image, mimes, minimum size and maximum size, image dimensions height and width, and image dimension by ratio.
Image Validation In Laravel
Blade file
<form method="POST" enctype="multipart/form-data" id="UserForm">
@csrf
<div class="mb-2">
<label for="name" class="form-label">User Image</label>
<input type="file" class="form-control" id="user_image" placeholder="Image" name="user_image">
@if ($errors->has('user_image'))
<span class="text-danger">{{ $errors->first('user_image') }}</span>
@endif
</div>
<button type="submit" class="btn btn-success">Save</button>
</form>
Example 1: Simple Image Validation
In this example, you can see below we added a simple image validation for our request. If haven’t idea how to create a request please use the below command.
php artisan make:request UserImages
<?php
#app\Http\Requests\UserImages.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserImages 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 [
'user_image' => 'required|image'
];
}
}
Example 2: Image Validation with Mimes
In the below example, we can only accept images with png, jpeg, jpg, gif, and bmp extensions.
<?php
#app\Http\Requests\UserImages.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserImages 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 [
'user_image' => 'required|mimes:png,jpeg,jpg,gif,bmp'
];
}
}
Example 3: Image Validation with Size
In the below example, we will determine whether the user-uploaded image is between the specified sizes.
<?php
#app\Http\Requests\UserImages.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserImages 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 [
'user_image' => 'required|image|size:2048' // only 2MB is allowed
];
}
}
Example 4: Image Validation with Dimensions (Height/Width)
Now, add image validation for the dimensions with height and width.
<?php
#app\Http\Requests\UserImages.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserImages 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 [
'user_image' => 'required|image|size:2048|dimensions:min_width=100,min_height=100,max_width=800,max_height=800'
];
}
}
Example 5: Image Validation with Dimensions (Ratio)
We added above the image validation for dimensions with height and width, now let’s add the ratio.
<?php
#app\Http\Requests\UserImages.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UserImages 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 [
'user_image' => 'required|image|size:2048|dimensions:ratio=5/3'
];
}
}
Now you have a basic idea of how to validate the image using Laravel.