Laravel 5干预中的图像验证

Nee*_*eel 12 laravel laravel-5 laravel-validation intervention laravel-filesystem

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

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

我不明白的是,干预如何处理上传图像的验证?我的意思是,干预是否已经在其中进行了内置图像验证检查,或者是我需要使用Laravel手动添加Validation来检查文件格式,文件大小等的东西.?我已阅读干预文档,但在使用laravel干预时,我无法找到有关图像验证如何工作的信息.

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

Nee*_*eel 16

感谢@maytham的评论,这些评论指出了我正确的方向.

我发现,图像干预本身并没有进行任何验证.所有图像验证必须在传递到图像干预以进行上载之前完成.感谢Laravel内置的验证器imagemime类型,使图像验证变得非常简单.这就是我现在所处的文件输入,然后再将其传递给Image Intervention.

处理干预前的验证员检查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');
    };
 });
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 为什么不使用$ file = $ request-> photo; ? (2认同)

Rah*_*rve 5

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

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