×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Can't seem to read image to base64 encoding?

Can't seem to read image to base64 encoding?

Can't seem to read image to base64 encoding?

(OP)
hi,

I have this...

CODE

use MIME::Base64 qw( encode_base64 );

# read image file to base64 and set for result
open (my $img, '/images/banners/' . $page . '_' . $banner[0]{'RecID'} . '.' . $banner[0]{'Ext'}) or die $!;
my $raw = do { local $/ = undef; $img; };
$result->{data}->{img} = encode_base64( $raw ); 

But the base64 encoding for the image data that is created is only

Quote:

R0xPQigweDU1NWFmZGMp

Which cannot be the entire image encoded and when used via

CODE

<img src="data:image/jpeg;base64,R0xPQigweDU1NWFmZGMp"> 
I just get an image place holder not the image, so the source must be invalid.

What am I missing?

Thanks,
1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Can't seem to read image to base64 encoding?

Hi

Given that "R0xPQigweDU1NWFmZGMp" means "GLOB(0x555afdc)", is evident that $raw is not really raw data. You forgot to actually read :

CODE --> Perl

my $raw = do { local $/ = undef; <$img>; }; 

Feherke.
feherke.ga

RE: Can't seem to read image to base64 encoding?

(OP)
well it seemed my slurp wasn't working so I changed it to a while loop and it worked fine.

CODE

my $raw;
open (my $img, '/images/banners/' . $page . '_' . $banner[0]{'RecID'} . '.' . $banner[0]{'Ext'}) or die $!;
binmode $img;
while (<$img>){$raw .= $_;} 

I assumed the slurp syntax was wrong so I looked up slurp and found this works

CODE

local $/=undef; # this is what makes it slurpy by removing EOL char setting!
open (my $img, '/images/banners/' . $page . '_' . $banner[0]{'RecID'} . '.' . $banner[0]{'Ext'}) or die $!;
binmode $img;
my $raw = <$img>;
my $encoded = encode_base64( $raw ); 

Teach me for cargo-culting from SO junk!

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Can't seem to read image to base64 encoding?

Hi

Interesting. Your initial slurping code worked for me. ( Of course, after adding the < .. > ) Tested with a couple of PNG files in command line.

Feherke.
feherke.ga

RE: Can't seem to read image to base64 encoding?

(OP)
that'll learn me, missed your first post. the original SO thread was using nasty bareword file handles, when I refactored to use a proper var, I forgot the angles!

I actually condensed it to

CODE

local $/=undef; # this is what makes it slurpy by removing EOL char setting!
open (my $img, '/images/banners/' . $page . '_' . $banner[0]{'RecID'} . '.' . $banner[0]{'Ext'}) or die $!;
binmode $img;
my $encoded = encode_base64( <$img> ); 
close $img; 
Anything you can see wrong with this approach?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Can't seem to read image to base64 encoding?

Hi

Well, the original code used local inside a dedicated block which restricted its effect only to a single reading operation. That way the effect of reseting $/ was automatically lost when execution left that block.

Now you reset $/ somewhere in a larger code block. So be aware that it affects all the code that follows your quoted fragment, until the end of that block, probably an entire sub.

Feherke.
feherke.ga

RE: Can't seem to read image to base64 encoding?

(OP)
yes, I get that local $/ is localising to the lexical scope of the block, there are no other file reads in the block.

I assume the fact that the code is in not just a 'try/catch' block but also an 'else' clause, it is localised to the else clause?

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Can't seem to read image to base64 encoding?

Hi

Yes, else's block is also a block, so it will limit local's effect.

Feherke.
feherke.ga

RE: Can't seem to read image to base64 encoding?

(OP)
And the irony in all this is.....

1. Email clients especially Outlook don't allow images embedded with the Data URI Scheme.
2. MIME::Lite doesn't accept pre-encoded data.

I ended up having to just use the raw binary data and pass that to MIME::Lite as

CODE

$msg->attach( 
                    Encoding => 'base64',
                    Type     => 'image/' . $img->{ext},
                    Data     => $img->{img}, # this is raw binary of the image file (not base64 encoded)
                    Id       => $img->{id},
                    Disposition => 'inline'
                    ); 
Then I could use the CID in the HTML to reference the MIME encoded inline images. smile

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Can't seem to read image to base64 encoding?

Hi

Quote (1DMF)

1. Email clients especially Outlook don't allow images embedded with the Data URI Scheme.
Is there difference between allow and support in this case ? The Data URI scheme | Email Client support Wikipedia article mentions "Microsoft Outlook 2003". ( I am happily unable to test this. )

Feherke.
feherke.ga

RE: Can't seem to read image to base64 encoding?

(OP)
Yes, according to : https://www.campaignmonitor.com/blog/post/3927/emb...

Outlook 2003 worked, but 2007+ doesn't , our company runs 2010/2013 with perhaps one user on 2007.

No one is running 2003!

If we were only using 2003 it would be fine, as this email is for internal use only, but it certainly wouldn't be of any use for generic emails due to the poor support, which apparently is due to spammers trying to get round latest image blocking security in emails. Something I wasn't trying to circumvent!

I guess I should have done a little homework before trying to apply the new HTML5 stuff via FileReader and Data URI to my HTML emails.

In terms of the application I'm building, it's neither here nor there how I pass the image data to the email model. What I couldn't do is pass a 'path' or 'filehandle' as the image file is being deleted, but I still wanted to include the embedded image of the banner that was deleted in the 'banner advert removed' email.

Yes, I could send the email with a standard attachment then perform the delete, but I didn't like that, it doesn't feel right sending an email saying something has been performed before the code to actually perform it has ran and is checked for success. So I run the method to remove the record from the DB, as well as delete the physical file on the HD, but I first read the file, so I have it to pass to the email model for inline embedding.

That way I'm sending the email only when the delete process is successful but still able to physically show the image of the advert to make it easy for the recipient to instantly see what was removed, rather than just having a textual description.

I wasn't sure MIME::Lite would accept the raw image via the 'Data' attribute rather than 'Path', but it does smile

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music

RE: Can't seem to read image to base64 encoding?

Hi

Doh, my mistake. I saw there is mentioned "Mozilla Thunderbird 3", then I tested data URI in Thunderbird 31, and as it worked I considered that "3" actually means "since 3". So I supposed the Outlook support also means "since 2003".

Feherke.
feherke.ga

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close