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.

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

2 Answers 2

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.'">';
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Remember to swap image/jpeg with image/png if you are displaying png file.
This is very slow. Is there any other way to do it perhaps faster
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 ?>">

2 Comments

show me this -> Parse error: syntax error, unexpected '<'
Can you post your full example somewhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.