Image dimension validation rules in Laravel 5.3

(This is part of a series of posts on New Features in Laravel 5.3.)

  1. Introducing Laravel Echo: An In-Depth Walk-Through
  2. The new $loop variable in Laravel 5.3
  3. Customizing additional parameters in FirstOrCreate in Laravel 5.3
  4. The new cache() global helper in Laravel 5.3
  5. New JSON-column where() and update() syntax in Laravel 5.3
  6. Advanced operations with Collection::where in Laravel 5.3
  7. Image dimension validation rules in Laravel 5.3
  8. Customizing pagination templates in Laravel 5.3
  9. 5.3 feature announcement notes from Laracon
  10. Routing changes in Laravel 5.3
  11. Introducing Laravel Scout
  12. Introducing Laravel Passport
  13. Introducing Mailables in Laravel 5.3
  14. Directory structure changes in Laravel 5.3
  15. The new Notification system in Laravel 5.3
  16. Update to queue workers in Laravel 5.3
  17. Using Vue in Laravel 5.3, with the Vue bootstrap and sample component
  18. Defining console commands via closure in Laravel 5.3

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

Matt Stauffer headshot

Hi, I'm Matt Stauffer.

I'm partner & technical director at Tighten Co.

You can find me on Twitter at @stauffermatt


Like what you're reading?

I wrote an entire 450+ page book for O'Reilly: Laravel: Up and Running.

You can order the eBook or print book today.