首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 5干预中的图像验证

Laravel 5干预中的图像验证
EN

Stack Overflow用户
提问于 2015-08-08 13:06:43
回答 5查看 135.8K关注 0票数 21

我已经在Laravel5.1中安装了干预,我正在使用图像上传并调整大小,如下所示:

代码语言:javascript
复制
Route::post('/upload', function()
{
Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');
});

我不明白的是,干预是如何对上传的图像进行验证的?我的意思是,干预中是否已经有内置图像验证检查,还是需要使用Laravel Validation手动添加一些内容来检查文件格式、文件大小等?我已经阅读了干预文档,我找不到图像验证如何工作时,使用干预与拉里。

谁能给我指明正确的方向吗..。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-08-09 13:31:02

感谢@maytham,他的评论为我指明了正确的方向。

我发现图像干预本身并没有进行任何验证。所有的图像验证都必须在其传递到图像干预上传之前完成。多亏了Laravel的内置验证器(如imagemime类型),这使得图像验证变得非常容易。这就是我现在要先验证文件输入的地方,然后再将它传递给图像干预。

处理干预前的Image 类:

代码语言:javascript
复制
 Route::post('/upload', function()
 {
    $postData = $request->only('file');
    $file = $postData['file'];

    // Build the input for validation
    $fileArray = array('image' => $file);

    // Tell the validator that this file should be an image
    $rules = array(
      'image' => 'mimes:jpeg,jpg,png,gif|required|max:10000' // max 10000kb
    );

    // Now pass the input and rules into the validator
    $validator = Validator::make($fileArray, $rules);

    // Check to see if validation fails or passes
    if ($validator->fails())
    {
          // Redirect or return json to frontend with a helpful message to inform the user 
          // that the provided file was not an adequate type
          return response()->json(['error' => $validator->errors()->getMessages()], 400);
    } else
    {
        // Store the File Now
        // read image from temporary file
        Image::make($file)->resize(300, 200)->save('foo.jpg');
    };
 });

希望这能有所帮助。

票数 42
EN

Stack Overflow用户

发布于 2017-12-28 04:35:11

简单地说,将其集成以获得验证。

代码语言:javascript
复制
$this->validate($request, ['file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);
票数 16
EN

Stack Overflow用户

发布于 2019-02-12 07:00:53

图像支持格式 已启动/格式

可读图像格式取决于选择的驱动程序(GD或Imagick)和本地配置。默认情况下,干预图像当前支持以下主要格式。

代码语言:javascript
复制
    JPEG PNG GIF TIF BMP ICO PSD WebP

GD✔️----✔️*

Imagick✔️✔️*

  • 对于WebP支持,为了使用imagewebp(),必须在PHP5、>= 5.5.0或PHP7中使用GD驱动程序。如果使用Imagick,则必须使用libwebp编译它以支持WebP。

请参阅make方法的文档,以了解如何从不同来源读取图像格式,分别编码和保存以了解如何输出图像。

注意:(干预映像是一个开源的PHP映像处理和操作库 http://image.intervention.io/)。此库不验证任何验证规则,它是由幼体Validator类完成的。

Laravel Doc https://laravel.com/docs/5.7/validation

提示1: (请求验证)

代码语言:javascript
复制
$request->validate([
   'title' => 'required|unique:posts|max:255',
   'body' => 'required',
   'publish_at' => 'nullable|date',
]); 

// Retrieve the validated input data...
$validated = $request->validated(); //laravel 5.7

技巧2: (控制器验证)

代码语言:javascript
复制
   $validator = Validator::make($request->all(), [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31893439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档