将数据库中的图像保存为blob类型。但是我不能在下面显示这些图像,我会给出解说图像的代码。
它会给出一个输出,就像一些有特殊符号的代码。如何正确显示图像。我对php.很陌生。
<?php
while($row = mysql_fetch_assoc($result)){
$type=$row['type'];
header("content-type : $type");
echo $row['src'];
}
?>发布于 2014-10-06 06:57:41
你需要base64_encode
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['src'] ).'"/>';只是一个建议,在服务器上存储映像和在DB中保存映像名比匹配更容易。
发布于 2014-10-06 07:44:35
您需要先读取图像文件,然后才能回显图像。我在下面粘贴了一个示例代码。
<?php
function base64_encode_image ($filename=string,$filetype=string) {
if ($filename) {
$imgbinary = fread(fopen($filename, "r"), filesize($filename));
return 'data:image/' . $filetype . ';base64,' . base64_encode($imgbinary);
}
}
?>
<img src="<?php echo base64_encode_image('img.png','png'); ?>"/>发布于 2014-10-06 07:53:48
不使用 base64_encode(),它不适合大多数浏览器。相反,可以这样做:
文件main.php (可以是普通html):
<img src="image.php?id=5"/>文件image.php
// replce this with your db-loading code
$image = read_from_db((int)$_GET['id']);
// important: this tells the client what kind of image you have
header('Content-Type: ' . $image->mime);
// give the browser an indication of the size of the image
header('Content-Length: ' . strlen($image->data));
// TODO Put other useful headers here
// send the actual image data to the browser
echo $image->data;https://stackoverflow.com/questions/26211197
复制相似问题