PHP Base64 Image data not working in image tag?

2019-09-15 16:49发布

This is based off a past question of mine, but its a different question and I think making a new question will help many people who have this problem also.

Here is what I have

<img src="data:image/png;base64,<?php echo picture(); ?>" />    

The image shows up as a broken image, and when inspect it as an element it shows up as the correct thing.... but with some weird tags at the end

{base64 data}&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;img src=" data:image="" png;base64,<html="">

Does anyone know why this is? Its so bizarre.

Thanks!

Edit: So the title of the page is "Test"... but i dont get why its all showing up in the image tag

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-15 17:26

As noted by Dagon use of data:image is bad idea as you then depend on browser's implementation (which is out of your control and often it is broken).

I suggest you to use link to php script

<html>
<head>
<title>Test</title>
</head>
<body>
<img src="getpicture.php" />    
</body></html>

getpicture.php:

<?php

function getPicture()
  {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, 'image url');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer XXXX'));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $picture = curl_exec($ch);
    curl_close($ch);
    return $picture;
  }

header("Expires: " . date("D, j M Y H:i:s", strtotime("+1 year")));
header("Content-Type: image/png");

echo getPicture();

?>
查看更多
登录 后发表回答