Displaying images stored in mysql via webbrowser

Discussion in 'Perl Misc' started by Stuart Weaver, Jan 16, 2004.

  1. 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";
    }
    Stuart Weaver, Jan 16, 2004
    #1
    1. Advertising

  2. Stuart Weaver <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 16, 2004
    #2
    1. Advertising

  3. Stuart Weaver

    gnari Guest

    "Stuart Weaver" <> wrote in message
    news:p...
    > 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
    gnari, Jan 16, 2004
    #3
  4. 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--
    Sherm Pendley, Jan 16, 2004
    #4
  5. 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.
    Alan J. Flavell, Jan 16, 2004
    #5
  6. 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.
    Stuart Weaver, Jan 16, 2004
    #6
  7. 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;
    }
    Stuart Weaver, Jan 17, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    0
    Views:
    383
  2. =?Utf-8?B?bmVpbF9wYXQ=?=

    Displaying Stored Procedure results in a textbox

    =?Utf-8?B?bmVpbF9wYXQ=?=, Mar 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    393
    Karl Seguin
    Mar 4, 2005
  3. Blaktyger
    Replies:
    5
    Views:
    669
    Blaktyger
    Aug 16, 2004
  4. Tim Chase
    Replies:
    0
    Views:
    448
    Tim Chase
    Oct 9, 2010
  5. Robert Nilsson
    Replies:
    5
    Views:
    141
    Joe Smith
    Feb 25, 2004
Loading...

Share This Page