首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将存储在数据库中的图像显示为blob type.in PHP?

问将存储在数据库中的图像显示为blob type.in PHP?
EN

Stack Overflow用户
提问于 2014-10-06 06:55:10
回答 3查看 1K关注 0票数 0

将数据库中的图像保存为blob类型。但是我不能在下面显示这些图像,我会给出解说图像的代码。

它会给出一个输出,就像一些有特殊符号的代码。如何正确显示图像。我对php.很陌生。

代码语言:javascript
复制
<?php
while($row = mysql_fetch_assoc($result)){
    $type=$row['type']; 
    header("content-type : $type");
    echo $row['src'];
}
?>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-06 06:57:41

你需要base64_encode

代码语言:javascript
复制
 echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['src'] ).'"/>';

只是一个建议,在服务器上存储映像和在DB中保存映像名比匹配更容易。

票数 1
EN

Stack Overflow用户

发布于 2014-10-06 07:44:35

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

代码语言:javascript
复制
<?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'); ?>"/>
票数 0
EN

Stack Overflow用户

发布于 2014-10-06 07:53:48

不使用 base64_encode(),它不适合大多数浏览器。相反,可以这样做:

文件main.php (可以是普通html):

代码语言:javascript
复制
<img src="image.php?id=5"/>

文件image.php

代码语言:javascript
复制
// 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;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26211197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档