简体   繁体   English

如何从mysql blob显示图像

[英]How to display an Image from a mysql blob

I am trying to display an image from a MySQL blob field. 我正在尝试显示来自MySQL Blob字段的图像。 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);

<?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. 在发送二进制图像数据之前或之后,php脚本不会输出任何额外的字符。

  2. The php script is saved as a pure ASCII text file, not as a Unicode/UTF-8 encoded file. php脚本保存为纯ASCII文本文件,而不是Unicode / UTF-8编码文件。 The Unicode/UTF-8 encoded PHP files might include a signature as the first bytes. Unicode / UTF-8编码的PHP文件可能包括签名作为前几个字节。 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. 这些字节在您的文本编辑器中将是不可见的,但是服务器会在JPEG / GIF / PNG数据之前将这几个额外的字节发送到浏览器。 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. 要解决此问题,请在记事本中创建一个空白文本文件,粘贴php代码,然后将文件保存为ANSI编码。

I believe that the issue that you are encountering is an issue with encoding. 我相信您遇到的问题是编码方面的问题。 This resource claims that you can use the print function. 此资源声称您可以使用打印功能。

Another option you might consider (assuming you are on Apache): 您可能会考虑的另一种选择(假设您使用的是Apache):

Create an .htaccess file with a mod_rewrite for all image extensions (png, jpg, gif). 为所有图像扩展名(png,jpg,gif)创建一个带有mod_rewrite.htaccess文件。

Have it redirect to a php script that looks up the image requested in the DB. 让它重定向到一个php脚本,该脚本查找数据库中请求的图像。 If it is there, it echos out the header and BLOG. 如果存在,它将回显标头和BLOG。 If it isn't there, it returns a standard 404. 如果不存在,则返回标准404。

This way you can have: 这样,您可以:

<img src="adorablepuppy.jpg" />

Which then gets redirected ala: 然后将其重定向到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中的文件名匹配的唯一键:

 $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. 但这将允许您执行我想做的事情,即使用简单的image元素将图像输出为服务器上的平面图像文件。 I'm inclined to agree that BLOBs are not the best way to go, but this does avoid any cross-browser issues. 我倾向于同意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!) a)使它们保持索引(du!)
You can do this by storing the images flat on the server and just indexing the image filename. 您可以通过将图像平面存储在服务器上并仅索引图像文件名来实现此目的。

b) keeping the image hidden/protected b)保持图像隐藏/受保护
Flickr and alike still store the images flat on the server and use a different approach. Flickr等仍然将图像平放在服务器上,并使用其他方法。 They generate a URL thats hard to find. 它们生成一个很难找到的URL。

This link points to a protected image on my account. 链接指向我帐户中的受保护图片 You can still access it once you know the correct URL. 知道正确的URL后,您仍然可以访问它。 Try it! 试试吧!

farm2.static - a farm optimized for delivering static content farm2.static-为交付静态内容而优化的服务器场
1399 - perhaps the server 1399年-也许是服务器
862145282 - my username 862145282-我的用户名
bf83f25865_b - the image bf83f25865_b- 图像

In order to find all my secret images any user can hard hit Flickr with the above address and change the last part. 为了找到我所有的秘密图像,任何用户都可以使用上面的地址硬击Flickr并更改最后一部分。 But it would take ages and the user would probably be blocked for hammering the server with thousands of 404s. 但是这将花费很多时间,并且用户可能无法使用数千个404锤击服务器。

That said there is little reason to store images on BLOBs. 就是说,没有理由将图像存储在BLOB上。

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. 只是一个指向某人的链接,该链接的解释要比我解释的要好得多, 为什么BLOB并不是存储图像时要走的路

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM