Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Displaying images stored in mysql via webbrowser

Reply
Thread Tools

Displaying images stored in mysql via webbrowser

 
 
Stuart Weaver
Guest
Posts: n/a
 
      01-16-2004
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";
}

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      01-16-2004
Stuart Weaver <(E-Mail Removed)> wrote:


> 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.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
gnari
Guest
Posts: n/a
 
      01-16-2004
"Stuart Weaver" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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(<td align="center"> \n);

> $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




 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      01-16-2004
On Fri, 16 Jan 2004 21:06:43 +0000, Stuart Weaver wrote:

> 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--
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      01-16-2004
On Fri, Jan 16, Sherm Pendley inscribed on the eternal scroll:

> 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.
 
Reply With Quote
 
Stuart Weaver
Guest
Posts: n/a
 
      01-16-2004
On Fri, 16 Jan 2004 15:24:51 -0600, Tad McClellan wrote:

> 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.

 
Reply With Quote
 
Stuart Weaver
Guest
Posts: n/a
 
      01-17-2004

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Opening a webpage in the background via webbrowser.open() Tim Chase Python 0 10-08-2010 11:28 PM
Displaying Stored Procedure results in a textbox =?Utf-8?B?bmVpbF9wYXQ=?= ASP .Net 1 03-04-2005 07:23 PM
Displaying images stored as binary in MySQL Blaktyger Python 5 08-16-2004 10:06 PM
Displaying images thats stored in Oracle database in CGI script Robert Nilsson Perl Misc 5 02-25-2004 09:49 AM
Displaying images from a MySQL Database Matt ASP .Net 0 08-01-2003 10:25 AM



Advertisments