Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Binary Challenge

by Buzz (Novice)
on Mar 19, 2002 at 04:44 UTC ( #152649=perlquestion: print w/ replies, xml ) Need Help??
Buzz has asked for the wisdom of the Perl Monks concerning the following question:

Perl Monks, i seek your wisdom. I have been baffled for the past few days with what i feel is an intriguing problem. You see i would like to put a binary gif file inside a perl script.

One idea included writing a string, the binary data, to a Temp file and accessing it from there. but it was not sufficient for my requirements.

I also wish for the script to be self contained, that is, no dependencies or libraries kind of a guaranteed portability.

I would be happy to hear any ideas that you are willing to offer

Loyal Servant
Buzz.

Comment on Binary Challenge
Re: Binary Challenge
by BeernuT (Pilgrim) on Mar 19, 2002 at 05:12 UTC
    2 scripts( really one )
    First one is used to create the img you wish to be included into your script.
    #!/usr/bin/perl use warnings; use strict; use MIME::Base64; my $image; open(IMAGE, 'img.jpg') or die $!; while(<IMAGE>){ $image .= $_; } close(IMAGE); my $encoded = encode_base64($image); print $encoded;
    2nd is the code that reads the image and prints it from the script.
    #!/usr/bin/perl use warnings; use strict; use MIME::Base64; my $image; while (<DATA>){ $image .= $_; } print "Content-type: image/jpeg\n\n"; print decode_base64($img); __END__ /9j/4AAQSkZJRgABAgEASABIAAD/7QtyUGhvdG9zaG9wIDMuMAA4QklNA+kA SAAAAAADGAJB//f/9wNAAkogAgV7A+AAAAAAAWgBaAAAAAAPeAtFAWwAMgtF Jw8AAQABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQBkAAAAAAAAAAACABCxBQAR QklNA+0AAAAAABAASAAAAAEAAQBIAAAAAQABOEJJTQPzAAAAAAAIAAAAAAAA AAEAADhCSU0nEAAAAAAACgABAAAAAAAAAAI4QklNA/UAAAAAAEgAL2ZmAAEA L2ZmAAEAoZmaAAYAAAAAAAEAMgAAAAEAWgAAAAYAAAAAAAEANQAAAAEALQAA A/gAAAAAAHAAAP////////////////////////////8D6AAAAAD///////// ////A+gAAAAA/////////////////////////////wPoAAAAAP////////// //8D6AAAOEJJTQQIAAAAAAAQAAAAAQAAAkAAAAJAAAAAADhCSU0ECQAAAAAJ OQAAAYAAAFWAAAAJYQAYAAH/2P/gABBKRklGAAECAQBIAEgAAP/+ACdGaWxl QWRvYmUgUGhvdG9zaG9wqCA0LjAA/+4ADkFkb2JlAGSAAAAAAf/bAIQADAgI FQ8MDA8VGBMTFRMTGBEMDAwMDAwRDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM Dg4QFA4ODhQUDg4ODhQRDAwMDAwREQwMDAwMDBEMDAwMDAwMDAwMDAwMDAwM /8AAEQgAOQCAAwEiAAIRAQMRAf/dAAQACP/EAT8AAAEFAQEBAQEBAAAAAAAA CwEAAQUBAQEBAQEAAAAAAAAAAQACAwQFBgcICQoLEAABBAEDAgQCBQcGCAUD

    The gibberish is the encoded output you got from the first script. ( its not a pic so dont expect it to output anything of use to you ). This was posted on a newsgroup a few months back, can't remember who posted it but i've used it from time to time.

    Documentation:
    MIME::Base64

    Have any problems let me know.

    -bn

      When you write while(<IMAGE>)... you will read in a chunk of image until you get to the binary for a newline char 00001010. Thus you will read in random length chunks of data. You should really use read(). You should also consider binmode which is essential on Win32 and harmless on *nix.

      open IMG, $image or die $!; binmode IMG; binmode STDOUT; my $buffer; while (read(IMG, $buffer, 1024)) { print $buffer; # etc }

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Binary Challenge
by zengargoyle (Deacon) on Mar 19, 2002 at 05:12 UTC
Re: Binary Challenge
by grep (Monsignor) on Mar 19, 2002 at 05:14 UTC

    Why exactly do you need the script self contained? There was just a discussion today about arbitrary requirements.

    Zip files, Self-Extracting Zip files and install programs have been standards for years why would you want perl to do something (one of the few things) that it is not particularly good at (e.g. storing binary files), when you have many methods that are good at it.

    If you feel you must do this, I would personally use the __DATA__ filehandle. I've never used it before to store images, but it might work

    UPDATE:Apparently I was a little late to the punch with __DATA__ suggestion... but I also found another node about using Image __DATA__

    grep
    grep> cd /pub
    grep> more beer

      I was curious to see if it were possible to "insert" a graphic image, gif or jpg, into a web based perl script... Kind of a "can it be done" learning experience.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://152649]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-08-23 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls