Discover how to implement image validation rules in Laravel 11 with this comprehensive example. Learn how to validate image uploads, set file size limits, file types, dimensions, and more. This step-by-step guide is perfect for developers looking to ensure secure and efficient image handling in their Laravel 11 applications. You Can Learn Laravel 11: How to Remove Public from URL – Complete Guide with Example
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel ImageValidation
In this step, we will create a new ImageController; in this file, we will add two method index() and store() for render view and store image logic. You Can Learn How to Add Text to Image in Laravel 11 – Step-by-Step Guide
Let’s create ImageController by following command:
php artisan make:controller ImageController
next, let’s update the following code to Controller File.
app/Http/Controllers/ImageController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\View\View; use Illuminate\Http\RedirectResponse; class ImageController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(): View { return view('imageUpload'); } /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function store(Request $request): RedirectResponse { $this->validate($request, [ 'image' => [ 'required', 'image', 'mimes:jpg,png,jpeg,gif,svg', 'dimensions:min_width=100,min_height=100,max_width=1000,max_height=1000', 'max:2048' ], ]); $imageName = time().'.'.$request->image->extension(); $request->image->move(public_path('images'), $imageName); /* Write Code Here for Store $imageName name in DATABASE from HERE */ return back()->with('success', 'You have successfully upload image.') ->with('image', $imageName); } }
READ MORE
The above is the detailed content of Laravel Image Validation Rules – Complete Example and Guide. For more information, please follow other related articles on the PHP Chinese website!