douqian6315 2012-07-31 05:30
浏览 57
已采纳

在HTTP上的URL上通过HTTPS提供图像的数据字符串

I am attempting to read an image file, as a base64 encoded string and output a data string when the connection uses HTTPS/SSL, and otherwise put out the URL in the IMG src attribute if it is only on HTTP. Here is my current code, however it does not work.

<?php 
function base64_encode_image($filename, $filetype) {
    if (($_SERVER["HTTPS"] == "on") && $filename) {
        $file = "/home/content/61/9295861/html/resource/image$filename";
        $imgbinary = fread(fopen($file, "r"), filesize($file));
        return "data:image/$filetype;base64," . base64_encode($imgbinary);
    } else {
        return $filename;
    }
}
?>
<img src="<?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?>" width="96" height="72" />

It outputs:

<img src="<br />
<b>Warning</b>:  fopen(/home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png) [<a href='function.fopen'>function.fopen</a>]: failed to open stream: No such file or directory in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br />
<br />
<b>Warning</b>:  filesize() [<a href='function.filesize'>function.filesize</a>]: stat failed for /home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br />
<br />
<b>Warning</b>:  fread() expects parameter 1 to be resource, boolean given in <b>/home/content/61/9295861/html/theme/latest/index.php</b> on line <b>5</b><br />
data:image/png;base64," width="96" height="72" />
  • 写回答

2条回答 默认 最新

  • douzi6992 2012-07-31 05:59
    关注

    You have added the /image/resource directory twice to your URL which is resulting in a file not found error.

    $file = "/home/content/61/9295861/html/resource/image$filename";
    $filename = "/resource/image/logo-96x72.png"
    

    So your File URL is /home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大