php Php显示从url到主页的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31793512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php display image from url into homepage
提问by Bob
after no one answered at this question Php Rss feed use img in CDATA -> content:encodedi try to do something else solving this problem...
在没有人回答这个问题后, Php Rss feed use img in CDATA -> content:encoded我尝试做其他事情来解决这个问题......
how can i load an image from a given url directly into my homepage?
如何将给定 url 中的图像直接加载到我的主页中?
<?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.
*我不想将图像保存在任何地方...只需从 url 加载图像并将其显示在我自己的主页中。
回答by Varun
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.'">';
?>
回答by Przemys?aw Jan Wróbel
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.
你快到了。下载文件内容时,如果您不打算将其存储在服务器上,则需要将其编码为 base64。
<?php
$url = '...';
$image = base64_encode(file_get_contents($url));
?>
Then you can display it:
然后你可以显示它:
<img src="data:image/x-icon;base64,<?= $image ?>">