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

I am having a problem with my code to return an image. My cgi file I am accessing looks like this
#!c:/perl/bin/perl print "Content-type: text/html\n\n"; print "<html><body><img src='test.cgi?asdf'></body></html>";
and the test.cgi looks like this
#!c:/perl/bin/perl print "Content-type: image/gif\n\n"; open(IMG, "packplan.gif"); print <IMG>; close(IMG);
Now, instead of getting an image like I want when the first cgi script is accessed, i get an "X" like their was no image. What am I doing wrong?

Replies are listed 'Best First'.
Re: Image content help
by ambrus (Abbot) on Jun 17, 2004 at 06:54 UTC

    The second CGI is wrong. It reads only the first line of the image. You'll have to binmode the file., and read the whole, not just one line. (Update: /me was stupid here.)

    Update: also you'll have to chdir to the correct directory if you open the file with a relative path.

      It reads only the first line of the image.

      This is not true. In the code line:

      print <IMG>;

      <IMG> is in list context. It should produce all the "lines" (by whatever definition of "line" $/ (the input record sepatator) gives - default separator is \n), then print should print all those elements joined by $, (the output field separator - by default undefined), followed by the content of $\ (the output record separator - also by default undefined).

      I changed the second code to
      #!c:/perl/bin/perl print "Content-type: image/gif\n\n"; open(IMG, "packplan.gif"); binmode(IMG); my @file = <IMG>; close(IMG); foreach my $line (@file){ print $line; }
      still no go. This is my first time to try this image thing and I am lost. All the help you are giving me is appreciated.
        You forgot to binmode STDOUT.

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

        Again, either use an absolute path in the open, or else chdir to the correct directory.

Re: Image content help
by saskaqueer (Friar) on Jun 17, 2004 at 08:27 UTC

    The HTML page:

    #!perl -w $| = 1; use strict; use CGI; my $CGI = CGI->new(); print $CGI->header('text/html'), <<'END_HTML'; <html> <head> <title>Sample Image Page</title> </head> <body> <img src="test.cgi" /> </body> </html> END_HTML

    The perl script for the image (note that using read() is preferable compared to reading line-by-line when handling binary data such as images, as you may be pushing a lot of data into memory when you don't want to):

    #!perl -w $| = 1; use strict; use CGI; my $CGI = CGI->new(); print $CGI->header('image/gif'); open( my $img, '<', 'packplan.gif' ) or die("open failed: $!"); binmode($img); binmode(STDOUT); while ( read($img, my $buf, 1024) ) { print $buf; } close($img) or die("close failed: $!");
      Oh, I understand, so if you binmode(STDOUT) then it is binmoded for the rest of the script?

        Correct.