Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Proper way to read an image

Status
Not open for further replies.

Kirsle

Programmer
Jan 21, 2006
1,179
US
What's the proper way to read an image from the hard drive, for example to base64 encode it?

I usually do something along the lines of...

Code:
use MIME::Base64 qw(encode_base64);

open (READ, "something.gif");
binmode READ;
my @data = <READ>;
close (READ);
chomp @data;

my $bin = join("",@data);
my $base64 = encode_base64 ($bin);

# then i.e. feed it to Tk::Photo
$mw->Photo (-data => $base64, -format => 'GIF');

Sometimes I join("\n") or join("")... I usually try one or the other and test it out and if the image is corrupted when rewriting it to a file after then I know it was the opposite one.

Is there a better way of reading an image? Do I need to chomp it or worry about newlines, or does the binary data all come as one line to where I could just do

Code:
open (DATA, "something.gif");
binmode DATA;
$bin = <DATA>;
close (DATA);
$base64 = encode_base64($bin);

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top