DEV Community

Cover image for Laravel 12 Image Validation Rules Example
itstuffsolutions
itstuffsolutions

Posted on

Laravel 12 Image Validation Rules Example

Laravel 12 Image Validation Rules Example Tutorial

In this tutorial, we’ll walk you through how to apply image validation rules in a Laravel 12 application. Laravel offers built-in validation rules like image,** mimes, max, min, size,** and dimensions **to ensure proper file uploads.
With these rules, you can easily validate image types such as **jpeg, jpg, png, svg,
etc. The max and min rules help control the file size, while the dimensions rule lets you enforce height and width constraints on the uploaded images.
Follow the steps below to implement image upload validation in your Laravel 12 project.

Step for Laravel 12 Image Validation Rules:

Step 1: Install Laravel 12
Step 2: Create Controller
Step 3: Create Routes
Step 4: Create Blade File
Step 5: Run Laravel App

Step 1: Install Laravel 12
Perform this step only if Laravel 12 is not yet installed. To create a new Laravel 12 project, run the following command in your terminal:

laravel new laravel-image-upload

Enter fullscreen mode Exit fullscreen mode

Step 2:Create the Controller
In Step 2, we'll generate a new controller named ImageController. Inside this controller, we'll define two methods: index() to load the blade view and postImage() to handle image upload along with validation logic.
To create the controller, run the command below:

php artisan make:controller ImageController
Enter fullscreen mode Exit fullscreen mode

next step, let’s update the following code to ImageController **File. open the File **app/Http/Controllers/ImageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Str;

class ImageController extends Controller
{
    public function index()
    {
        return view('image');
    }

    public function postImage(Request $request){
         $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048 | min:50 |dimensions:min_width=100,max_width=500,min_height:200,max_height:1000',
        ]);
        $imageName = time(). '_' . Str::random(10).'.'.$request->image->extension();
        //store file in public folder
        $request->image->move(public_path('uploads'), $imageName);



        return back()->with('success', 'Image uploaded successfully!')->with('img',$imageName);
    }
}

Enter fullscreen mode Exit fullscreen mode

Image Validation Rules Guidelines
required: The image field cannot be empty — uploading an image is mandatory.
image: This validation rule Confirms that the uploaded file is a valid image format.
mimes: Only allows specific types like .jpeg, .png, etc., to ensure compatibility.
max:2048: Restricts the image size to a maximum of 2MB (2048 kilobytes).
dimensions: The image must have a width between 100px to 500px and height between 200px to 2000px to be accepted.

Image validation form Preview

Step 3: Set Up Routes

Furthermore, open the file routes/web.php and add the routes to manage GET and POST requests for rendering view and image upload logic.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;

Route::get('/', function () {
    return view('welcome');
});
Route::get('/image-upload', [ImageController::class ,'index']);
Route::post('/image-upload', [ImageController::class ,'postImage'])->name('post.image');

Enter fullscreen mode Exit fullscreen mode

Step 4:Create the Blade View

In the final step, create a new blade file “image.blade.php“.
Read the full tutorial on Laravel 12 Image Validation Rules Example for detailed steps and code.

Suggested Posts:

Top comments (0)