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

Hi Monks, This is driving me crazy. I'm trying to make a (very) simple CGI program that will open a GIF image from a directory, and print it to STDOUT so that I can call it from an <IMG SRC="thumb.pl?imagenumber=3"> type tags. It seems to work, but for about half of the GIF images I try to read, only ~half of the GIF is displayed, and sometimes that last line of the GIF looks garbled. It looks like it is running into a binary character in the GIF that makes it quit for some reason. Here's the barebones of my code:
open ($myimage, "$imagelocation") or die "cannot open"; print "Content-type: image/gif\n\n"; while (<$myimage>) { print; } close $myimage;
simple, right? I can't figure it out because it works for *some* images but not all... any wisdom? Thanks.

Replies are listed 'Best First'.
Re: weird GIF problem
by Corion (Patriarch) on Jan 03, 2009 at 20:52 UTC

    Most likely, you're on Windows. You should always (even on Unixish OSes, nowadays) use binmode on filehandles you're reading that are not supposed to contain text:

    open ($myimage, '<', $imagelocation) or die "cannot open '$imagelocation': $!"; binmode $myimage; ...
      THANKS! That worked. I knew it was something like that and I tried all manner of binmode(STDOUT) and binmode(STDIN) to no avail. Much appreciated.
Re: weird GIF problem
by fullermd (Vicar) on Jan 03, 2009 at 23:01 UTC

    Good that your problem is solved. Just a few notes on the code:

    open ($myimage, "$imagelocation") or die "cannot open";

    This being a web thing, it may be intentional that you limit the info exposure, but it can be easier if you include what happened in the error message:

    open ($myimage, "$imagelocation") or die "cannot open: $!";

    Also, you can condense the output loop down to a single print:

    print <$myimage>;

    <filehandle> in list context returns all the lines, and print is list context, outputting all the items in its list. And since a binary file doesn't exactly have lines anyway...

      One minor suggestion; I'd modify

      open ($myimage, "$imagelocation") or die "cannot open: $!";

      to

      open ($myimage, "$imagelocation") or die "cannot open $imagelocation: $!";

Re: weird GIF problem
by zentara (Cardinal) on Jan 04, 2009 at 14:39 UTC
    It looks like binmode solved your problem, but one other gif related problem can produce similar symptoms, and you might want to look out for it. If you open an animated gif, that has been highly optimized, the separate images can look corrupted, unless they were extracted with a program that understands the optimizations. IIRC, it's called Frame Optimization, and successive images only show the difference between frames...much like digital tv does now. One of the ways you can filter out animated gifs, from regular gifs, is in perldoc Imager::Files. I think you can try the read_multi method, and see if you get more than one image. There may be a better way.

    I'm not really a human, but I play one on earth Remember How Lucky You Are