Home > front end >  Creating a file that returns an image from an external server
Creating a file that returns an image from an external server

Time:08-10

As the title says, I want to create a file that returns an image from an external server without downloading / storaging it. I tried something with PHP headers, but it didn't work as intended (I can't find the code right now sorry). For example, if we have an image_displayer.php file.

image_displayer.php?url=https://example.com/images/epic_image.png

This URL should return the "https://example.com/images/epic_image.png" image. This image will be displayed from an HTML file from the same server. I couldn't get it working with PHP headers (it was giving problems in the HTML file) It doesn't have to be in PHP. It doesn't matter to me. Thanks in advance.

CodePudding user response:

You could try something like this:

$image_data = file_get_contents(IMAGE_URL);
$image_resource = imagecreatefromstring($image_data);

header('Content-Type: image/png');
imagepng($image_resource);
imagedestroy($image_resource);

If you just want to quickly show the image without handling it, you could simply do a 301 redirect to it.

  • Related