4

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

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

我不什么不解的是,如何做干预过程的验证上传的图片?我的意思是,干预已经有inbuild图像验证检查,或者是我需要手动添加使用Laravel Validation来检查文件格式,文件大小等。我已阅读干预文档,并且我无法找到使用laravel进行干预时图像验证如何工作的信息。

有人点我在正确的方向,请..

+1

你可能需要看看这个https://laracasts.com/discuss/channels/general-discussion/validating-an-image-laravel-5和这个http://tutsnare.com/upload-files-in-laravel/据我所知干预中没有内容验证。 –

+0

谢谢@maytham您的评论当然帮助我指出了正确的方向。我已经发布了我现在使用的解决方案。 :) – Neel

+0

不客气,这里是我的投票也。 –

回答

10

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

我发现的是图像干涉本身不会进行任何验证。所有图像验证必须在传递到Image上传之前完成。感谢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'); 
    }; 
}); 

希望这有助于。

+0

那里''photo''来自'$ file = Input :: file('photo');'? – Chriz74

+1

@ Chriz74这是输入名称的格式 –

+2

为什么不使用$ file = $ request-> photo; ? – Chriz74

0

我有custum的形式,并且这种变体不起作用。所以我用正则表达式验证

这样的:

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

可能这将是对别人有帮助的

1

简单,集成这得到验证

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