【问题标题】:Laravel: Adding video dimension validationLaravel:添加视频尺寸验证
【发布时间】:2018-03-13 15:13:00
【问题描述】:

我知道我们可以在 laravel 中为图片添加尺寸验证,

$validator = Validator::make($request->all(), 
        [
            'banner' => 'bail
                        |image
                        |mimes:jpeg,png,jpg,gif,svg
                        |max:7000
                        |dimensions:ratio=170/63
                        |dimensions:min_width=510,min_height=189'
        ]
    );

我已经为视频尝试了这些尺寸规则,但它似乎不起作用。

视频是否可以达到相同的效果?

【问题讨论】:

    标签: php validation laravel-5 laravel-5.5


    【解决方案1】:

    制定自己的规则怎么样?存在一个通过 composer 读取视频文件元数据的库,名为 getID3。

    安装它:

    composer require james-heinrich/getid3
    

    创建自定义规则类:

    php artisan make:rule VideoDimension
    

    借助 getid3 创建规则的逻辑:

    <?php
    
    namespace App\Rules;
    
    use Illuminate\Contracts\Validation\Rule;
    
    class VideoDimension implements Rule
    {
        protected $maxWidth;
        protected $maxHeight;
    
        public function __construct($maxWidth, $maxHeight)
        {
            $this->maxWidth = $maxWidth;
            $this->maxHeight = $maxHeight;
        }
        /**
         * Determine if the validation rule passes.
         *
         * @param  string  $attribute
         * @param  mixed  $value
         * @return bool
         */
        public function passes($attribute, $value)
        {
            $getID3 = new getID3;
    
            // the value is an instance of UploadedFile
            $file = $getID3->analyze($value->getRealPath());
    
            $passes = true;
    
            if ($this->maxWidth < $file['video']['resolution_x']
                || $this->maxHeight < $file['video']['resolution_y']){
                $passes = false;
            }
    
            return $passes;
        }
    
        /**
         * Get the validation error message.
         *
         * @return string
         */
        public function message()
        {
            return 'The :attribute excess the dimensions.';
        }
    }
    

    最后,应用规则:

    $validator = Validator::make($request->all(), 
        [
            'video' => ['bail',
                        'file',
                        'max:7000',
                        new VideoDimension(400, 600)]
        ]
    );
    

    希望这个例子能帮助你弄清楚如何完成你的任务。

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 2020-08-04
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多