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

Toufik94's avatar

How to serve image of external source over a Laravel API

Hi guys,

The question is simple: I have a file from an external source, lets say my Azure Blob Storage called ''www.externalwebsite.com/test.jpg". I want to create an API to display the content from that url via my own defined API which could be "www.localhost:8000/api/image/test.jpg".

I want that API-url to be used for the 'src' attribute of my HTML image tag.

What is the best way to do this without first saving the image locally or in some kind of cache?

0 likes
4 replies
christian-qode's avatar

@toufik94 You can get the content of the image by file_get_contents PHP function. In your API controller you can stream this content.

Toufik94's avatar

@christian-qode

Isnt that the same as Laravels StreamedResponse which you can get with ->download or response()->file($path)? I already retrieved this information but I'm stuck how to present the actual image so that it can be used in a HTML img tag. I know you can use that stream to download the file but thats not what I want.

christian-qode's avatar

@Toufik94 I understand. I have done this in a project by myself with the following response:

      return response()->file(storage_path($file->path));

You can include the full URL of the route (without .jpg of course) in the img tag.

The $file->path in the code above is a reference to an existing file in the storage, you might change this to your stream.

Please or to participate in this conversation.