Norway


In 5.3, we have a new option: dimensions for 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 20;width/height21;) will be rejected

You can combine any 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-">
    <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=00'
        ]);

        // or...

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

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



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here