CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
image encode base64 - help needed


New User

Feb 11, 2016, 6:47 AM

Post #1 of 2 (2592 views)
image encode base64 - help needed Can't Post

Hi everyone
I'm trying to create a perl script which encodes images to base64. This shall be used to transfer images via xml to another system.

I created the following script to read the capture.png file:

use MIME::Base64; 
open (my $image, 'Z:\capture.png') or die "$!";
$raw_string = do{ local $/ = undef; <$image>; };
$encoded = encode_base64( $raw_string );

For the beginning, I just re-write into a new file photo.png

my $decoded= MIME::Base64::decode_base64($encoded); 
open my $fho, '>', 'Z:\photo.png' or die $!;
binmode $fho;
print $fho $decoded;

The photo.png is created with exactly the same size as the input photo, however the newly created file can't be opened.

I also comared the base64 value with the value created by an online encoding webpage (using the same input photo) and this is different.

I would be very happy if somebody could help me out with this!

Thanks a lot in advance.
Best regards


Feb 13, 2016, 10:25 AM

Post #2 of 2 (2575 views)
Re: [zurich] image encode base64 - help needed [In reply to] Can't Post


Could you provide a standalone version of your script that represents the problem. Using the below standalone version, I do not encounter the same issue i.e. input and output images are identical and not corrupted:

use strict; 
use warnings;
use MIME::Base64;

my $input_filepath = 'input.png';
my $output_filepath = 'output.png';

open my $input_filehandle, '<', $input_filepath or die $!;
my $string = do { local $/ = undef; <$input_filehandle> };
close $input_filehandle;

my $string_encoded = encode_base64( $string );
my $string_decoded = decode_base64( $string_encoded );

open my $output_filehandle, '>', $output_filepath or die $!;
binmode $output_filehandle;
print $output_filehandle $string_decoded;
close $output_filehandle;



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives