1

How to read images from MySQL database using PHP?

If the images are stored in a BLOB in the database, how to use the binary data that I get and turn it into an image using src in <img> or using the CSS property background-image?

Thanks.

1
  • 2
    Seriously consider moving your images out of your database as BLOBs and store them locally in your file system. If multiple machines need access to the same image consider a shared resource that you mount across machines. Commented Jan 23, 2011 at 2:19

2 Answers 2

4

To directly use the binary data as a an image source, you can use the data URI scheme, for example:

$uri = 'data:image/png;base64,'.base64_encode($row['binary-data']);

This URI can then be used directly as the image’s source:

background-image: url(<?php echo $uri; ?>)
<img src="<?php echo $uri; ?>">

But that has some substantial disadvantages: Besides the lack of support for these data URIs in older browsers, data URIs do also have disadvantaged regarding payload, caching, and references.

Sign up to request clarification or add additional context in comments.

Comments

1

What you should probably do, if storing images as BLOBs, is provide a URL that calls your script in a way that it can determine what image to return.

Use that URL as the src, or background-image: url(...) and in the script, read the BLOB from the database in to a variable.

Then output the variable after appropriate header information, telling the browser it is to receive an image, for instance:

header('Content-Type: image/jpeg');

Sending a Content-Length header and sensible information on caching/expiry would also be wise.


NB. Having said all that, I tend to be wary of using BLOBs in databases, they tend to cripple performance. When I want to store images, I store then in some directory structure and reference them in the database in some fashion.

5 Comments

Thanks a lot, I got the concept but still missing what is going to happen in that script that I call from src.
One of my latest questions on stackoverflow was how to do what you have just mentioned, store image on file system and call it when needed. If buidlding generic and configurable cms how can I make reference to image with some item in database, for exammple news item that is in the news table in database.
@SonOfOmer: It really depends on the purpose, if you have news articles, are you storing more than one image per article, or just the one? If it is just the one, then have a directory set aside of article images and use the news table ID as the filename, with suitable extension. Then you can store image width/height/format in the database, and use that to fill in the values of the <img> tag. If more than one, then keep another table, that lists that information per image, with a sequence ID and store them as <articleID>_<seqID>.ext, or just put them in a directory per article.
@SonOfOmer: The script you call from src needs to have sufficient parameters in the URL itself to identify the BLOB in the database that you want. Then simply use a SELECT query to get the BLOB value in to a string. Output the Content-Type and Content-Length header values, and then echo the BLOB value that came back as the result from the query. Finally exit the script. Ensure no other text is sent (other than the BLOB) at all after the headers, not one character.
You can consider alternate image storage methods too: filesystem, S3, CouchDB, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.