开发者

How to display an Image from a mysql blob

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);


<?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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜