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

I am trying to print a image to the screen without SSI. I wrote the following code:
print "Content-type: image/gif\n\n"; open(GIFIMAGE, "agif.gif"); while (<GIFIMAGE>) {print $_}; close(GIFIMAGE);
The previous code works with very small images (1x1 px), but when the files get bigger, perl dosen't print it correctly. What is wrong?

Replies are listed 'Best First'.
Re: Print Image File
by turnstep (Parson) on Mar 30, 2000 at 04:33 UTC

    You need to output one other http header besides the Content-type:
    Content-Length

    This should be set to the length of your file (obviously). Also, you don't need the $_ which is the default for print. Try this:

    $gif = "agif.gif"; $gifsize = (-s $gif); print "Content-Length: $gifsize\n"; print "Content-type: image/gif\n\n"; open(GIFIMAGE, "$gif"); while (<GIFIMAGE>) {print; }; close(GIFIMAGE);

    Simple as that! If it still doesn't work, please let us know.

RE: Print Image File
by Anonymous Monk on Mar 11, 2000 at 22:35 UTC
    You probably need to binmode your filehandle...
Re: Print Image File
by Anonymous Monk on Mar 11, 2000 at 23:28 UTC
    You might want to print the whole file at once... as in
    print do { local $/; <GIFIMAGE> };