【问题标题】:Validating fileupload(image Dimensions) in Backend Octobercms在后端 Octobercms 中验证文件上传(图像尺寸)
【发布时间】:2016-08-28 16:12:05
【问题描述】:

我正在创建一个插件,我在后端使用文件上传字段类型。我有一个现实 $attachOne,我需要验证图像尺寸(高度和宽度),有没有办法做到这一点?

【问题讨论】:

    标签: laravel validation file-upload octobercms


    【解决方案1】:

    您需要向模型添加验证逻辑。其中关系称为somerelation,在模型类中定义这样的方法:

    public function beforeValidate()
    {
        $file = $this->somerelation()->withDeferred($this->sessionKey)->first();
        $filename = $file->getLocalPath();
        list($width, $height) = getimagesize($filename);
    
        if ($width < 800) {
            throw new ValidationException(['somerelation' => 'Width must be greater than 800']);
        }
    }
    

    要使此方法覆盖起作用,请确保模型已使用 October\Rain\Database\Traits\Validation 特征。因为它只发生在后端,快速检查App::runningInBackend() 应该可以解决问题。

    【讨论】:

      【解决方案2】:

      使用它会给出图像的宽度和高度。

      list($width, $height) = getimagesize($filename);
      

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 2020-02-22
        • 2015-09-20
        • 2018-09-22
        • 1970-01-01
        相关资源
        最近更新 更多