通过PHP

时间:2015-12-14 18:35:38

标签: php base64

以下API网址在PNG图片中有输出:

http://qrfree.kaywa.com/?l=1&s=8&d=google.com

我想将API URL转换为“data:image / png; base64”(DATA URL / DATA URI),类似于下面的PHP代码示例:

$image = ("<img src = http://qrfree.kaywa.com/?l=1&s=8&d=google.com"); // False! (only example!)
//or
$image = 'real-picture.png'; // True!

$imageData = base64_encode(file_get_contents($image));
$src = 'data:image/png;base64,'.$imageData;
echo '<img src="'.$src.'">';

但是,上面的代码适用于具有指定格式的实际图片,如果替换为API URL,则无法读取图像路径。 如何纠正代码?

1 个答案:

答案 0 :(得分:1)

假设向http://qrfree.kaywa.com/?l=1&s=8&d=google.com发出的网络请求会返回image / png类型的图片,您可以执行...

$image = "http://qrfree.kaywa.com/?l=1&s=8&d=google.com";
echo '<img src="data:image/png;base64,', 
    base64_encode(file_get_contents($image)),
    '">';

file_get_contents()需要有效的文件名或有效的http(s)请求。您的代码传递了以某些HTML <img ...

开头的内容