6

after no one answered at this question Php Rss feed use img in CDATA -> content:encoded i try to do something else solving this problem...

how can i load an image from a given url directly into my homepage?

<?php
     $url = "...";
     $image = file_get_contents("$url");
     echo $image;
?>

*i don't want to save the image anywhere... just load the image from the url and show it in my own homepage.

Community
  • 1
  • 1
Bob
  • 83
  • 1
  • 1
  • 7
  • Try this `$image = file_get_contents($url);` or if you are just displaying why dont you just show image from the url – Varun Aug 03 '15 at 17:50
  • Now it shows me a lot of letters and question marks... it seem like it tries to decoding the image – Bob Aug 03 '15 at 17:54
  • You have to base 64 encode it. Check this: http://stackoverflow.com/a/21600709/4407926 – Varun Aug 03 '15 at 17:55
  • tried it... it works but show me this -> mime_content_type(): Can only process string or stream arguments – Bob Aug 03 '15 at 18:00

2 Answers2

9

Try this code,work fine on my machine.

<?php

$image = 'http://www.google.com/doodle4google/images/d4g_logo_global.jpg';
$imageData = base64_encode(file_get_contents($image));
echo '<img src="data:image/jpeg;base64,'.$imageData.'">';
?>
Varun
  • 1,946
  • 2
  • 11
  • 17
2

You are almost there. When you download the contents of the file you need to encode it to base64 if you do not plan to store it on server.

<?php

$url = '...';
$image = base64_encode(file_get_contents($url));

?>

Then you can display it:

<img src="data:image/x-icon;base64,<?= $image ?>">