如何在 Laravel 4 上传之前检查图像尺寸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27559393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check image dimensions before upload in laravel 4
提问by Richat CHHAYVANDY
I am new in laravel 4.
我是 Laravel 4 的新手。
I want to upload photo with maximum dimensions 800x600.
我想上传最大尺寸为 800x600 的照片。
Please help to guide me!
请帮助指导我!
回答by Sudhir Bastakoti
you could simple use getImageSize(), as
你可以简单地使用getImageSize(),因为
list($width, $height) = getimagesize(Input::file('file'));
回答by Rafael
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.
您可以决定是否要静态使用它。但我的建议不是静态定义它,如果您想使用相同的验证检查其他上传而没有依赖项。
回答by Sanjaya
Check like below in your form validation.
在您的表单验证中检查如下。
'avatar' => 'dimensions:min_width=100,min_height=200'