ItSolutionStuff.com

Laravel 11 Display Image from Storage Folder Example

By Hardik Savani • November 4, 2024
Laravel

In this post, I will show you how to display image from storage app public folder in laravel 11 application.

Laravel provides a secure way to store images and files in the storage folder, preventing users from directly accessing files via URL. So, how can we display these images from the storage folder? Below, I’ll outline two methods you can use to display images securely from storage. Let’s explore both options so you can choose the one that works best for your needs.

Solution 1:

first of all, we will create a Symbolic Link if you haven't already, to make the public storage directory accessible from the web:

php artisan storage:link

Now, Access the Image URL in your Blade template or controller:

You can use Laravel’s Storage facade to get the URL of the image.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class HomeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $imageUrl = Storage::url('images/your-image.jpg');
    }
}

Display the Image in your Blade view:

<img src="{{ Storage::url('images/your-image.jpg') }}" alt="Image">

Solution 2:

In second solution, we have to create route for display image on your application. so let's create route as like bellow:

Create Route:


Route::get('image/{filename}', 'ImageController@displayImage')->name('image.displayImage');

You can write the controller file code:

app/Http/Controllers/ImageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ImageController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function displayImage()
    {
        $path = 'public/images/' . $filename;

        if (!Storage::exists($path)) {
            abort(404);
        }

        $file = Storage::get($path);
        $type = Storage::mimeType($path);

        return response($file, 200)->header('Content-Type', $type);
    }
}

Now you can use like as bellow:

<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">

You can use anyone.

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 11 Store JSON Format Data in Database Tutorial

Read Now →

How to Integrate AdminLTE 3 in Laravel 11?

Read Now →

Laravel 11 Event Broadcasting Tutorial

Read Now →

Laravel 11 Confirm Box Before Delete Record from Database

Read Now →

Laravel 11 Localization | Create Multi Language in Laravel 11

Read Now →

Laravel 11 Client Side Form Validation using JQuery

Read Now →

Laravel 11 Breeze Multi Auth Tutorial

Read Now →

Laravel 11 Reverb Real-Time Notifications Example

Read Now →

How to Compress Image Size in Laravel 11?

Read Now →

Laravel 11 Stripe Payment Gateway Integration Example

Read Now →

How to Get User Location using IP Address in Laravel 11?

Read Now →

Laravel 11 One to One Relationship Example

Read Now →

Laravel 11 Resize Image Before Upload Example

Read Now →

Laravel 11 Resource Route and Controller Example

Read Now →

Laravel 11 CRUD Application Example Tutorial

Read Now →