Yii2 图像维度验证

Yii2 image dimension validation

此验证行无效。我可以上传任意尺寸的图片。

['image', 'image', 'minWidth' => 250, 'maxWidth' => 250,'minHeight' => 250, 'maxHeight' => 250], 

在控制器中,我使用。

 $image = UploadedFile::getInstance($this, 'image');

就我所见,最后一行没有任何问题。 https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-core-validators.md#yiivalidatorsimagevalidatorimage-

但是您为 image 属性声明了两次规则 - 一次作为文件,一次作为图像。图像验证器扩展自文件验证器,因此它继承了它的所有属性。

Image Validator (docs):

This validator checks if the input value represents a valid image file. It extends from the file validator and thus inherits all its properties. Besides, it supports the following additional properties specific for image validation purpose:

尝试将其合并为一条规则,看看是否有帮助。

[
     'image', 
     'image', 
     'minWidth' => 250, 
     'maxWidth' => 250,
     'minHeight' => 250, 
     'maxHeight' => 250, 
     'extensions' => 'jpg, gif, png', 
     'maxSize' => 1024 * 1024 * 2
],

编辑: 如果您在控制器中,您需要将图像保存在 $model 中,例如 $model->image,以便通过模型验证规则对其进行验证。

这是一个很好的例子: http://www.yiiframework.com/doc-2.0/guide-input-file-upload.html