Laravel 6 Display Image From Storage Folder

Spread the love

In this example, I will give you an example of a laravel 6 display image from storage folder. The image of the storage folder can be easily viewed in the Laravel 6 application. The image of the storage folder is displayed in the blade file of the Laravel 6 project.

All developers and clients want to save images or files securely on their server. so that nobody can access it directly via url. For this reason, Laravel introduces uploading files from the storage folder. Almost use the storage folder to upload files and images to your Laravel application. However, when uploading, it may also be necessary to view or download the image or file.

In this example, however, I will give you two options for displaying your image in a blade file.

Solution 1:

If you want to display the image with the asset function, you must first link your storage folder with the following command:
php artisan storage:link

You can now write the image loading logic to your controller and load images.

If you want to display an image in your blade file, you can use the asset function as in the following example:

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

Solution 2:

In the second solution, we need to create a path to display the image in your application. So let’s create the route as follows:

Create Route:


Route::get(‘image/{filename}’, ‘HomeController@displayImage’)->name(‘image.displayImage’);

Create Controller Method:

<!-- wp:paragraph -->
<p>public function displayImage($filename)<br>
{</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>$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;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>}</p>
<!-- /wp:paragraph -->

Now you can use as follows:

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

You can use anyone.

I hope this tutorial laravel 6 display image from storage folder could help you .

Thaaaanks Buddies….