在PHP中以blob type.in显示存储在数据库中的图像?

时间:2014-10-06 06:55:10

标签: php blob

将图像作为blob类型保存在数据库中。 但是我无法在下面显示这些图像,我将提供用于消除图像的代码。

它会输出一些带有特殊符号的代码...如何正确显示图像。我是php .`

的新手
<?php
while($row = mysql_fetch_assoc($result)){
    $type=$row['type']; 
    header("content-type : $type");
    echo $row['src'];
}
?>

3 个答案:

答案 0 :(得分:0)

您需要 base64_encode

 echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['src'] ).'"/>';

只是一个建议,它更容易将图像存储在服务器上并将图像名称保存在数据库中而不是匹配

答案 1 :(得分:0)

您需要先读取图像文件,然后才能回显图像。我在下面粘贴了一个示例代码。

<?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'); ?>"/>

答案 2 :(得分:0)

不要使用 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;
相关问题