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

Dear Monks,

I am seeking your wisdom in order to understand what happened to some simple code that was working fine under Active Perl Win2K. I started pulling my hair when I ported the code onto a UNIX server running iPlanet.

See, this code is just reading and displaying a gif file... The top of the image looks fine but the bottom of the image (I would say the last 1/10th of it) is all garbled...

Can YOU PLEASE TELL ME WHY ?!?
... use CGI qw(header path_info); my $file = getNoImage($SHOP); print header(-Content_type => 'image/gif', -Content_length => length $ +file), $file; ... sub getNoImage { my $SHOP = shift || croak ("ProcessAction : Missing Shop data"); my ($chunk, $image); open IMAGEFILE, "$SHOP->{-TEMPLATE_PATH}/img/MyFile.gif" or die "Can +not open file: $!\n"; binmode IMAGEFILE; while ( read(IMAGEFILE, $chunk, 1024) ) { $image .= $chunk; } close IMAGEFILE; return $image; }

Replies are listed 'Best First'.
Re: Image not displayed properly
by tedrek (Pilgrim) on Aug 18, 2003 at 21:25 UTC

    my first instinct is you transfered the gif using ftp and forgot binary mode.

      Gosh... I really I wish it would be that simple...

      Unfortunately for me... I already checked and the file itself is fine... It displays nicely in an IMG tag
        Why not just use an IMG tag then?
Re: Image not displayed properly
by sauoq (Abbot) on Aug 18, 2003 at 21:26 UTC

    It is doubtful that this has anything to do with your CGI script or with Perl. More likely, you corrupted the gif image file when you moved it from Windows to Unix. Did you use FTP in ASCII mode by accident?

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Image not displayed properly
by bean (Monk) on Aug 18, 2003 at 23:36 UTC
    I'll take a wild stab at this...

    Is the image garbled after the 1024th byte (or some multiple thereof), perhaps? I always have to look up how to handle binary information in Perl and don't have a reference handy, but I'll bet the .= operator does not always Do The Right Thing (TM) with binary data. You know the size of the file - try reading the whole thing in all at once. Or use pack.
    Update
    Another possibility - you may need to set your output handle to binmode as well... Here's a snippet
    $gifname = "picture.gif"; open(GIF, $gifname) or die "can't open $gifname: $!"; binmode(GIF); # now DOS won't mangle binary input from GIF binmode(STDOUT); # now DOS won't mangle binary output to STDOUT while (read(GIF, $buff, 8 * 2**10)) { print STDOUT $buff; }
    that looks like it does exactly what you want. I found it in the O'Reilly Perl Cookbook Sample Chapter 8. O'Reilly rocks.
    Update 2
    On second thought, the snippet still doesn't handle the problem of concatenating binary data. Oh well.
      I tried reading the whole file at once. Doesn't do the trick.

      Really I don't know what to do else...

      HEEEELP
        Mr bean says :
        Another possibility - you may need to set your output handle to binmode as well ...

        Do you do this? Your response was "I tried reading the whole file at once. Doesn't do the trick. " which is completely disconnected. binmode is the trick. Did you binmode?

        FYI - binmode is not just for "dos"

        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.

Re: Image not displayed properly
by esh (Pilgrim) on Aug 19, 2003 at 02:35 UTC

    If you happen to be in mod_perl, check out the send_fd() method on the Request object.

    Here's sample (untested) code based on the Eagle book:

    my $fh = Apache::gensym(); open $fh, "..." or die "Cannot open file: $!\n"; $r->send_fd($fh); close($fh);

    -- Eric Hammond