您如何将图像标签的来源设置为二进制图像数据,我所看到的一切都不适合我

问题描述

我有 PHP 从 memcached 获取的二进制图像数据,它将它发送到 javascript,然后 Javascript 创建一个新图像并将源设置为数据,数据已经在 base64_encoding 中, 由于它来自用户机器,当他们将图像文件上传站点时,我使用 javascript FIle_Reader 获取二进制数据,将其发送到 PHP ,并将其存储在 File_Server 和 memcached 中以备后用。 但是,当检索数据并尝试使用它来输出可见图像时,出现错误,我已经在网上搜索过,但没有人真正解决我的具体问题。

function Create_Img_From_Binary($data){
    $Data=json_encode($data);
    echo "
    <script>
    function Create_Img_From_Binary(data){
      let img= document.createElement('img');
      img.src=$data;
      document.body.appendChild(img); 
        
    }
    Create_Img_From_Binary($Data);
    </script>
    ";
   
}
$fd= fopen("img.html",'r');
$data= fread($fd,filesize("img.html"));
Create_Img_From_Binary($data);




  The img.html file just contains the raw image data

解决方法

以这种方式使用标签:

<img src="data:image/gif;base64,xxxxxxxxxxxxx...">

上面的 xxxx 是图片 base64 图片代码。

<img src="data:image/jpeg;base64,<?=base64_encode($Data)?>">

这里 $data 是你的二进制图像

它只是您问题的解决方案。看看它是否有效。