danielkatic@gmail.com's avatar

Validating an image, Laravel 5

Hi,

how can I make validation to pass only if request image is max 1280x1024?

So far in my form request file i have:

$rules = [ ... 'img' : 'required|mimes:jpeg|image' ];

But i also want to limit the resolution of an image... I'm using Intervention Image to upload image and generate thumbs so maybe it can help me validate?

Thanks.

0 likes
13 replies
deadlockgB's avatar

You can extend the Validation rules with this documentation: http://laravel.com/docs/5.0/validation#custom-validation-rules You therefore would need an Image reading/manipulation package (e.g. https://github.com/Intervention/image) and build rule yourself, like maybe so:

Validator::extend('resolution', function($attribute, $value, $parameters)
{
    // Image is the intervention library with integration into laravel 4/5
    // $value could look like this 1280x1024
    return Image::make(Input::get($attribute))->height() <= explode("x", $value)[0]  && Image::make(Input::get($attribute))->width() <= explode("x", $value)[1];
});

I will make a better writeup later because the above will not work and is just intended as a hint.

danielkatic@gmail.com's avatar

Ok.. Where do i put this Validator::extend method? Sorry but i'm new to Laravel so please explain with more details.. Thanks. I'm already using Intervention/Image so it's no problem..

deadlockgB's avatar

Here another super ugly (I just put everything in routes.php for quick testing) write up with the use of intervention/image:

Validator::extend('resolution', function($attribute, $value, $parameters)
{
    // Image is the intervention library with integration into laravel 4/5
    // $parameters[0] could look like this 1280x1024
    $resX = explode("x", $parameters[0])[0];
    $resY = explode("x", $parameters[0])[1];
    $img = Image::make($value);
    return $img->height() <= $resY  && $img->width() <= $resX;
});

Route::get('image', function()
{
    $validator = Validator::make([
        // here use the path to the uploaded Image
        'image' => public_path() . '\logo.jpg'],
        [
            'image' => 'resolution:1000x300'
        ]);

    if($validator->fails())
    {
        // this should be sticked to the extended validator via the message attributes
        return 'file resolution too large';
    }
        //
        return 'yay';
});

You should propapbly put that Validator::extend to some ValidatorExtensionProvider and register it in you config/app.php

Also I should mention, you need to upload the file to the server therefore. so in the $validator->falis() method you should delete that image again.

kelsen's avatar

New to laravel, me too, the following content just for reference only:
You can also validator it in client use javascript.
The Intervention/image works after the image file uploaded. If the imagefile is largest, that will take lot of time.

danielkatic@gmail.com's avatar

Finally something quick and useful, not to mention painless to use.. Package is working.. Thanks anyway..

rmff's avatar

@sefran I tried to use dimensions, but for me, doesn't work in L 5.2.

'required|dimensions:width=1000,height=200',
'required|dimensions:min_width=1000, max_width=1000, min_height=200, max_height=200',
sefran's avatar

@rmff I've tried only width and height:

                        'original_img' =>'file|image|mimes:jpeg,jpg|max:2000|dimensions:width=1600,height=1000',

and in the validation.php I added a custom validation message:

                       'custom' => [
                                  'original_img' => [
                                                'dimensions' => "The image chosen isn't 1600x1000",
                                  ]
                       ],

Please or to participate in this conversation.