laravellaravel-5laravel-validationinterventionlaravel-filesystem

Image Validation in Laravel 5 Intervention


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

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 Validation to 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.

Can someone point me in the right direction please..


Solution

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

    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 image and mime types 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.

    Validator Check Before Processing Intervention Image Class:

     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.