Laravel 5 干预中的图像验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31893439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:05:16  来源:igfitidea点击:

Image Validation in Laravel 5 Intervention

laravellaravel-5laravel-validationinterventionlaravel-filesystem

提问by Neel

I have installed interventionin Laravel 5.1 and I am using the image upload and resize something like this:

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

Route::post('/upload', function()
{
Image::make(Input::file('photo'))->resize(300, 200)->save('foo.jpg');
});

What I dont understand is, how does intervention process the validation for the uploaded image? I mean, does intervention already has the inbuild image validation check in it or is that something I need to manually add using Laravel Validationto check for the file formats, file sizes, etc..? I have read through the intervention docs and I couldnt find info on how image validation works when using intervention with laravel.

我不明白的是,干预如何处理上传图像的验证?我的意思是,干预是否已经进行了内置图像验证检查,还是我需要使用 Laravel 手动添加Validation以检查文件格式、文件大小等。?我已经通读了干预文档,但找不到有关在使用 laravel 干预时图像验证如何工作的信息。

Can someone point me in the right direction please..

有人可以指出我正确的方向吗..

回答by Neel

Thanks to @maytham for his comments which pointed me in the right direction.

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

What I found out is that the Image Intervention does not do any Validation by itself. All Image Validation has to be done before before its passed over to Image intervention for uploading. Thanks to Laravel's inbuilt Validators like imageand mimetypes which makes the image validation really easy. This is what I have now where I am validating the file input first before passing it over to Image Intervention.

我发现图像干预本身不进行任何验证。所有图像验证都必须在将其传递给图像干预进行上传之前完成。感谢 Laravel 内置的 Validators likeimagemimetypes,这使得图像验证变得非常容易。这就是我现在所拥有的,我首先验证文件输入,然后再将其传递给 Image Intervention。

Validator Check Before Processing Intervention ImageClass:

处理干预Image类之前的验证器检查:

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

Hope this helps.

希望这可以帮助。

回答by Rahul Hirve

Simply, Integrate this to to get validation

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

$this->validate($request, ['file' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',]);

回答by venkatskpi

Image Supported Formatshttp://image.intervention.io/getting_started/formats

图像支持的格式http://image.intervention.io/getting_started/formats

The readable image formats depend on the chosen driver (GD or Imagick) and your local configuration. By default Intervention Image currently supports the following major formats.

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

    JPEG PNG GIF TIF BMP ICO PSD WebP

GD ?? ?? ?? - - - - ?? *

GD ?? ?? ?? - - - - ?? *

Imagick ?? ?? ?? ?? ?? ?? ?? ?? *

幻术???? ?? ?? ?? ?? ?? ?? *

  • For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support.
  • 对于 WebP 支持,GD 驱动程序必须与 PHP 5 >= 5.5.0 或 PHP 7 一起使用才能使用 imagewebp()。如果使用 Imagick,则必须使用 libwebp 对其进行编译以支持 WebP。

See documentation of make method to see how to read image formats from different sources, respectively encode and save to learn how to output images.

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

NOTE: (Intervention Image is an open source PHP image handling and manipulation libraryhttp://image.intervention.io/). This library does not validate any validation Rules, It was done by Larval Validator class

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

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

Laravel 文档https://laravel.com/docs/5.7/validation

Tips 1:(Request validation)

技巧一:(请求验证)

$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

Tips 2:(controller validation)

技巧二:(控制器验证)

   $validator = Validator::make($request->all(), [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }

回答by Alex Semenov

i have custum form, and this variant does not work. So i used regexp validation

我有习惯形式,这个变体不起作用。所以我使用了正则表达式验证

like this:

像这样:

  client_photo' => 'required|regex:/^data:image/'

may be it will be helpful for someone

可能对某人有帮助