If you're talking about using a database (such as mysql) to store images
for later display/perusal on a web page, I believe I can offer a bit of
aid.
First - it's a bad idea. Databases add a number of layers of overhead
for the access of BLOB data as opposed to the filesystem of whatever OS
you're running. The *best* solution for speed and performance (and
probably for maintenance as well) is to use the database to store the
file reference information (filename) rather than the file itself. this
allows you to still attach images to database records by saving the file
location with the record, and avoids the overhead of the database doing
the lookup to get the information.
my $sth = $db->prepare(qq(SELECT picture FROM table WHERE key = ?));
$sth->execute('recordxx');
my ($Image) = $sth->fetchrow_array(); #sets $Image to "recordxx.jpg";
print qq(<img src="/images/$Image">);
Using thew above example, you've only had to retrieve the varchar field
which stores the filename.
But, assuming for whatever reason you _really_ do decide to store the
files themselves in the database, it's still possible to do what you
want, although you'll be incurring significant overhead when perl needs
to allocate scalar variables to store the 100+ Kb images (even if only
temporarily).
# pic_bin contains a jpeg image
my $sth = $db->prepare(qq(SELECT pic_bin FROM table WHERE key = ?));
$sth->execute('recordxx');
my ($Image) = $sth->fetchrow_array();
print CGI->header('image/jpeg'); # prints Content-type: image/jpeg\n\n
print $Image;
The trick to remember is that if your script is in the middle of
printing an HTML page, you can't suddenly switch and print out a JPEG
(or whatever) image. You'll need to have the initial script print an
image <img.. tag referencing the perl script which gets the image from
the database and prints it. Just fill in the name and query string
which will bring up the record as the src.
eg:
<img src="/this_script.pl?action=get_image&image_key=imagekey">
I do ALOT of dynamically generated websites, and have never come across
a reason to store the images (even when I've got thousands of them) in
the database. The only time I let perl print the binary data for an
image is if perl had to generate the image itself from scratch.
Hope that helps,
--A
bob hwan wrote:
>
> Could anyone please send me any code which will allow
> me to display Blobs (Binary Large Objects)from a Sql
> database, on my web page using perl!
>
> it would me most helpfull.
>
> Bob Hwan.
>
> ____________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
> or your free @yahoo.ie address at http://mail.yahoo.ie