Perl convert image to base64 -


i using service send picture server , require image converted base64 format before sent. tried mime::base64 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 error message

undefined subroutine &mqin::encode_base64 called @ line 6.

when specify empty import list, this:

use mime::base64 (); 

you no imports.

change line to:

use mime::base64; 

the () parens specifying mime::base64 exports nothing namespace. default behavior (without parens) export encode_base64 , decode_base64. you're overriding convenient default. if don't want functions polluting main namespace retain original use mime::base64 () line, , fully-qualify subroutine call:

$encoded = mime::base64::encode_base64($base64_string); 

but it's whole lot easier, , satisfactory allow default export list processed removing parenthesis use line.

update you're not reading file. line:

$base64_string = image; 

...should updated this:

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

that problem have been caught more verbosely if had use strict 'subs' in effect. problem "image" bareword, , perl thinks it's subroutine call. angle brackets, "<>" common way of reading filehandle. "local $/ = undef" part means of ensuring slurp entire file, not until first sequence looks "\n" perl.

update2: , mob points out, either need escaping backslashes in path, or use forward slashes. perl doesn't mind, on win32. of course since you're taking wise step of using or die $! on open, you've discovered mistake.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -