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

The following code i didn't write prints a 1x1 transparent GIF to the browser:
print "Content-type: image/gif\n\n", pack "H*", "47494638396101000100800000ffffff" . "00000021f90401000000002c00000000" . "010001000002024401003b";
My question: Is it possible to somehow encode and embed a larger gif inside a script for displaying whenever needed? ie:
&showgif(1); or something..
thanks - epoptai

Replies are listed 'Best First'.
Re: Embed GIF in perl source?
by Fastolfe (Vicar) on Dec 07, 2000 at 03:37 UTC
    Two things:

    Firstly, you're probably better off putting the images in separate files. This is mainly for a maintenance reason. Somebody coming along later needing to update your script because one of the images needs to be updated is going to have a hell of a time doing it. If your CGI script is being used in an image context, you're better off having the CGI script open a real .gif file on your local filesystem and just sending the data to the browser.

    Secondly, your example of &showgif(1) makes me wonder if you are hoping to be able to use this function multiple times in your CGI script. If so, you can't. Your CGI script can only output a single content type (unless you want to go with some multipart encoding, but I don't know if browsers have enough of a sophisticated support for that sort of thing to be able to interpret it in a way that would make what you're trying to do remotely useful). Any attempts to display multiple GIFs one after the other will fail, because the browser is expecting and assuming the content from that request will be a single item.

    You may also wish to take a look at, say, MIME::Base64, which will let you encode/decode binary items in such a way so as to continue to be printable (which seems to be your goal) while being a bit more efficient space-wise than a simple hex string. It'll also take slightly longer to process.

      You got me Fastolfe. I was hoping to include multiple images in the script and use them where needed. I should have realized the header would prevent it. Thanks for the module suggestion, i'll look into that.

      And thanks to everyone else for the informative responses. I was hoping for just a hint or clue and got plenty plus a few working scripts! Where has perlmonks been all my life? :-)

      your pal - epoptai

Re: Embed GIF in perl source?
by chipmunk (Parson) on Dec 07, 2000 at 03:24 UTC
    Yes, you would probably do it similarly to the snippet you posted.

    Here's a simple script that converts a file to a hex encoding:

    #!/usr/local/bin/perl -w # convert binary file to hex use strict; @ARGV or die "Usage: $0 <filename>\n"; my $file = shift; open(FH, $file) or die "Can't open $file: $!\n"; binmode(FH); my $buf; while (read(FH, $buf, 32)) { # 32 bytes fit nicely on one line when hex-encoded print unpack('H*', $buf), "\n"; } __END__
    And here's a script that converts a hex encoding back to the original data:
    #!/usr/local/bin/perl -w use strict; $| = 1; while (<DATA>) { chomp; print pack('H*', $_); } __END__ 23212f7573722f6c6f63616c2f62696e2f7065726c202d770a0a2320636f6e76 6572742062696e6172792066696c6520746f206865780a0a7573652073747269 63743b0a0a4041524756206f7220646965202255736167653a202430203c6669 6c656e616d653e5c6e223b0a0a6d79202466696c65203d2073686966743b0a0a 6f70656e2846482c202466696c6529206f7220646965202243616e2774206f70 656e202466696c653a2024215c6e223b0a62696e6d6f6465284648293b0a0a6d 7920246275663b0a0a7768696c652028726561642846482c20246275662c2033 322929207b0a20207072696e7420756e7061636b2827482a272c202462756629 2c20225c6e223b0a7d0a
    Of course, you could store the encoded gif in your script however you wish; a here doc would be an excellent way to do it.
Re: Embed GIF in perl source?
by mdillon (Priest) on Dec 07, 2000 at 03:31 UTC
      And check out Hex Embedded Images as well...
      # hex up your image open IMG, "$ARGV[0]" or die "Couldn't open $ARGV[0]: $!\n"; binmode(IMG); undef $/; $image = <IMG>; $hex = unpack("H*", $image); close IMG; while ($txt = substr($hex,0,32,'')) { print "'$txt'\n"; }

      # print your hexed stuff out print "Content-type: image/gif\n\n"; print Image(); sub Image { my $image = '4749463839610f001000a2ff00848684' . 'dfe0dfc6c7c6c0c0c000000000000000' . '000000000021f90401000003002c0000' . '00000f00100040034048baacf3608049' . 'a98824ca0aae78d9b375a4164a92470e' . '016a8dde10721c48dfc029ee528b0ac0' . 'e06717121a8132cd6729b4ed8cc4c891' . '97a45a09b286569100003b00'; return(pack("H*",$image)); }


      #!/home/bbq/bin/perl
      # Trust no1!
Re: Embed GIF in perl source?
by thoglette (Scribe) on Dec 07, 2000 at 13:52 UTC
    Rather than helping, let me extend the question: can't you do it algorithmically? I don't know of a CPAN module to create GIFs (or JPGs) but it's not rocket science (no more so that Tk widgets) Butlerian Jihad now!
Re: Embed GIF in perl source?
by turnstep (Parson) on Dec 07, 2000 at 22:00 UTC