Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sagar001's avatar

validation not working properly.. Help!

I am Trying to validate the dimension of an image.. if either width or height is greater or equals to 1080px i need to pass the validation.. but my code is not working properly.. can someone point out where is the mistake.. Thanks in advanced :)

$image = $request->file('file');
list($width,$height) = getimagesize($image);

$validator = \Validator::make($request->all(), [
    'file' => ['required',
        'image',
        'dimension' => function ($width,$height) {
            if($width >= 1080 || $height >= 1080){
                return true;
            } else{
                return false;
            }
        }
    ],
]);

if ($validator->fails()) {
    return response()->json($validator->errors(), 422);
}
0 likes
15 replies
jlrdw's avatar

Have you tried max, i.e.,

...max_height=1080...
Nakov's avatar

@jlrdw he should try min_height=1080 because his condition is >= which means bigger or equal.

So basically this:

'dimension' => function ($width,$height) {
            if($width >= 1080 || $height >= 1080){
                return true;
            } else{
                return false;
            }
        }

Can be replaced by this:

'file' => 'dimensions:min_width=1080,min_height=1080'
Sinnbeck's avatar

I believe that the min_width=1080,min_height=1080 would checked as and instead of or

Meaning it needs to be both 1080 in width and height, not just one dimension.

1 like
sagar001's avatar

Yaa guys i tried max_width and min_width option... But not working as axpected... I want to pass the validation.. If one of them (width, height) is 1080px. Thanks for the replies.

Snapey's avatar

try

$validator = \Validator::make($request->all(), [
    'file' => ['required',
        'image',
        function ($attribute, $value, $fail) {

            list($width,$height) = getimagesize($attribute);
            
            if($width < 1080 && $height < 1080) {
                $fail($attribute . ' must be at least 1080 in any direction');
            }

        }
    ],
]);

2 likes
sagar001's avatar

Thanks for the code... i tried it but got this error - getimagesize(file): failed to open stream: No such file or directory further i change the code to

list($width,$height) = getimagesize($request->file('file'));

but still got the error - Undefined variable: request do u have any idea about this error.

Sinnbeck's avatar

You need to use a variable to be able to access it

function ($attribute, $value, $fail) use ($request) {
1 like
sagar001's avatar

I change the code as below... but now it's not give any error.. just passed the validation even for the wrong dimension image.

function ($attribute, $value, $fail) use ($request){
    list($width,$height) = getimagesize($request->file('file'));
    if($width > 1080 && $height > 1080) {
        $fail($attribute . ' must be at least 1080 in any direction');
    }
}
Sinnbeck's avatar

You changed from < to > in the if statement

That means it only fails if both are too big. Look at the example by @snapey

1 like
sagar001's avatar
sagar001
OP
Best Answer
Level 1

UPDATED

Thanks Guys [@Sinnbeck | @snapey | @nakov | @jlrdw ] Solve it... here is the full code

$validator = \Validator::make($request->all(), [
    'file' => ['required',
            'image',
            'mimes:jpeg,jpg,png',
            'max:2048',
            function ($attribute, $value, $fail) use ($request){
                list($width,$height) = getimagesize($request->file('file'));
                if($width < 1080 && $height < 1080) {
                    $fail($attribute . ' must be at least 1080 in any direction');
                }
             }
            ],
]);
Snapey's avatar

Your answer

a) is mostly my reply

b) does not match your request because you said that the image had to be at least 1080 in one direction only. Your current code checks that the image is at least 1080 in both directions.

For the example you have now, you could have just used min-height and min-width

sagar001's avatar

a) You Help The Most Thank You..

b) Updated the code. replace || to &&

thank You once again :)

Please or to participate in this conversation.