显示存储在mysql blob中的图像 [英] displaying an image stored in a mysql blob

查看:267
本文介绍了显示存储在mysql blob中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码时,它显示一个图像,该图像作为blob变量存储在mysql Db中.问题是我是否回声了其他任何东西,即使像echo'--------'这样简单的东西也是如此;在我调用图像之前,图像将不会显示.仅显示随机字符.如果我在图像显示后回声了什么,但图像显示后却什么也没有.有人可以告诉我为什么吗,以及我如何一起显示其他项目和图像,谢谢

when i run the code below it displays an image that is stored in a mysql Db as a blob variable. The problem is if I echo out anything else, even something as simple as echo '--------'; before I call the image, the image will not display. only random characters display. if i echo out anything after the image the image displays but nothing does after it. Can someone tell me why this is and how i can go about displaying other items and the image together, thanks

<?php

include("inc/library.php");

connectToDatabase();

$sql = "SELECT * FROM theBlogs WHERE ID = 1;";

$result = mysql_query($sql) or die(mysql_error());  
$row = mysql_fetch_array($result);

header("Content-type: image/jpeg");
echo $row['imageContent'];
$db->close();

?>

推荐答案

您可以将图像数据转换为base64并将其粘贴在<img>标记中.当前,您正在尝试在图像数据之外写入文本,并且Internet浏览器认为您的文本是图像的一部分,因此会引发错误.

You can convert the image data into base64 and stick it in an <img> tag. Currently, you are trying to write text outside of the image data, and the internet browser thinks your text is part of the image and thus throws an error.

尝试这样的事情:

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

请注意,这不是最好的解决方案,因为它无法缓存并且速度相当慢,尤其是在手机上.检出可以使用数据URI .

Note, this isn't the best solution because it cannot be cached and is fairly slow, especially on mobile phones. Check out the caniuse for data URIs.

这篇关于显示存储在mysql blob中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆