how to check image dimensions before upload in laravel 4

ぐ巨炮叔叔 提交于 2020-01-23 03:34:05

问题


I am new in laravel 4.

I want to upload photo with maximum dimensions 800x600.

Please help to guide me!


回答1:


you could simple use getImageSize(), as

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



回答2:


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.




回答3:


Check like below in your form validation.

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


来源:https://stackoverflow.com/questions/27559393/how-to-check-image-dimensions-before-upload-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!