http://qs321.pair.com?node_id=20028

This is more of a thank-you note than a cool use for perl. But since I think it IS cool, here we go:

With the help of httptech, mdillon, chromatic, Corion and others (the list gets long), I managed to unpack image files in hex mode, and embed them directly into a perl script. This will allow me to build a self-sufficient script, without the need for extra image files in HTML, and make it as simple as can be to install the script on any server. All you have to do, is upload one file, chmod it, and its ready to go. When the script is called with a ?img=foo it re-packs the image and sends it back to the client. There are a few obvious downsides to this, but for small scripts, small images, I think its a pretty clever way to be user friendly.

So, without further ado, you can get the source at http://johnny.warp.psi.br/images.txt, and you can see the ouput images (all shamelessly "borrowed" from Apache) here, here, here and here!

Thanks again everyone! I had a great time learning on this one. Peers are the best teachers.

Replies are listed 'Best First'.
Re: Hex Embedded Images
by epoptai (Curate) on Dec 22, 2000 at 04:09 UTC
    Sounds excellent. Where's the code?
      Ooops, that server has gone away... The code went something like this (not exactly, but perty close).
      #!/usr/bin/perl -w use CGI; use CGI::Carp 'fatalsToBrowser'; CGI::ReadParse(*in); print "Content-type: image/gif\n\n"; print Images($in{img}); exit; sub Images { my $image = $_[0]; my %IMG; $IMG{dir} = '47494638396114001600c20000ffffff'. 'ffcc99ccffff99663333333300000000'. '000000000021fe4e5468697320617274'. '20697320696e20746865207075626c69'. '6320646f6d61696e2e204b6576696e20'. '4875676865732c206b6576696e684065'. '69742e636f6d2c2053657074656d6265'. '7220313939350021f90401000002002c'. '000000001400160000035428badcfe30'. 'ca4959b9f8ce12baef45c47d64a629c5'. '407a6a8906432cc72b1c8ef51a13579e'. '0f3c9c8f05ec0d4945e171673cb2824e'. '2234da495261569856c5ddc27882d46c'. '3c2680c3e6b47acd232c4cf08c3b0100'. '3b'; $IMG{bomb} = '47494638396114001600e30000ffffff'. 'ffcc33ccffffccccccbbbbbb99999988'. '88886666665555553333330000000000'. '0000000000000000000000000021fe4e'. '546869732061727420697320696e2074'. '6865207075626c696320646f6d61696e'. '2e204b6576696e204875676865732c20'. '6b6576696e68406569742e636f6d2c20'. '53657074656d62657220313939350021'. 'f90401000002002c0000000014001600'. '00048f30c949a7b85824c0bbdf49766d'. '5e4986d916000ab07624808eddfb7dd8'. 'a60449d0768a447048e3040a258572a9'. 'a0ad14055ba7402d34351fe16f4abdc6'. '0008a85631987a3d6204f540e6588b1c'. 'a51a603884cb6f2cba40a0db8500795f'. '2c557f808249078a08124a8167688b8d'. '8e881e03418d42789096434a658f709d'. '9f66a225a7820955abac5442194cb1b2'. '5711003b'; $IMG{back} = '47494638396114001600c20000ffffff'. 'ccffff99999966666633333300000000'. '000000000021fe4e5468697320617274'. '20697320696e20746865207075626c69'. '6320646f6d61696e2e204b6576696e20'. '4875676865732c206b6576696e684065'. '69742e636f6d2c2053657074656d6265'. '7220313939350021f90401000001002c'. '000000001400160000034b18badcfe23'. '10f26a103353bba2cedcf5699c374e16'. '768290740605372a451c6e586d2ffb92'. 'eb354650f103ea78c6a491a66c16284e'. 'a720da8cc0a83a824018d55692ded055'. 'fce891cf8b04003b'; return($IMG{$image}); }
      Enjoy!

      #!/home/bbq/bin/perl
      # Trust no1!
RE: Hex Embedded Images (5.004 is different)
by ybiC (Prior) on Jun 28, 2000 at 11:02 UTC
    I have to agree - it IS cool.

    But when I try BBQ's code to unpack an image to hex, I receive the following error:

    Too many arguments for substr near "'')" Execution aborted due to compilation errors

    Suggestions from more advanced brothers ?

    Perl 5.004
    Debian 2.1

    update 6-28-2000 : btrott pointed out that substr in Perl 5.004 is different, and provided this code (that does work with 5.004)

    while ($txt = substr($hex, 0, 32)) { substr($hex, 0, 32) = ""; print "'$txt'\n"; }

    Thanks also to swiftone, chromatic, and of course BBQ.
        ybiC
      That's probably because you copied the code from my home node yesterday. I had binmode in the wrong place (after undef $/, not a good thing) and one thing probably lead to another. Is this what you have?
      #!/usr/bin/perl open(IMG,'foo.gif') or die("Couldn't open image: $!\n"); binmode(IMG); undef($/); $image = <IMG>; $hex = unpack("H*", $image); close(IMG); while ($txt = substr($hex,0,32,'')) { print "'$txt'\n"; }


      #!/home/bbq/bin/perl
      # Trust no1!

        Yeah, I did copy it yesterday, but I still get this error:

        Too many arguments for substr at ./gif2hex.perl line 17, near "'')" Execution of ./gif2hex.perl aborted due to compilation errors.

        with today's code from your home node:

        #!/usr/bin/perl -w # Description: Hex embedded images sample # Date: June 28, 2000 # Author: bbq@zaz.com.br # URL: http://johnny.warp.psi.br/perl/images.pl?img=text open IMG, "./arrow.gif" or die "Couldn't open image: $!\n"; binmode(IMG); undef $/; $image = <IMG>; $hex = unpack("H*", $image); close IMG; while ($txt = substr($hex,0,32,'')) { print "'$txt'\n"; } # END

        Thanks for you help - hope I'm not missing something obvious.
        This is definately CUFP.
            ybiC