ItSolutionStuff.com

Laravel Image Dimension Validation Rules Example

By Hardik Savani • April 16, 2024
Laravel

Laravel provide new image dimensions validation option for image upload and you are able to use this dimensions validation in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. In this validation rules we can set several rules like as listed bellow:

Dimensions Rules:

1)width

2)height

3)min_width

4)min_height

5)max_width

6)max_height

7)ratio

In this Dimensions option through we can set fix width and height, if invalid width and height of image then it will return error. Same way we can set validation min and max height width on your validation.

Few days ago i posted image upload with validation post in Laravel 5.3, you can see here : Laravel 5.3 Image Upload with Validation example.

In this post i used simple validation with mime type and max size like as bellow :

$this->validate($request, [

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

]);

You can also replace your validation using Dimensions rules 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:min_width=350,min_height=600',

]);

Example 3:

$this->validate($request, [

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:max_width=350,max_height=600',

]);

Example 4:

$this->validate($request, [

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:ratio=3/2',

]);

You can check this rules with image uploading post...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Password and Confirm Password Validation Example

Read Now →

Laravel 10 Image Validation Rule Example

Read Now →

Laravel 10 Custom Validation Rule Example

Read Now →

Laravel 10 Ajax Form Validation Example Tutorial

Read Now →

Laravel Carbon Time Format AM PM Example Code

Read Now →

Laravel Money/Currency Format Example

Read Now →

Laravel React JS Form Validation Example

Read Now →

Laravel Store JSON Format Data in Database Example

Read Now →

Laravel Multi Step Form Example Tutorial

Read Now →

Laravel URL Validation Rule Example

Read Now →

Laravel Form Validation Request Class Example

Read Now →

Laravel Bail Rule | Stop Validation On First Failure

Read Now →

How to Use Date Format Validation in Laravel?

Read Now →

Laravel Validation Check If Value is Not Equal to a Another Field

Read Now →