php - Yii2 图像维度验证

标签 php image yii yii2 yii-cactiverecord

此验证行不起作用。我可以上传任何维度的图像。

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

在 Controller 中,我使用。

 $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
],

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

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

关于php - Yii2 图像维度验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35473388/

相关文章:

yii - 在 yii2 表格小部件中显示时合并两列

php - 使用 Yii 设置默认值并创建到数据库中

javascript - 如何从 JQuery 中的表行中提取 PHP 值以供稍后使用?

php - Doctrine 2 - 从 $em->find() 获取 SQL

html - 使用 CSS 和 HTML 最大化网页上的图像

java - 如何通过实例化类来使多个 JButton 单独工作?

javascript - jQuery ajax ".done"回调未触发

php - doctrine2 - 如何提高冲洗效率?

android - 如何使进度条使用图像而不是在 Android 中使用颜色来显示进度

validation - yii2 条件验证在服务器端不起作用?