How to make image dimension validation rules in Laravel ?

Today now in this post i will show you How to make image dimension validation rules in Laravel ? We know Laravel is provide new image dimensions validation option for the image upload and we are able to use this dimensions validation in any version laravel 6, laravel 7 and laravel 8 . In this validation rules you can set several rules as like as listed bellow:

Dimensions Rules:

  1. width
  2. height
  3. min_width
  4. min_height
  5. max_width
  6. max_height
  7. ratio

Now in this Dimensions option through we can easily set the fix width and also height, if input invalid width and height of image then it will be return an error. And also on the same way we can set the validation min and max height width on our validation.

Now in this post i will use simple validation also with mime type and also max size as like as bellow :

$this->validate($request, [
    'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

We can also replace our validation by using Dimensions rules as like as bellow:

Example 1:
$this->validate($request, [
    'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:width=500,height=500',
Example 2:
$this->validate($request, [
    'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:ratio=3/2',
Example 3:
$this->validate($request, [
    'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:min_width=350,min_height=600',
Example 4:
$this->validate($request, [
    'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:max_width=350,max_height=600',

Read Also: How to implement infinite ajax scroll pagination in Laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook