image size validation in laravel code example

Example 1: laravel validate max file size

$validator = Validator::make($request->all(), [
    'file' => 'max:500000',
]);

Example 2: image dimension when uploading in laravel validation

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

Example 3: laravel image ratio validation

// ImageController
public function postImage(Request $request)
{
  $this->validate($request, [
    'avatar' => 'dimensions:min_width=250,min_height=500'
  ]);

  // or... 

  $this->validate($request, [
    'avatar' => 'dimensions:min_width=500,max_width=1500'
  ]);

  // or...

  $this->validate($request, [
    'avatar' => 'dimensions:width=100,height=100'
  ]);

  // or...

  // Ensures that the width of the image is 1.5x the height
  $this->validate($request, [
    'avatar' => 'dimensions:ratio=3/2'
  ]);
}

Tags:

Php Example