laravel 验证器未验证图像图像尺寸是否正确

laravel validator not validating image image dimensions correct

我正在尝试验证图像的宽度和高度。即使用户 select 分辨率正确的图像也会出错。

控制器代码

$messages = [
    'productImages.dimensions' => "Profile image must be maximum 100x100 "
];
$this->validate($request,[
    'productName'           => 'required|max:40',
    'productDescription'    => 'required|max:1000',
    'productCondition'      => 'required',
    'productImages'         =>     'required|max:5|dimensions:max_width=100,max_height=100',
    'category'              => 'required'
],$messages);

我想在用户 select 正确分辨率的图像时提交该图像。

基于 productImages 是复数并且您设置了 max 值这一事实,我猜您传递的是一组图像,而不是单个图像。因此,您需要分别验证数组中每一项的维度。

$this->validate($request,[
    'productName'           => 'required|max:40',
    'productDescription'    => 'required|max:1000',
    'productCondition'      => 'required',
    'productImages'         => 'required|max:5',
    'productImages.*'       => 'dimensions:max_width=100,max_height=100',
    'category'              => 'required'
],$messages);