2

我正在尝试读取图像文件,作为 base64 编码字符串,并在连接使用 HTTPS/SSL 时输出数据字符串,否则如果仅在 HTTP 上,则将 URL 放在 IMG src 属性中。这是我当前的代码,但是它不起作用。

<?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" />

它输出:

<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" />
4

2 回答 2

1

您已将/image/resource目录添加到您的 URL 两次,这导致了file not found 错误

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

所以你的文件网址是/home/content/61/9295861/html/resource/image/resource/image/logo-96x72.png

于 2012-07-31T05:59:21.507 回答
0

改变

<img src="<?php echo base64_encode_image('/resource/image/logo-96x72.png', 'png'); ?>" width="96" height="72" />

<img src="<?php echo base64_encode_image('logo-96x72.png', 'png'); ?>" width="96" height="72" />
于 2012-07-31T06:50:32.463 回答