Reader Stacks

File and Image Validation in Laravel

The image rule alone only checks the file extension and MIME type — dimensions, aspect ratio, and a genuinely minimum file size all need their own separate, explicit rules layered on top.

File and Image Validation in Laravel

Laravel's file and image validation rules cover the basics — but genuinely thorough validation (dimensions, aspect ratio, precise size limits) needs several additional rules layered on top of the base check.

Basic file validation

$request->validate([
    'document' => 'required|file|mimes:pdf,doc,docx|max:10240',
]);

max:10240 is specified in kilobytes — this limit here is 10MB, and it works alongside (not instead of) the server's own upload_max_filesize/post_max_size php.ini settings, both of which need to independently allow a file this large for the validation rule to ever even get a chance to run.

Basic image validation

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

The image rule alone validates that the file is a genuine image and restricts it to common formats (jpeg, png, bmp, gif, svg, webp) — but by itself, it checks only the file type, not its dimensions, aspect ratio, or an exact minimum size.

Validating specific dimensions

$request->validate([
    'photo' => [
        'required',
        'image',
        Rule::dimensions()->maxWidth(2000)->maxHeight(2000),
    ],
]);

Validating an exact aspect ratio

Rule::dimensions()->ratio(16 / 9)
Rule::dimensions()->ratio(1) // a perfect square, e.g. for a profile avatar

Enforcing a specific aspect ratio is genuinely useful for content with a fixed display area (a banner image, a square avatar) where an unexpectedly shaped upload would otherwise get cropped or distorted awkwardly by the front-end layout.

Validating a minimum dimension

Rule::dimensions()->minWidth(400)->minHeight(400)

A minimum dimension check is worth adding for content that will be displayed at a specific larger size (like a hero banner) — an uploaded image smaller than that display size would otherwise appear blurry or pixelated when scaled up to fit.

Combining multiple dimension constraints together

$request->validate([
    'banner' => [
        'required',
        'image',
        'mimes:jpeg,png,webp',
        Rule::dimensions()
            ->minWidth(1200)
            ->minHeight(400)
            ->ratio(3 / 1),
    ],
]);

A custom validation rule for a genuinely minimum file size

$request->validate([
    'photo' => ['required', 'image', 'min:50'], // at least 50KB
]);

min: on a file input checks the file's minimum size in kilobytes — genuinely useful for catching a suspiciously tiny "image" that might actually be a corrupted or placeholder file rather than real photographic content.

Validating multiple images, following the array validation pattern

$request->validate([
    'photos' => 'required|array|max:10',
    'photos.*' => 'image|mimes:jpeg,png|max:2048',
]);

Following the array-wildcard validation pattern covered elsewhere on this site, this applies the same image rules independently to every file in a multi-file upload, with photos itself capped at a maximum of 10 files total.