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

Thanks for the help ahead of time.

What I am trying to do is return a png file from a cgi script that is called by other files in the following way
<img src="http://www.xyz.com/cgi-bin/genImage.cgi">
The script works fine it drops the cookies it is suppose to and I know it reads the image file in correctly from some testing I have done but the calling file displays a broken image link instead of the picture. When i call the script directly it displays: ‰PNG

I cannot use the GD mod because I cannot get it installed on my server. Here it the code that returns the image does anyone have any ideas what is going on. Keep in mind I am positive it reads the image file and the file does exist. Thanks again
print "Expires: Fri, 30 Oct 1998 14:19:41 GMT\n"; print "Content-type:image/png\n\n"; open IMAGE, "e:/image1.png"; my ($image, $buff); while(read IMAGE, $buff, 1024) { $image .= $buff; } close IMAGE; #binmode STDOUT; #tried this with no success as well #print STDOUT $image; #tried this with no success as well print $image;

Replies are listed 'Best First'.
Re: return image from CGI scrript error
by PodMaster (Abbot) on Oct 21, 2003 at 16:58 UTC
    You need to binmode STDOUT and IMAGE, and add error checking
    print "Expires: Fri, 30 Oct 1998 14:19:41 GMT\n"; print "Content-type:image/png\n\n"; open IMAGE, "e:/image1.png" or die "ERROR $!"; binmode IMAGE; binmode STDOUT; my ($image, $buff); while(read IMAGE, $buff, 1024) { $image .= $buff; } close IMAGE; #binmode STDOUT; #tried this with no success as well #print STDOUT $image; #tried this with no success as well print $image;

    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.

      thanks...it didn't work because i didn't
      binmode IMAGE;
      Good eye!
Re: return image from CGI scrript error
by hardburn (Abbot) on Oct 21, 2003 at 15:56 UTC
    print "Expires: Fri, 30 Oct 1998 14:19:41 GMT\n";

    Hrm, you're sending an Expires date of about five years ago. Just how that will be handled is probably browser-specific, but I suspect this is your problem. Don't hardcode the expire data. Instead, use Date::Manip (or a similar module) to add a certain ammount of time to the current data.

    Also, you're creating more work for yourself and using memory unnecessarily. Instead of buffering the image, print it out as you get it:

    open IMAGE, "e:/image1.png"; my $buff; while(read IMAGE, $buff, 1024) { print $buff; } close IMAGE;

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    :(){ :|:&};:

    Note: All code is untested, unless otherwise stated

      the reason for the old expiration is i don't want the image to be cached...is there a better way of doing this?

        Yes.

        Pragma: no-cache Cache-control: no-cache

        Which should work in all major browsers. See also: Issuing Correct HTTP Headers on perl.apache.org.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        :(){ :|:&};:

        Note: All code is untested, unless otherwise stated

Re: return image from CGI scrript error
by Taulmarill (Deacon) on Oct 21, 2003 at 15:53 UTC
    hm, works on my mashine. should be something else.
    is the image valid?