【问题标题】:Silverstripe 3 set minimum width and minimum height for image uploads memberprofil moduleSilverstripe 3 设置图片上传的最小宽度和最小高度 memberprofil 模块
【发布时间】: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


【解决方案1】:

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

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

应该是

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

否则,您将某些内容分配给未定义的对象...

【讨论】:

  • 我的反应太快了!当我点击保存时,它会从 cms 返回验证错误并显示最小宽度和高度问题......你能再帮忙吗?谢谢!
【解决方案2】:

我已经在链接上咨询了问题和答案,告诉我我的问题是重复的...Link。 我意识到我的脚本中有一些错误,我用一些新的编码重新编辑了它。我已经用其他邮政编码替换了验证功能,它可以工作!

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



}

【讨论】:

    猜你喜欢
    • 2016-03-30
    • 2012-05-27
    • 1970-01-01
    • 2016-03-08
    • 2010-12-28
    • 2018-11-08
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    相关资源
    最近更新 更多