Laravel Image Validation Rules – Complete Example and Guide

Mary-Kate Olsen
Release: 2024-10-16 12:07:02
Original
142 people have browsed it

Laravel Image Validation Rules – Complete Example and Guide

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

Step for Laravel 11 Validation Rules for Image and Photo

Step 1: Install Laravel 11

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
Copy after login

Step 2: Create Controller

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
Copy after login

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); 
    }
}
Copy after login

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!