加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Yii2图像尺寸验证

发布时间:2020-12-13 17:49:49 所属栏目:PHP教程 来源:网络整理
导读:此验证行不起作用.我可以上传任何尺寸的图像. ['image','image','minWidth' = 250,'maxWidth' = 250,'minHeight' = 250,'maxHeight' = 250], 在控制器中,我使用. $image = UploadedFile::getInstance($this,'image'); 解决方法 就我所见,最后一行没有任何问
此验证行不起作用.我可以上传任何尺寸的图像.

['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 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','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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读