Series This post is in a series on New Features in Laravel 5.3.

Image dimension validation rules in Laravel 5.3

Posted on July 25, 2016

!
Warning: This post is over a year old. I don't always update old posts with new information, so some of this information may be out of date.

In Laravel 5.3, we have a new validation option: image dimensions for image uploads. The validation rule is called dimensions, and you can pass the following parameters to it:

  • min_width: Images narrower than this pixel width will be rejected
  • max_width: Images wider than this pixel width will be rejected
  • min_height: Images shorter than this pixel height will be rejected
  • max_height: Images taller than this pixel height will be rejected
  • width: Images not exactly this pixel width will be rejected
  • height: Images not exactly this pixel height will be rejected
  • ratio: Images not exactly this ratio (width/height, expressed as "width/height") will be rejected

You can combine any rules that make sense together. Let's take a look at a few examples. First, let's set up our base install.

// routes file
Route::get('/', function () {
    return view('form');
});

Route::post('/', 'ImageController@postImage');
<!--form.blade.php-->
<form method="POST" enctype="multipart/form-data">
    <input type="file" name="avatar">
    <input type="submit">
</form>

Now, let's make our ImageController and take a look at a few sample validations.

// 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'
        ]);
    }

That's it! One less thing you have to manage yourself in your own code.


Comments? I'm @stauffermatt on Twitter


Tags: laravel  •  laravel 5.3


This is part of a series of posts on New Features in Laravel 5.3:

  1. Jun 16, 2016 | laravel, laravel 5.3, echo, websockets
  2. Jun 27, 2016 | laravel, laravel 5.3
  3. Jun 29, 2016 | laravel, laravel 5.3, eloquent
  4. Jul 6, 2016 | laravel, laravel 5.3
  5. Jul 8, 2016 | laravel, laravel 5.3, eloquent
  6. Jul 25, 2016 | laravel, laravel 5.3
  7. Jul 26, 2016 | laravel, laravel 5.3
  8. Jul 27, 2016 | laravel, laravel 5.3, laravel scout, laravel passport, mailable
  9. Jul 27, 2016 | laravel, laravel 5.3, routing
  10. Jul 29, 2016 | laravel, laravel 5.3, laravel scout
  11. Jul 30, 2016 | laravel, laravel 5.3, laravel passport, oauth
  12. Aug 5, 2016 | laravel, laravel 5.3, mail, laravel mailables
  13. Aug 8, 2016 | laravel, laravel 5.3
  14. Oct 19, 2016 | laravel, laravel 5.3, laravel notifications, notifications
  15. Dec 21, 2016 | laravel, laravel 5.3, queues
  16. Dec 21, 2016 | laravel, laravel 5.3, vuejs, vueify, authorization
  17. Jan 30, 2017 | laravel, laravel 5.3, artisan

Subscribe

For quick links to fresh content, and more thoughts that don't make it to the blog