Silverstripe 3 设置图片上传的最小宽度和最小高度 memberprofil 模块

Silverstripe 3 set minimum width and minimum height for image uploads memberprofil module

我正在寻找一种解决方案来实施验证以设置图像上传的最小高度和最小宽度。我认为框架默认不提供这些功能。

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;
    }

保存后,return 出现错误:最小图像尺寸为 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();
    }
}

在您的 updateMemberFormFields 中,您必须切换一些行:

    $Upload->setValidator($validator);
    $fields->push( $Upload = new UploadField('ImageMembre', 'Image membre'));

应该是

    $fields->push( $Upload = new UploadField('ImageMembre', 'Image membre'));
    $Upload->setValidator($validator);

否则你给未定义的对象赋值...

我已经在 link 上查阅了问题和答案,顶部告诉我的问题是重复的... Link。 我意识到我的脚本中有一些错误,我用一些新的编码重新编辑了它。我用其他 post 代码替换了验证函数并且它有效!

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 isImageLargeEnough()
    {
        $imageSize = getimagesize( $this->tmpFile["tmp_name"] );
        if ($imageSize !== false)
        {
            if ( $imageSize[0] < $this->minwidth || $imageSize[1] < $this->minheight )
            {
                return false;
            }
        }       
        return true;
    }

    public function validate() {

        if(!$this->isImageLargeEnough()) {
            //$this->errors[] = 'Image size is not large enough';
         $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();
    }



}