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',]);