Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I render a remote image with php?

Here's a jpg: https://i.sstatic.net/PIFN0.jpg

stackoverflow logo jpg

Let's say I'd like this rendered from /img.php?file_name=PIFN0.jpg

Here's how I'm trying to make this work:

/sample.php

<p>Here's my image:</p>
<img src="/img.php?file_name=PIFN0.jpg">

/img.php

<?php
    $url = 'https://i.sstatic.net/' . $_GET['file_name'];
    header('Content-type: image/jpeg');
    imagejpeg($url);
?>

I would expect /sample.php to show the image. But this doesn't work. All I get is a broken image. What am I doing wrong?

like image 276
Ryan Avatar asked Jul 16 '26 02:07

Ryan


1 Answers

Use imagecreatefromjpeg:

<?php
    $url = 'https://i.sstatic.net/' . $_GET['file_name'];
    header('Content-type: image/jpeg');
    imagejpeg(imagecreatefromjpeg($url));
?>

Reference: http://php.net/manual/en/function.imagecreatefromjpeg.php

like image 75
Daniel Li Avatar answered Jul 17 '26 15:07

Daniel Li



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!