首页 > 解决方案 > 如何验证 Laravel Backpack 中图片的权重?

问题描述

我正在使用backpackforlaravel(“backpack/crud”:“3.5.*”),这很棒。我做了一个图像 CRUD 部分。我有一个使用以下代码的请求文件验证。

return [
            'name' => 'required|min:5|max:255',
            'code' => 'required|min:2|max:20',
            'image' => 'required|mimes:jpeg,png,jpg,gif|max:2048',
        ];

在我的控制器中,我有这个代码

$this->crud->addField([ // image
            'label' => "Bandera",
            'name' => "image",
            'type' => 'image',
            'upload' => true,
            'crop' => false, // set to true to allow cropping, false to disable
            'aspect_ratio' => 1, // ommit or set to 0 to allow any aspect ratio
            'mime_types' => ['image'],
            'filesize'      => 5,
            // 'disk' => 's3_bucket', // in case you need to show images from a different disk
            // 'prefix' => 'uploads/images/profile_pictures/' // in case your db value is only the file name (no path), you can use this to prepend your path to the image src (in HTML), before it's shown to the user;
        ]);

现在它显示图像必须是以下类型的文件:jpeg、png、jpg、gif。图片不得超过 2048 个字符。

这是验证图像重量作为字符串,我需要一个正常的重量和类型验证

标签: phplaravelvalidationlaravel-backpack

解决方案


使用该验证器数组时,您正在正确验证图像。

  • 验证mimes:jpeg,png,jpg,gif检查它是否是图像。
  • 验证max:2048对文件大小设置了最大值。

推荐阅读