我正在寻找一种解决方案来实现验证,以便为图片上传设置最小高度和最小宽度。我认为框架默认不提供这些功能。
Example : Allowed image upload minimum width: 500px and minimum height: 500px
我的代码扩展了成员数据对象:
member_extension.php
require_once 'ImageUpload_Validator.php';
class MemberExtension extends DataExtension {
private static $has_one = array(
'ImageMembre' => 'Image'
);
public function updateMemberFormFields(FieldList $fields) {
//setup the new validator
$validator = new ImageUpload_Validator();
$validator->setMinDimensions(500,500);
$validator->setAllowedExtensions(array('jpg','jpeg', 'gif', 'png'));
//$validator->setAllowedMaxFileSize(array('*' => 4194304));
$fields->push( $Upload = new UploadField('ImageMembre', 'Image membre'));
$Upload->setValidator($validator);
}
function updateCMSFields(FieldList $fields) {
// $fields = parent::getCMSFields();
$uploadfield = new UploadField('ImageMembre','Image Membre');
//setup the new validator
$validator = new ImageUpload_Validator();
$validator->setMinDimensions(500,500);
$validator->setAllowedExtensions(array('jpg','jpeg', 'gif', 'png'));
//$validator->setAllowedMaxFileSize(array('*' => 4194304));
$fields->addFieldToTab('Root.Main',$uploadfield);
$uploadfield->setValidator($validator);
return $fields;
}
保存后,它返回错误:最小图像尺寸为500px x 500px,但我的图像有更多分辨率。
我认为脚本中有错误:
ImageUpload_Validator.php
class ImageUpload_Validator extends Upload_Validator{
public $minwidth;
public $minheight;
public function setMinDimensions($width,$height){
if(is_numeric($width) && intval($width)>=0)
$this->minwidth=intval($width);
else
user_error('Invalid minimum width, value must be numeric and at least 0',E_USER_ERROR);
if(is_numeric($height) && intval($height)>=0)
$this->minheight=intval($height);
else
user_error('Invalid minimum height, value must be numeric and at least 0',E_USER_ERROR);
}
public function isValidDimensions() {
//if we cannot determine the image size return false
if(!$dims = getimagesize($this->tmpFile['tmp_name']))
return false;
if(($this->minheight && $dims[1]<=$this->minheight) || ($this->minwidth && $dims[0]<=$this->minwidth))
return false;
return true;
}
public function validate(){
if(!isset($this->tmpFile['name']) || empty($this->tmpFile['name']))
return true;
if(!$this->isValidDimensions()){
$this->errors[]=sprintf('Minimum image size is %s x %s ', $this->minwidth?$this->minwidth.'px':'(ANY)',$this->minheight?$this->minheight.'px':'(ANY)');
return false;
}
return parent::validate();
}
}
答案 0 :(得分:1)
在updateMemberFormFields中,您必须切换一些行:
$Upload->setValidator($validator);
$fields->push( $Upload = new UploadField('ImageMembre', 'Image membre'));
应该是
$fields->push( $Upload = new UploadField('ImageMembre', 'Image membre'));
$Upload->setValidator($validator);
否则你将一些东西分配给一个未定义的对象......
答案 1 :(得分:0)
我已经在链接上查询了问题和回答,告诉我的问题是重复...... Link。 我在脚本中发现了一些错误,并且我用一些新的编码重新编写了它。我用其他邮政编码替换了验证功能,它可以工作!
Connector