Laravel Validation Dimensions Example

10-Apr-2023

.

Admin

Laravel Validation Dimensions Example

Hi guys, Today i will explained to the Laravel Validation Dimensions Example in your laravel project.Laravel provide new image dimensions validation option for image upload and you are able to use this dimensions validation in laravel 6, laravel 7 and laravel 8 project.Laravel Validation Dimensions Example is so easy to use.so you can just follow my step by step and learn Laravel Validation Dimensions Example.

So let's start to the example and follow to the my all step.

Dimensions Rules:


1)width

2)height

3)min_width

4)min_height

5)max_width

6)max_height

7)ratio

In this Dimensions option through we can set fix width and height, if invalid width and height of image then it will return error. Same way we can set validation min and max height width on your validation.

Few days ago i posted image upload with validation post in Laravel 5.3, you can see here : Laravel 5.3 Image Upload with Validation example.

In this post i used simple validation with mime type and max size like as bellow :

Step 1: Create Route

Last step to create a route in web.php file and use this code.

Route :routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('dimention/form', [UserController::class, 'dimentionForm'])->name('dimention.form');

Route::post('dimention/store', [UserController::class, 'dimentionStore'])->name('dimention.store');

Step 2: Create a UserController Controller

Next you can require to the User Controller so create a User Controller in just following command through.

Controller : app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function dimentionForm()

{

return view('dimentionForm');

}

public function dimentionStore(Request $request)

{

// Ensures to the image width and height

$this->validate($request, [

'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:width=500,height=500',

]);

// or...

// Ensures to the image min_width and min_height

$this->validate($request, [

'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:min_width=350,min_height=600',

]);

// or...

// Ensures to the image max_width and max_height

$this->validate($request, [

'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:max_width=350,max_height=600',

]);

// or...

// Ensures that the width of the image is 1.5x the height

$this->validate($request, [

'avatar' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048|dimensions:ratio=3/2',

]);

dd('done');

}

}

Step 4: Create a DimentionForm Blade File

Next you can require to the dimentionForm.blade.php so create a dimentionForm.blade.php in just following step through.

View :resources/views/dimentionForm.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Validation Dimensions Example</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<body>

<div class="container">

<h1>Laravel Validation Dimensions Example</h1>

<div class="card-body">

@if (count($errors) > 0)

<div class="row">

<div class="col-md-12">

<div class="alert alert-danger alert-dismissible">

<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

@foreach($errors->all() as $error)

{{ $error }} <br>

@endforeach

</div>

</div>

</div>

@endif

{!! Form::open(array('route' => 'dimention.store','method'=>'POST','enctype'=>'multipart/form-data')) !!}

<div class="row">

<div class="col-md-12">

<div class="form-group">

<label>avatar</label>

<input type="file" class="form-control" placeholder="Rejection Reason" name="avatar">

</div>

</div>

</div>

<div class="row">

<div class="col-md-12 text-center mt-2 mb-3">

<button type="submit" class="btn btn-success">Submit</button>

</div>

</div>

{!! Form::close() !!}

</div>

</div>

</body>

</html>

So, finally we are done with our code we can get below output.

php artisan serve

Browser url run : http://localhost:8000/dimention/form

#Laravel 8

#Laravel 7

#Laravel