Richat CHHAYVANDY
Richat CHHAYVANDY

Reputation: 597

how to check image dimensions before upload in laravel 4

I am new in laravel 4.

I want to upload photo with maximum dimensions 800x600.

Please help to guide me!

Upvotes: 1

Views: 7856

Answers (3)

Sanjaya
Sanjaya

Reputation: 195

Check like below in your form validation.

'avatar' => 'dimensions:min_width=100,min_height=200'

Upvotes: 1

Rafael
Rafael

Reputation: 7746

Check validation like this

if ($validator->passes() && correct_size(Input::file('file'))) {
    // do something
}

Create a validation function like this in which ever model you choose

public function correct_size($photo) {
    $maxHeight = 100;
    $maxWidth = 100;
    list($width, $height) = getimagesize($photo);
    return ( ($width <= $maxWidth) && ($height <= $maxHeight) );
}

you can decide if you want to use it statically or not. But my recommendation is not defining it statically if you would like to check other uploads using the same validation without the dependency.

Upvotes: 2

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you could simple use getImageSize(), as

list($width, $height) = getimagesize(Input::file('file'));

Upvotes: 12

Related Questions