Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Binary Challenge

by BeernuT (Pilgrim)
on Mar 19, 2002 at 05:12 UTC ( [id://152652]=note: print w/replies, xml ) Need Help??


in reply to Binary Challenge

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

Replies are listed 'Best First'.
Re: Re: Binary Challenge
by tachyon (Chancellor) on Mar 19, 2002 at 10:30 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://152652]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-03-29 01:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found