Displaying images stored in mysql via webbrowser

S

Stuart Weaver

hello, I am fairly new to perl, please bare with me.

I have written a perl script that is intended to be use via cgi.
Given search parameters of a first and/or last name the script searches a
mysql database and in return is intended to display an image of that person.
The desired result is to display the image in-line on the fly, I do not wish
to write the file to disk and then use a 'img src' html tag to display the
image. The problem I am having is the image is displayed in binary format. I
believe I am displaying the data retrieved from the mysql database incorrectly.

I would greatly apricate any help. Thanks in advance.

Stuart

Snip-it of code follows:
If including the whole script would help please let me know.


$sql = qq{ SELECT image FROM pic WHERE fn = ? or ln = ? };
$sth = $dbh->prepare($sql);
$sth->execute($fn,$ln);
my $pic = $sth->fetchrow_array || "";
if ( "$pic" eq "" ) { print "Nothing Found \n\n"; }
else
{
print "<td align=\"center\"> \n";
print "$fn $ln <br><br> \n";
my $image_type;
if (substr($pic,0,3) =~ 'GIF' )
{
$image_type = 'image/gif';
}
elsif (substr($pic,0,10) =~ 'JFIF' )
{
$image_type = 'image/jpeg';
}
print "$image_type \n";
print $pic;
print "<br> \n";
}
 
T

Tad McClellan

The problem I am having is the image is displayed in binary format.


What does that mean?

Does it mean you want text format as opposed to binary format?

Did you binmode() the filehandle?

my $pic = $sth->fetchrow_array || "";
if ( "$pic" eq "" ) { print "Nothing Found \n\n"; }
^ ^
^ ^ a useless use of double quotes


From the Perl FAQ:

What's wrong with always quoting "$vars"?


I'd do it this way instead:

if ( my $pic = $sth->fetchrow_array ) {
# do stuff
}
else {
# not found
}

if (substr($pic,0,3) =~ 'GIF' )


A pattern match should *look like* a pattern match:

if (substr($pic,0,3) =~ /GIF/ )

You should use an equality test to test for equality:

if (substr($pic,0,3) eq 'GIF' )

print "$image_type \n";


You need a blank line following the headers.
 
G

gnari

Stuart Weaver said:
hello, I am fairly new to perl, please bare with me.
[snip problem of emmiting dynamic image from cgi]

(will not mention things others have commented about)
$sql = qq{ SELECT image FROM pic WHERE fn = ? or ln = ? };
$sth = $dbh->prepare($sql);
$sth->execute($fn,$ln);
my $pic = $sth->fetchrow_array || "";
this reads to me as : fetch an array and stash it into a scalar
I would prefer something like:
if (my ($pic) = $sth->fetchrow_array()) {
doyourstuff...
} else { print "Nothing Found \n\n"; }
print "<td align=\"center\"> \n";

you need to read up on how cgi works.
you need to print out CGI headers to specify a mimetype
other that text/html, in this case the gif or jpeg types.
you must print those BEFORE any thing else.
and whats more, the image should not contain any
<td> tags. <td> tags are very bad for images.
visit some of the cgi newsgroups for info about this.

apart from that, the previous line looks better as:
print qq( said:
$image_type = 'image/jpeg'; ....
print "$image_type \n";

this may be an attempt at CGI header, I do not know.
in that case, the header name would be helpful.
print "Content-Type: $image_type\n\n";
notice extra \n.
print $pic;

binmode, maybe?

1 more comment. it is akward to extract the mimetype
from the guts of the binary image at each display. It
would be neater to keep an extra column in the table
for that.

gnari
 
S

Sherm Pendley

The desired result is to display the image in-line on the fly, I do not wish
to write the file to disk and then use a 'img src' html tag to display the
image.

HTML does not allow for image data to be directly embedded. If you need to
provide a single file that contains all of your output, including embedded
images, you should have a look at the various modules for creating PDF
files.

Having said that, there's no reason that the src attribute of an HTML img
tag can't point to a CGI, so what you want to do is certainly possible
without creating a temp file on disk. Simply create a link to a second
CGI, passing to it enough information so that it can retrieve the image
data you need - something like <a href="showimage.cgi?id=abc123">.

sherm--
 
A

Alan J. Flavell

HTML does not allow for image data to be directly embedded.

OT here, but:

HTML provides mechanisms for linking to other resources (img, object,
and the non-standard embed) via their URI. HTML is not in the least
concerned what those URIs are: as far as HTML is concerned it's just
an opaque token, subject to some syntax rules (URI-encoding and all
that). If the URI directly contains the data, then HTML makes no
attempt to disallow that; so your claim is factually inaccurate.

Admittedly, not all browsers actually _support_ those forms of URI
which contain literal data. And it's a rather inefficient format for
images of any substantial size. But nevertheless, it's clearly
defined (RFC), and supported by some WWW-compatible browsers.
Having said that, there's no reason that the src attribute of an HTML img
tag can't point to a CGI, so what you want to do is certainly possible
without creating a temp file on disk.

This is the normal procedure, indeed.
 
S

Stuart Weaver

Does it mean you want text format as opposed to binary format?

The contents of the image is being displayed, like if I were to cat out a
jpeg on the command line.

^ ^ a useless use of double quotes
From the Perl FAQ:

What's wrong with always quoting "$vars"?

Thanks for pointing that out. I have read that portion of the FAQ and
will be reading the rest.

I'd do it this way instead:
A pattern match should *look like* a pattern match:
You should use an equality test to test for equality:
You need a blank line following the headers.

Thank you for all the suggestions I will be making some updates.
 
S

Stuart Weaver

Thank you to everyone for your help. I got it working using the following.

I am also going to take gnari's suggestion and create another
column to store the type of image in the database.

Once again thanks everyone, I am still learning and apricate all the
suggestions and comments.

Stuart.


$sql = qq{ SELECT image FROM pic WHERE fn = ? or ln = ? };
$sth = $dbh->prepare($sql);
$sth->execute($fn,$ln);
if ( my $pic = $sth->fetchrow_array )
{
my $image_type;
if (substr($pic,0,3) =~ /GIF/ )
{
$image_type = 'image/gif';
}
elsif (substr($pic,0,10) =~ /JFIF/ )
{
$image_type = 'image/jpeg';
}
print "Content-Type: $image_type\n\n";
print $pic;
}
else
{
php_header;
print "Nothing Found \n\n"; }
php_footer;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top