Image Verification in Laravel 5 Intervention

I installed intervention in Laravel 5.1 and I use image upload and resize something like this:

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

What I don’t understand is how intervention handles validation of the uploaded image? I mean, does the intervention already have a built-in image validation check in it, or is it something I need to add manually using Laravel Validation to check file formats, file sizes, etc.? I read the docs on tampering, and I could not find information on how image verification works when using tampering with laravel.

Can someone point me in the right direction, please.

+6
source share
3 answers

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

What I learned is that Image Intervention does not perform any validations. All image checks must be performed before they are submitted for tampering with the image for download. Thanks to Laravel's built-in validators, such as the image and mime types, which makes checking the image very easy. This is what I have now, when I first check the file input before transferring it to Image Intervention.

Validator Validation Before Intervention Processing 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.

+11
source

Just integrate this to get confirmation.

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

I have a custum form and this option does not work. So I used regex check

like this:

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

maybe it will be useful for someone

0
source

All Articles