【发布时间】:2016-02-09 00:06:52
【问题描述】:
我正在寻找一种解决方案来实施验证以设置图像上传的最小高度和最小宽度。我认为框架默认不提供这些功能。
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();
}
}
【问题讨论】:
-
嗨 wmk,不是完全重复...我指的是当我尝试将我的代码包含到 updateMemberFormFields 中时返回错误 500 的 memberprofil 模块。我在 CMS 中的自定义验证工作正常。有我用于自定义验证的链接:link
-
那么您已经运行了验证并希望将其添加到 MemberProfilePage?
-
是的,但是当我尝试将它添加到 updateMemberFormFields 函数中时它返回错误 500...
-
你能分享一些代码和/或挖掘错误日志中的错误吗?
标签: image validation size silverstripe