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

查看:31
本文介绍了显示存储在 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 并将其粘贴在 标签中.当前,您正在尝试在图像数据之外写入文本,而 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.';

请注意,这不是最佳解决方案,因为它无法缓存且速度相当慢,尤其是在手机上.查看 caniuse 数据 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天全站免登陆