Perl convert image to base64

I use the service to send the image to the server, and they require that the image be converted to base64 format before it is sent. I tried MIME :: Base64 with this code:

use MIME::Base64 (); open (IMAGE, "C:\\wamp\\www\\image.png") or die "$!"; $base64_string = IMAGE; $encoded = encode_base64($base64_string); print "Encode $encoded"; 

and got this error message

Undefined subroutine &mqin::encode_base64 called at line 6.

+6
source share
2 answers

When you specify an empty import list, for example:

 use MIME::Base64 (); 

Import is not possible.

Change this line to:

 use MIME::Base64; 

The () parameter indicates that MIME :: Base64 does not export anything to your namespace. The default behavior (without partners) is to export encode_base64 and decode_base64 . You override a convenient default value. If you really do not want these functions to pollute the main namespace, you could save the original line use MIME::Base64 () , and then fully qualify your subroutine call:

 $encoded = MIME::Base64::encode_base64($base64_string); 

But it is much simpler and probably satisfactory to simply allow default export list processing by removing the brackets from the use string.

Refresh . You also do not read the file. This line:

 $base64_string = IMAGE; 

... should be updated as follows:

 $raw_string = do{ local $/ = undef; <IMAGE>; }; $encoded = encode_base64( $raw_string ); 

This problem would be solved in more detail if you had use strict 'subs' . The problem is that " IMAGE " itself is just a gobe, and Perl considers it a subroutine call. Angle brackets " <> " are the usual way to read from a file descriptor. The " local $/ = undef " part is just a means of ensuring that you process the entire file, not just the first sequence, which looks like "\ n" in Perl.

Update2: And, as MOB points out, you need to either avoid backslashes in your path, or use slashes. Perl doesn't mind, even on Win32. Of course, since you are taking the wise step of using or die $! on your open , you already discovered this error.

+10
source

Base64 Encoder Short Program:

 # to_base64.pl use MIME::Base64 qw(encode_base64); open (IMAGE, $ARGV[0]) || die "$!"; binmode(IMAGE); local $/; my $file_contents = <IMAGE>; close IMAGE; open (B64, ">$ARGV[0].b64") || die $!; print B64 encode_base64($file_contents); close B64; print "output file is $ARGV[0].b64\n"; 

use it with this command line:

 perl to_base64.pl image_file.jpg 

It writes a file named image_file.jpg.b64 containing an input file encoded with Base64.

To decode Base64, you can use this script:

 # decode_base64.pl use MIME::Base64 qw(decode_base64); open (B64, $ARGV[0]) || die "$!"; local $/; my $base64_string = <B64>; close B64; my $filename; if ($ARGV[0] =~ /.\.b64$/i) { $filename = substr($ARGV[0], 0, length($ARGV[0]) - 4); } else { $filename = "$ARGV[0].bin"; } open (IMAGE, ">$filename") || die $!; binmode(IMAGE); print IMAGE decode_base64($base64_string); close IMAGE; print "output file is $filename\n"; 

Call it using this command line:

 perl decode_base64.pl my_base64_file.b64 

If the file name specified as a parameter for this script ends in .b64 , these remaining 4 characters will be deleted: image_file.jpg.b64 => image_file.jpg . Otherwise, the script will add .bin to the name of the input file to get the name of the output file.

0
source

Source: https://habr.com/ru/post/919535/


All Articles