Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I was wondering how I could display an image with a CGI program? What I did was...
Using ms-paint, I made a one pixel .gif image and saved it to a file.
Then I wrote a simple perl/cgi program...

#!/usr/bin/perl use CGI qw/:all/; print "Content-type: text/html\n\n"; my $length = 50; binmode STDOUT; print <<HTML; <table> <tr> <td><img src='/cgi-bin/pixel.gif' height='20' width='$length' +alt='test'/></td> </tr> </table> HTML


A broken image tag was displayed. I also tried 'image/gif' for the 'Content-type' but the result was the same. The image file is in the cgi-bin directory.

Thank you.

Replies are listed 'Best First'.
Re: Display an image with a CGI program?
by NetWallah (Canon) on May 25, 2009 at 22:28 UTC
    • binmode STDOUT; is not necessary, if you are generating HTML.
    • Images are typically stored in a separate directory - typically '/images'
    • Web server (apache) permissions for cgi-bin allow program execution but usually do not allow content display
    You may want to move your image to a different directory, then try to display it using plain HTML.

    After that succeeds, you can include that HTML in your perl code.

    More typically, the cgi-bin script would read the image, and render it directly, but the cgi-bin script would be called from HTML, so the image , which is dynamically generated is embedded in a web page.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

Re: Display an image with a CGI program?
by jettero (Monsignor) on May 25, 2009 at 22:27 UTC

    Your image problem probably has nothing to do with your perl program. It's probably a permissions problem. If you want to have perl display your image, it'd look more like this, but you wouldn't normally do this since having apache (or IIS) pick up perl and an image gets kindof heavy. If you want to print images, consider FCGI or mod_perl or something to keep perl picked up.

    open my $file "<", $image_file or die "grr: $!"; # (You shouldn't print the header manually if you use CGI) print "Content-type: image/gif\n\n"; print $_ while <$file>;

    Other notes: use strict and use warnings. Not using them will cause you trouble eventually. Also consider using a template package like HTML::Template. Also, don't load CGI if you're not going to use it for something. It's pretty big.

    use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); my $cgi = CGI->new; print $cgi->header; # use $cgi->header("image/gif") for gifs print $cgi->start_html; print $cgi->table( $cgi->Tr( $cgi->td([ $cgi->img({src=>"blarg"}) )));

    -Paul

Re: Display an image with a CGI program?
by ikegami (Patriarch) on May 26, 2009 at 03:04 UTC

    The image file is in the cgi-bin directory.

    Some servers are treat all files in the cgi-bin as scripts. Your .gif isn't a script, so the web server returns an error. Try moving it to a different directory.

    If that doesn't fix it, tell us what HTTP error the server returned. That would be in the web server's access log.

      Hi.

      Thanks for the help!

      I moved the image file to the parent directory of both 'htdocs' and 'cgi-bin' then referenced it with '../cgi-bin/pixel.gif' and it worked.
        i am also facing a similar kind of problem. Below is my code.The image is not displayed. Can anyone help me out.
        use CGI qw(:all); print "Content-type:text/html\n\n"; print '<HTML> <head></head> <body> <h1> <img src="../images/example_product_loading.gif" alt="site_imag +e" width="800" height="200"> Test Imageeeee. </h1> </body> </HTML>';