Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

display gif image

by indapa (Monk)
on Apr 26, 2001 at 23:31 UTC ( #75874=perlquestion: print w/replies, xml ) Need Help??

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

Hi,
How do you get a CGI script to display a gif image? I'm writing a CGI script which the user enters a file which is used to generates a gnu plot. The module I downloaded from CPAN outputs the gnuplot as a *.gif file, which I would like to display. I took a look at the Q and A section on : How can I use a CGI script to return an image? I tried one of the solutions as follows:

open (GIF, "/path/to/gif/") || die "$!\n"; my ($image, $buff); while (read GIF, $buff, 1024) { $image.=$buff; } close GIF; print "Content-type: image/gif\n\n"; print $image;
When I run the CGI script from the command line, it prints out a whole bunch of funny looking characters. But when I run it from the web page, no image is displayed. Is there a better solution to this? I appreciate the help.

Replies are listed 'Best First'.
Re: display gif image
by merlyn (Sage) on Apr 27, 2001 at 00:55 UTC
    My guess (since you didn't say) is that you're on ActiveState Perl and you forgot to say binmode on your GIF handle.

    But you can avoid even that by using a very efficient file-copy method:

    use File::Copy; print "Content-type: image/gif\n\n"; copy "/path/to/gif", \*STDOUT;

    -- Randal L. Schwartz, Perl hacker

Re: display gif image
by tadman (Prior) on Apr 27, 2001 at 00:53 UTC
    What you have is on the right track, but you might want to take a few other precautions:
    #!/usr/bin/perl -w use strict; use CGI::Carp; use CGI; my ($cgi) = new CGI(); my ($gif_name) = 'whatever.gif'; # Or $cgi->parameter('show') my ($image); open (GIF, $gif_name) || die "Could not open $gif_name: $!\n"; binmode (GIF); read (GIF, $image, -s $gif_name); close (GIF); binmode (STDOUT); print $cgi->header("image/gif"); print $image;
    Note the use of '-s' to find the complete size of the file, which allows you to read it in one go.

    CGI::Carp will allow you to record any run-time errors to the appropriate error_log file, especially those messages generated when you 'die', which normally go sight-unseen.

    Update: Added 'binmode(GIF)' as pointed out by dws below, and merlyn as well (Thanks!) No sense in posting 'bad' code.
      If you're on Win32, you'll need to add   binmode(GIF);
      (Ah, merlyn caught this, too.)
Re: display gif image
by ckohl1 (Hermit) on Apr 27, 2001 at 00:11 UTC
    One solution would be to create the GIF file in a path that is available through your web server (e.g. images). Then use the relative web path in your CGI script:
    # Open the file: open (GIF, "/home/httpd/images/my_perl_gif.gif") || die "$!\n"; my ($image, $buff); while (read GIF, $buff, 1024) { $image.=$buff; } close GIF; # Now send the information to the browser: print "Content-Type: text/html\n\n"; print '<html><body>'; print "<img src='/images/my_perl_gif.gif' alt='My GIF'>"; print '</body></html>';
    Chris
      Update:

      I just realized that the file code you posted was an attempt to output the GIF to the agent browser. At first, from the context of your post, I had made the assumption that you were building the GIF with your snippet.

      What you want to do is simply tell the agent, calling on the web server, where the file to be displayed lives. You can do that using the IMG tag. You will also want to build the GIF in a place that is available to web agents.

      I apologize for my hurried initial reply.

      Chris

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://75874]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2019-12-12 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?