Image Validation in Laravel 5 Intervention

Neel

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

Neel

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel 5 intervention image upload multiple size

Saving Intervention Image In Owners Folder in Laravel 5

Laravel 5 Class 'Intervention\Image\ImageServiceProvider' not found

Image Intervention Image on Laravel

Laravel 5 - Image Upload and Resize using Intervention Image Package

Laravel image intervention compression

intervention image in Laravel 8

Laravel 5: Intervention Image, imagecache. Missing argument 2 error

How to delete image with Intervention.io library for Laravel 5

Can't write image data laravel 5 Intervention

Image array validation in Laravel 5

Laravel Image Intervention avoid rotation

image intervention out of memory - laravel

Using Image Intervention with Laravel Nova

Add white space to image using Laravel 5 intervention image to make square image

Intervention image multiple image upload in laravel

Laravel Progressive Image using Image intervention

Laravel Image Resize Problem (image-intervention)

Laravel 5 upload multiple image validation

Laravel intervention/image chrome/safari issue

Intervention cannot write image to the path in Laravel

Using intervention/image for upload /resizing with Laravel API

Laravel image intervention resize and put in storage

Image Intervention Can Not Be Installed On Laravel 9

Image Intervention w/ Laravel 5.4 Storage

php laravel What is the Stream() function in image intervention

Laravel + Image Intervention : Force download of file that is not saved

Laravel Image Intervention not working in artisan command

Laravel 5.2: create image thumbnails in using intervention