Bob
Bob

Reputation: 83

Php display image from url into homepage

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.

Upvotes: 6

Views: 46775

Answers (2)

Varun
Varun

Reputation: 1946

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.'">';
?>

Upvotes: 9

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 ?>">

Upvotes: 2

Related Questions