How to display the storage folder image in Laravel?
This tutorial is for How to display the storage folder image in Laravel? I would like to display an image saved in a storage directory. Laravel default provides storage:link command. this command is to use the storage folder created in your root directory.
The Laravel storage app directory is more robust and secure. So, Laravel also recommended using the storage filesystems always. most developers and clients want to store images or files on insecure servers. so anyone can not access directly using URL. Most developers and clients use a storage folder for uploading files and images on the Laravel app. And when files or Images are uploaded, And when uploaded images or files also might be required to download or display. So we will learn in this tutorial, to display the storage folder image in Laravel applications.
Also read: Laravel storage folder in image upload and old image delete
You can create a storage link. run the below command :
php artisan storage:link
<img src="{{ asset('public/storage/templates/image.png') }}" class="img img-thumbnail">
Define routes
#web.php
Route::get('image/{filename}', 'HomeController@displayImage')->name('image.displayImage');
HomeController.php displayImage function define.
#app/Http/Controllers/HomeController.php
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;
}