How to Display Image from Storage Folder in Laravel?

By Hardik Savani April 16, 2024 Category : Laravel

In this example, i will give you example of laravel display image from storage folder. we can easily show image from storage folder in laravel 6 application. we will display image from storage folder in blade file with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.

All most developer and client want to store images or files secure on his server. so anyone can not access directly using url. that's the reason laravel introduce storage folder file upload. Almost using storage folder for uploading files and images on his laravel app. But when you upload it also might be require to display or download for that image or file.

However, In this example, i will give you 2 way to show your image in blade file.

Solution 1:

first of all if you want to show image using asset function than you need to link your storage folder using following command:

php artisan storage:link

Now you can write image upload logic on your controller and image upload.

If you want to display image on your blade file than you can use asset function as like bellow example:

<img src="{{ asset(.'/images/'.$article->image) }}" alt="" title="">

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}', 'HomeController@displayImage')->name('image.displayImage');

Create Controller Method:

public function displayImage($filename)

{

$path = storage_public('images/' . $filename);

if (!File::exists($path)) {

abort(404);

}

$file = File::get($path);

$type = File::mimeType($path);

$response = Response::make($file, 200);

$response->header("Content-Type", $type);

return $response;

}

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...

Shares