在Laravel 5干预图像验证

问题描述 投票:10回答:4

我在Laravel 5.1安装intervention和我使用的图片上传和调整是这样的:

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

我不明白什么是,如何做介入过程上传的图片验证?我的意思是,不干预已经有inbuild图像验证检查,它或者是说一些我需要手动使用Laravel Validation来检查文件格式,文件大小等加..?我已经通过干预文档阅读,我无法找到的图像验证使用干预laravel时如何工作的信息。

有人能指出我在正确的方向,请..

laravel laravel-5 laravel-validation intervention laravel-filesystem
4个回答
16
投票

由于@maytham对他的评论指出这我在正确的方向。

我发现了什么是图像干预本身并不做任何确认。所有图像验证便是其传递到影像干预上传之前做过的事情。由于Laravel的内置校验像imagemime类型,这使得图像验证很容易。这是我现在在哪里,我在把它传递给图片干预前先验证文件的输入。

验证检验处理干预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');
    };
 });

希望这可以帮助。


5
投票

简单地说,整合这得到验证

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

1
投票

我有自定义表单,而这个变种不起作用。所以我用正则表达式验证

像这样:

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

可能这将是有帮助的人


1
投票

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

可读的图像格式取决于所选择的驱动器(GD或Imagick)和本地配置。默认情况下介入影像目前支持以下主要格式。

    JPEG PNG GIF TIF BMP ICO PSD WebP

GD✔️✔️✔️ - - - - ✔️*

Imagick✔️✔️✔️✔️✔️✔️✔️✔️*

  • 对于WebP的支持GD驾驶员必须用PHP 5> = 5.5.0或PHP 7,以便被用于使用imagewebp()。如果使用Imagick,它必须与libwebp对WebP的支持下重新编译。

见化妆方法的文档,以了解如何从不同的源中读取的图像格式,分别编码并保存,以了解如何输出图像。

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

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

提示1:(请求确认)

$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:(控制器验证)

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

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }
© www.soinside.com 2019 - 2024. All rights reserved.