tags:

views:

620

answers:

4

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

+1  A: 

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

monksy
if it's not an answer, it's not an answer =(
thephpdeveloper
It was a suggestion.
monksy
A: 

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.

Frankie
Just FYI, I find that managing BLOBs in the DB are perfectly reasonable and sound thinking. It IS easier to manage both in code and in storage. I generally stream the BLOB out of the DB vs. load-n-dump but why hate on BLOBs?
Xepoch
You had my +1 before going off on the tangent about where to store images...
Thilo
@Xepoch I ran into BLOB hell trying to scale a clients site. It is much faster to serve the files statically and just store the links in the database. But other people have it laid out much better than me: http://mysqldatabaseadministration.blogspot.com/2008/01/i-will-not-blob.html
Frankie
@Thilo, I do believe SO is not only about answering the users questions but anticipating his problems and sharing your knowledge. I gave a straight answer to his request and then told him why I thought it was a bad idea. Thank you for your comment!
Frankie
+1  A: 

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.

Anthony
+1  A: 
<?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.

Salman A