BLOB 数据作为图像标签的 src 属性?

发布于 2024-08-17 11:11:12 字数 700 浏览 2 评论 0原文

我继承了一个网站,该网站使用不太理想的方式来处理图像,它们已作为 BLOB 存储在数据库中。图像标签包含

<img src='image.php?id=22' />

但不显示图像。当我访问 image.php?id=22 页面时,BLOB 数据只是转储到屏幕上(都是有趣的字符),并且没有显示图像。我不确定这是否与发送到浏览器的数据的内容类型有关?有人告诉我“直到几周前这还可以正常工作”。

我的问题是,是否可以将 BLOB 数据显示为图像标签的 src 属性?

编辑:根据要求,这里是 image.php 的内容。希望这有一些用处。

<?
error_reporting(0);
include("../inc/connect.inc");
include("../inc/common.inc");
include("item.class");
$item = new Item($id);
echo $item->Picture;
?>

EDIT2:如果我添加内容类型行,页面只会打印出图像的路径: http://www.site.com/dir/image.php?id=49。尝试了多种内容类型,没有区别。奇怪的!

I've inherited a site that uses a less then ideal way to handle images, they have been stored in the DB as BLOBs. The image tags consist of

<img src='image.php?id=22' />

but no images are displaying. When I visit the image.php?id=22 page, the BLOB data is just dumped out on to the screen (all funny characters) and no image is shown. I'm not sure if this has to do with the content type of data that's being sent to the browser? I've been told "this used to work fine until a few weeks ago".

My question is, is it even possible to display BLOB data as the src attribute of an image tag?

EDIT: by request, here are the contents of image.php. Hope this is of some use.

<?
error_reporting(0);
include("../inc/connect.inc");
include("../inc/common.inc");
include("item.class");
$item = new Item($id);
echo $item->Picture;
?>

EDIT2: If i add the content-type line, the page just prints out the path to the image: http://www.site.com/dir/image.php?id=49. Tried with several content types, no difference. Strange!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-08-24 11:11:12

在数据库中,您应该有一个内容类型字段以及 BLOB 数据,除非 BLOB 数据是一种固定类型(例如,image/png),您可以在图像中将其硬编码.php 脚本。您需要包含一个带有 Content-type: set 的新标头。

header('Content-type: image/png');

但你必须找出图像类型是什么 - 我想,它一定是 image/jpeg、image/png 或 image/gif 之一?

You should have, in the database, a content-type field as well as the BLOB data, unless the BLOB data is one fixed type (e.g., image/png) where you can hardcode it in your image.php script. You'll need to include a new header with the Content-type: set.

header('Content-type: image/png');

But you'll have to find out what the image type is - trial and error will do I guess, it's gotta be one of image/jpeg, image/png or image/gif surely?

月下伊人醉 2024-08-24 11:11:12

好吧,如果您直接链接到图像,则将发送回完全相同的数据,“BLOB”与网络服务器读取并发送到浏览器的数据之间没有区别。

所以,你的内容类型想法是正确的……firebug 所说的内容类型是什么?您可能想发布 image.php 的代码。

编辑:

所以我们开始...只需将您的脚本修改为如下所示:

<?
header( 'Content-Type: image/png' );
error_reporting(0);
include("../inc/connect.inc");
include("../inc/common.inc");
include("item.class");
$item = new Item($id);
echo $item->Picture;
?>

image/png 替换为您的图像的任何格式。

Well, if you linked directly to the image the exact same data would be sent back, there's no distinction between a 'BLOB' and just data read by the webserver and sent to the browser.

So, you're on the right track with the content-type idea... What does firebug say the content-type is? You might want to post the code for image.php.

edit:

So there we go... Just modify your script to look like:

<?
header( 'Content-Type: image/png' );
error_reporting(0);
include("../inc/connect.inc");
include("../inc/common.inc");
include("item.class");
$item = new Item($id);
echo $item->Picture;
?>

Replacing image/png with whatever format your images are.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文