Image Validation in Laravel 5 Intervention

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.


Image Supported Formats http://image.intervention.io/getting_started/formats

The readable image formats depend on the chosen driver (GD or Imagick) and your local configuration. By default Intervention Image currently supports the following major formats.

    JPEG PNG GIF TIF BMP ICO PSD WebP

GD ✔️ ✔️ ✔️ - - - - ✔️ *

Imagick ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️ *

  • For WebP support GD driver must be used with PHP 5 >= 5.5.0 or PHP 7 in order to use imagewebp(). If Imagick is used, it must be compiled with libwebp for WebP support.

See documentation of make method to see how to read image formats from different sources, respectively encode and save to learn how to output images.

NOTE: (Intervention Image is an open source PHP image handling and manipulation library http://image.intervention.io/). This library does not validate any validation Rules, It was done by Larval Validator class

Laravel Doc https://laravel.com/docs/5.7/validation

Tips 1: (Request validation)

$request->validate([
   'title' => 'required|unique:posts|max:255',
   'body' => 'required',
   'publish_at' => 'nullable|date',
]); 

// Retrieve the validated input data...
$validated = $request->validated(); //laravel 5.7

Tips 2: (controller validation)

   $validator = Validator::make($request->all(), [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect('post/create')
                    ->withErrors($validator)
                    ->withInput();
    }

Simply, Integrate this to to get validation

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

i have custum form, and this variant does not work. So i used regexp validation

like this:

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

may be it will be helpful for someone