Use Rule::dimensions() in validation rules to enforce exact pixel dimensions, minimum thresholds, and aspect ratios on uploaded images.
Validating avatar uploads or banner images requires ensuring images meet visual layout requirements before storing them on disk.
Laravel provides the fluent Rule::dimensions() helper.
Basic Usage in Form Requests
use Illuminate\Validation\Rule;
public function rules(): array
{
return [
// Enforce minimum and maximum pixel boundaries
'avatar' => [
'required',
'image',
Rule::dimensions()
->minWidth(200)
->minHeight(200)
->maxWidth(2000)
->maxHeight(2000),
],
// Enforce exact 16:9 banner aspect ratio
'banner' => [
'required',
'image',
Rule::dimensions()
->minWidth(1200)
->ratio(16 / 9),
],
];
}
Square Dimensions Shorthand
For square profile pictures, enforce a 1:1 aspect ratio:
Rule::dimensions()->ratio(1 / 1)->minWidth(100);
Summary
- Validates image dimensions using PHP's native image inspection before processing.
- Supports
minWidth(),maxWidth(),minHeight(),maxHeight(),width(),height(), andratio(). - Prevents layout breaks caused by disproportionate uploaded media.
Related Tips
View all tips →Verify Existing User Credentials with the current_password Validation Rule
Use the current_password validation rule to require users to confirm their existing password before performing sensitive account actions.
Stop Validating on First Attribute Failure with the bail Rule
Add the bail rule to validation rules to stop executing subsequent expensive checks (like database queries) as soon as the first validation rule fails.