rendering images from a database

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

If you have a table in a database with an ID field (int), name
(varchar), a content-type field (varchar) and a data field (blob), and
you have binary data in the data field (like GIFs, JPEGs, PDFs, etc.),
you can display these directly in a new HTML page. A query from the
the database returns this:
  id => 666
  name => greg.jpg
  content-type => image/jpeg
  data => &w`l *=EF=BF=BD m :=EF=BF=BD=EF=BF=BDm=01F=EF=BF=BD=CE=84=14=

How would you embed this data in an HTML page with a Content-Type of
text/html? The <img> element needs a src attribute, which I don't
have, and the <object> element likewise needs a URI for the data

I can certainly display the "data" from the database, but it's
character based data (clubs, diamonds, hearts, and spades) rather than
the nice, pretty picture that's really there. Is there a PM that can
take a binary and turn it into something that will look like an image
in a browser?

Thanks, CC.

Re: rendering images from a database

Quoted text here. Click to load it

The src attribute can refer to a script, i.e. "src=''".
Then in, you'd fetch the image data and send it.


Re: rendering images from a database

Quoted text here. Click to load it

This is the correct approach :). An alternative would be to construct a
data: url and supply that as the value of the src attribute. URI knows
how to correctly construct data: urls from arbitrary data; whether
browsers are willing to accept the very long urls necessary is another
matter entirely.


Site Timeline