File Exchange Pick of the Week

Our best user submissions

Encode images as Base64

Posted by Jiro Doke,

Jiro‘s pick this week is Base64 Image Encoder by Michael Katz.

Some of you may remember Mike from when he was one of the bloggers for the MATLAB Community Blog, formerly known as the MATLAB Desktop blog.

Starting from R2016a, you can create live MATLAB scripts, which are executable MATLAB documents that can have formatting and embedded images.

openExample('matlab_featured/Creating2DPlots2Example')

You were always able to publish your MATLAB scripts, but with live scripts you’re editing and executing the final document.

With publishing, I usually prefer to use HTML for sharing, because it doesn’t create pages, which sometimes cause undesireable page breaks in the document. However, one pet peeve I had was that it would create separate image files for the graphics.

If I want to share this, I need to zip the files and have the other person unzip to view the document. I much prefer a single document.

Live scripts, publishing, HTML, single document… How do these all tie together? Well, live scripts are saved with a .mlx extension, and you can share them with other MATLAB users, but you can also choose to output them as static documents, such as HTML or PDF.

When you choose HTML, it creates a single HTML file!

This is when I first learned that you could embed images directly into your HTML file. You represent your image file (binary format) as ASCII text using Base64 encoding. Then, in your HTML file, you embed the image like this.

<img src="data:image/png;base64,...

Sweet!!

I just needed to convert my images to Base64 encoding, and I simply searched the web for “matlab base64 image”. Thanks to Mike’s code, I was able to create a simple function for embedding images into a single HMTL file. Thanks, Mike!

Comments

Give it a try and let us know what you think here or leave a comment for Mike.

Get the MATLAB code

Published with MATLAB® R2016b

24 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.