Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php display image from url into homepage

Tags:

url

php

image

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.

like image 806
Bob Avatar asked Aug 03 '15 17:08

Bob


2 Answers

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.'">';
?>
like image 85
Varun Avatar answered Sep 17 '22 19:09

Varun


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 ?>">
like image 29
Przemysław Jan Wróbel Avatar answered Sep 19 '22 19:09

Przemysław Jan Wróbel