我正在尝试读取图像文件,作为 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" />