在 laravel 请求 post 上限制图像大小

Limit the image size on laravel request post

大家好,我需要限制图像的大小,不是文件的大小,而是宽度和高度,到目前为止我有这个:

public function rules()
{
    if(request()->isMethod('put')) // could be patch as well
    {
         // Update rules here - Don't require image here
         return [
            'name' =>                'required',           
            'code' =>                'required|regex:/^#?(([a-f0-9]{3}){1,2})$/i',
            'image' =>               'required|image|max:8192',
        ];

    }
}

有可能

'image' => 'required|image|dimensions:max_width=300,max_height=300'

如文档所述

The file under validation must be an image meeting the dimension constraints as specified by the rule's parameters:

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

Available constraints are: min_width, max_width, min_height, max_height, width, height, ratio.

参考:https://laravel.com/docs/8.x/validation#rule-dimensions