Laravel 5.4:如何验证图像大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51334333/
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
Laravel 5.4: How do I validate an image size?
提问by altoids
I have a form where a user can submit an image. I don't want to store it on my server in case it's a virus but instead store it to Amazon S3.
我有一个表单,用户可以在其中提交图像。我不想将它存储在我的服务器上以防它是病毒,而是将它存储到 Amazon S3。
My issue is that I need to validate if the image is less than a specific dimension. How can I do that in Laravel 5.4?
我的问题是我需要验证图像是否小于特定尺寸。我怎样才能在 Laravel 5.4 中做到这一点?
My controller
我的控制器
$validator = \Validator::make($request->all(),[
"logo" => "dimensions:max_width:300,max_height:200",
]);
if($validator->fails()){
return \Redirect::back()->withInput()->withErrors( $validator );
}
The validation fails and redirects back with "The logo has invalid image dimensions." eventhough the image i'm uploading is 100x100. What's the correct way to validate or what are alternate solutions without having to save the file?
验证失败并重定向回“徽标的图像尺寸无效”。尽管我上传的图片是 100x100。验证的正确方法是什么,或者无需保存文件的替代解决方案是什么?
回答by Talha Siddiqi
use equals sign (=) rather than colon (:) with max_width
& max_height
:
使用等号 ( =) 而不是冒号 ( :) 和max_width
& max_height
:
"logo" => "dimensions:max_width=300,max_height=200",
Image dimension validation rules in Laravel
If it doesn't solve, make sure that your form has enctype="multipart/form-data"
(as mentioned in comment by @F.Igor):
如果它没有解决,请确保您的表单具有enctype="multipart/form-data"
(如@F.Igor 的评论中所述):
<form method="POST" enctype="multipart/form-data">
<input type="file" name="logo">
<input type="submit">
</form>
If you already using this, then check (before validation) whether you are getting image with your request and with what name:
如果您已经在使用它,请检查(在验证之前)您是否正在使用您的请求获取图像以及使用什么名称:
dd($request->all())