Reader Stacks

Validating Images in Laravel: Dimensions, MIME Types, and Size

Beyond the basic image rule, Laravel can validate exact or ratio-based dimensions, restrict specific formats, and limit file size — all without touching the image itself in application code.

Validating Images in Laravel: Dimensions, MIME Types, and Size

Laravel's validation rules for images go well beyond the basic "is this an image" check — dimension constraints, specific format restrictions, and size limits are all expressible declaratively in the validation rule array, without touching the actual image file in your own application code.

Basic image validation

$request->validate([
    'photo' => 'required|image',
]);

image confirms the uploaded file is actually one of the formats PHP's GD or Imagick libraries recognize as a valid image (jpeg, png, bmp, gif, svg, webp) — beyond just checking a file extension.

Restricting to specific formats

$request->validate([
    'photo' => 'required|image|mimes:jpeg,png,webp',
]);

mimes restricts to a specific allow-list of formats — useful when, say, animated GIFs or SVGs (both valid under the general image rule) aren't appropriate for a specific upload field, like a profile photo.

Limiting file size

$request->validate([
    'photo' => 'required|image|max:2048', // max is in kilobytes
]);

Requiring exact dimensions

$request->validate([
    'banner' => 'required|image|dimensions:width=1200,height=400',
]);

Requiring dimensions within a range

$request->validate([
    'photo' => 'required|image|dimensions:min_width=200,min_height=200,max_width=2000,max_height=2000',
]);

Requiring a specific aspect ratio

$request->validate([
    'banner' => 'required|image|dimensions:ratio=3/1',
]);

The ratio constraint is genuinely useful for something like a banner image that needs a consistent aspect ratio across different actual pixel sizes, without needing to pin down one exact width and height — a 1200x400 and a 900x300 image would both satisfy a 3/1 ratio requirement.

Combining multiple dimension constraints

$request->validate([
    'avatar' => [
        'required',
        'image',
        'mimes:jpeg,png',
        'max:2048',
        Rule::dimensions()->minWidth(200)->minHeight(200)->ratio(1/1),
    ],
]);

The fluent Rule::dimensions() builder is a more readable alternative to the string-based dimension syntax once several dimension constraints need combining, avoiding a long, comma-packed string that's harder to read at a glance.

Custom validation messages for image-specific rules

$request->validate([
    'banner' => 'required|image|dimensions:ratio=3/1',
], [
    'banner.dimensions' => 'The banner must have a 3:1 aspect ratio (e.g., 1200x400 pixels).',
]);

The default dimension validation error message doesn't explain the actual required ratio or size in a way most users would understand — a custom message spelling out the specific expected dimensions in plain language genuinely improves the upload experience over the generic default.

Why server-side image validation is essential regardless of client-side checks

Following the same principle covered for client-side form validation generally on this site, any dimension or size checks done in JavaScript before upload are a UX convenience only — the server-side rules shown here are what actually prevent an invalid or maliciously crafted file from being accepted and stored.