Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jeroen's avatar

Storage get image as file

I was able to use Storage::put() to upload an image. In this case, I use the local disk driver, and I could browse to the storage folder and view the file. However, in Laravel when I want to view the file using Storage::get() I get the contents as a string rather than as an image file. My question is how I can turn this into a file that I can use in my view.

0 likes
12 replies
Chris Magnussen's avatar

To render a blob as an image, you would have to set the correct headers

<?php
    header("Content-type: image/jpeg");
    echo Storage::get('file.jpg');
?>
1 like
michaeldyrynda's avatar

Check out intervention - it will handle setting the appropriate response headers for your images (and do a lot more for you!)

1 like
Jeroen's avatar

I don't get it working, also not with Intervention. The only thing that seems to work is putting this in my controller:

$photo = $this->photos->findOrFail($photoId);
$file = Storage::get('uploads/photos/'.$photo->filename);     //The filename is stored in a database.
return response($file, 200)->header('Content-Type', 'image/jpeg');

However, this does not allow me to create a gallery of my photos at some point.

Chris Magnussen's avatar

I'm not sure what you are trying to achieve here. If you want to show your image on your site, you can just use an img tag.

<img src="uploads/photos/{{ $photo->filename }}">
Jeroen's avatar

But that would not work as soon as I switch the disk from local to a cloud one. Would that always require me to change the views?

Chris Magnussen's avatar
Level 3

You could always output it as a base64 encoded blob.

<img src="data:image/jpeg;base64,{{ base64_encode($file) }}">

That way, it doesn't matter where you get your image from.

7 likes
Jeroen's avatar

That works (with local disk, still have to test cloud). Thanks! Although the page source now looks horrible :P

mmguide's avatar

Yes it seems the filesystem with storage is not intended to use on the app for gallery public viewing, only on a separate view or download page via response.

myronmiller's avatar

Why does the @if statement find the file but when I attempt to show the file it fails.

@if (Storage::disk('local')->exists('Images/' . $user->username . '/' . $user->profile->avatar))
                              <img src="{{ Storage::disk('local')->get('Images/' . $user->username . '/' . $user->profile->avatar) }}" style="width:150px; height:150px; float: left; border-radius:50%; margin-right:25px; padding-top: 10px;">
Snapey's avatar

because Storage is private. The file exists but it's not in public space.

Oh, and start your own thread if you want people to see you question!

1 like
myronmiller's avatar

Hello and thanks for responding, I purposely posted only to you because I felt you could answer all of my questions :-). However, I have figured it out, Storage:: and how local and public disk differ was the issue. I wanted all of my images to be secure no matter what from the outside. So I used Local with a directory structure of /Images/{username}/files and it is working perfectly.

psierak's avatar

@Chris Magnussen @Jeroen Thanks for the solution.

<img src="data:image/jpeg;base64,{{ base64_encode($file) }}>

I got it to serve a private image in storage as intended. Was there any work around for the webpage source not having the jpeg code in the view source?

1 like

Please or to participate in this conversation.