如何显示 mysql blob 中的图像

发布于 2024-08-12 06:31:52 字数 232 浏览 2 评论 0原文

我正在尝试显示来自 MySQL blob 字段的图像。我尝试了几种不同的方法,但似乎都不起作用。

我尝试过:

  • header("Content-type: $type"); img src = $blobData;

  • header("内容类型:$type"); echo($blobData);

I am trying to display an image from a MySQL blob field. I have tried a few different things and none of them seem to work.

I have tried:

  • header("Content-type: $type"); img src = $blobData;

  • header("Content-type: $type"); echo($blobData);

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

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

发布评论

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

评论(4

能否归途做我良人 2024-08-19 06:31:52
<?php
  header("Content-type: $type");
  echo $blobData;
?>

这段代码看起来完全没问题。然而,我从另一个人那里听到了类似的抱怨,我能够通过确保以下内容来解决问题:

  1. PHP 脚本在发送二进制图像数据之前或之后不会输出任何额外的字符。

  2. php 脚本保存为纯 ASCII 文本文件,而不是 Unicode/UTF-8 编码文件。 Unicode/UTF-8 编码的 PHP 文件可能包含签名作为第一个字节。这些字节在文本编辑器中是不可见的,但服务器会在 JPEG/GIF/PNG 数据之前将这些额外的字节发送到浏览器。因此,浏览器会在数据的开头发现错误的签名。要解决此问题,请在记事本中创建一个空白文本文件,粘贴 php 代码并以 ANSI 编码保存该文件。

<?php
  header("Content-type: $type");
  echo $blobData;
?>

This code looks perfectly OK. However, I heard a similar complain from another person and I was able to troubleshoot it by assuring that:

  1. The php script does not output any extra character before or after sending the binary image data.

  2. The php script is saved as a pure ASCII text file, not as a Unicode/UTF-8 encoded file. The Unicode/UTF-8 encoded PHP files might include a signature as the first bytes. These bytes will be invisible in your text editor but server will send these few extra bytes to the browser before the JPEG/GIF/PNG data. The browser will therefore find the wrong signature in the beginning of data. To workaround, create a blank text file in notepad, paste in the php code and save the file in ANSI encoding.

痴情 2024-08-19 06:31:52

您可能会考虑的另一个选项(假设您使用的是 Apache):

为所有图像扩展名(png、jpg、gif)创建一个带有 mod_rewrite.htaccess 文件。

让它重定向到一个 php 脚本,该脚本在数据库中查找请求的图像。如果存在,它会回显标题和博客。如果不存在,它会返回标准 404。

这样您就可以:

<img src="adorablepuppy.jpg" />

然后重定向 ala:

RewriteEngine on
RewriteRule \.(gif|jpg|png)$ imagelookup.php

该脚本对图像进行查询,(显然)假设所请求的图像具有与URL 中的文件名:

 $url = $_SERVER['REQUEST_URI'];
 $url_parts = explode("/", $url);
 $image_name = array_pop($url_parts);

现在您只有图像文件名。进行查询(我将由您决定,以及任何验证方法并检查地址处的真实文件等)。

如果它得出结果:

 header('Content-type: image/jpeg');
 header('Content-Disposition: inline; filename="adorablepuppy.jpg"');
 print($image_blog);

否则:

 header("HTTP/1.0 404 Not Found");

仅供参考:我不知道这在性能方面是否会很糟糕。但它可以让你做我认为你想做的事情,即使用简单的 image 元素将图像输出为服务器上的平面图像文件。我倾向于同意 BLOB 不是最好的方法,但这确实避免了任何跨浏览器问题。

Another option you might consider (assuming you are on Apache):

Create an .htaccess file with a mod_rewrite for all image extensions (png, jpg, gif).

Have it redirect to a php script that looks up the image requested in the DB. If it is there, it echos out the header and BLOG. If it isn't there, it returns a standard 404.

This way you can have:

<img src="adorablepuppy.jpg" />

Which then gets redirected ala:

RewriteEngine on
RewriteRule \.(gif|jpg|png)$ imagelookup.php

This script does a query for the image, which (obviously) assumes that the requested image has a unique key that matches the filename in the URL:

 $url = $_SERVER['REQUEST_URI'];
 $url_parts = explode("/", $url);
 $image_name = array_pop($url_parts);

Now you have just the image filename. Do the query (which I shall leave up to you, along with any validation methods and checks for real files at the address, etc.).

If it comes up with results:

 header('Content-type: image/jpeg');
 header('Content-Disposition: inline; filename="adorablepuppy.jpg"');
 print($image_blog);

otherwise:

 header("HTTP/1.0 404 Not Found");

FYI: I have no idea if this would be bad in terms of performance. But it would allow you to do what I think you want, which is output the image as though it were a flat image file on the server using a simple image element. I'm inclined to agree that BLOBs are not the best way to go, but this does avoid any cross-browser issues.

沉睡月亮 2024-08-19 06:31:52

我相信您遇到的问题是编码问题。此资源声称您可以使用打印功能。

I believe that the issue that you are encountering is an issue with encoding. This resource claims that you can use the print function.

请帮我爱他 2024-08-19 06:31:52

只需从数据库中获取图像即可。并使用正确的标题打印它。

$image = mysql_fetch_array(...)
header("Content-type: image/jpeg"); // change it to the right extension 
print $image['data'];

出于性能原因......这是不可取的。将图像放入数据库的原因有多种,但最常见的是:

a) 为它们建立索引(废话!)
您可以通过将图像平面存储在服务器上并仅对图像文件名建立索引来实现此目的。

b) 隐藏/保护图像
Flickr 等仍然将图像平面存储在服务器上并使用不同的方法。他们生成一个很难找到的 URL。

链接指向我帐户上的受保护图片。一旦知道正确的 URL,您仍然可以访问它。试试吧!

farm2.static - 一个针对交付静态内容而优化的农场
1399 - 也许是服务器
862145282 - 我的用户名
bf83f25865_b - 图像

为了找到我所有的秘密图像,任何用户都可以使用上述地址猛击 Flickr 并更改最后一部分。但这需要很长时间,并且用户可能会因为用数千个 404 攻击服务器而被阻止。

也就是说,没有理由将图像存储在 BLOB 上。

编辑:
只是一个链接,指向比我解释得更好的人为什么 BLOB 不是存储图像时的最佳选择

Just get the image from the database. And print it using the correct headers.

$image = mysql_fetch_array(...)
header("Content-type: image/jpeg"); // change it to the right extension 
print $image['data'];

For performance reasons... this is not advisable. There are several reasons to put images in databases but the most common are:

a) keeping them indexed (duh!)
You can do this by storing the images flat on the server and just indexing the image filename.

b) keeping the image hidden/protected
Flickr and alike still store the images flat on the server and use a different approach. They generate a URL thats hard to find.

This link points to a protected image on my account. You can still access it once you know the correct URL. Try it!

farm2.static - a farm optimized for delivering static content
1399 - perhaps the server
862145282 - my username
bf83f25865_b - the image

In order to find all my secret images any user can hard hit Flickr with the above address and change the last part. But it would take ages and the user would probably be blocked for hammering the server with thousands of 404s.

That said there is little reason to store images on BLOBs.

Edit:
Just a link pointing to someone that explained much better than I did why BLOB is not the way to go when storing images.

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