{"id":8205,"date":"2016-12-23T09:00:16","date_gmt":"2016-12-23T14:00:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8205"},"modified":"2016-12-22T02:08:51","modified_gmt":"2016-12-22T07:08:51","slug":"encode-images-as-base64","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/12\/23\/encode-images-as-base64\/","title":{"rendered":"Encode images as Base64"},"content":{"rendered":"<div class=\"content\">\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24514-base64-image-encoder\">Base64 Image Encoder<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1297696\">Michael Katz<\/a>.<\/p>\n<p>Some of you may remember Mike from when he was one of the bloggers for the <a href=\"https:\/\/blogs.mathworks.com\/community\/\">MATLAB Community Blog<\/a>, formerly known as the MATLAB Desktop blog.<\/p>\n<p>Starting from R2016a, you can create <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/live-scripts.html (link no longer works)\">live MATLAB scripts<\/a>, which are executable MATLAB documents that can have formatting and embedded images.<\/p>\n<pre class=\"language-matlab\">openExample(<span class=\"string\">'matlab_featured\/Creating2DPlots2Example'<\/span>)\r\n<\/pre>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_base64imageencoder\/potw_base64encode_livescript.png\" alt=\"\"> <\/p>\n<p>You were always able to <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/matlab_prog\/publishing-matlab-code.html (link no longer works)\">publish<\/a> your MATLAB scripts, but with live scripts you&#8217;re editing and executing the final document.<\/p>\n<p>With publishing, I usually prefer to use HTML for sharing, because it doesn&#8217;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.<\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_base64imageencoder\/potw_base64encode_publish.png\" alt=\"\"> <\/p>\n<p>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.<\/p>\n<p>Live scripts, publishing, HTML, single document&#8230; How do these all tie together? Well, live scripts are saved with a <tt>.mlx<\/tt> 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.<\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_base64imageencoder\/potw_base64encode_saveas.png\" alt=\"\"> <\/p>\n<p>When you choose HTML, it creates a single HTML file!<\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_base64imageencoder\/potw_base64encode_singleHTML.png\" alt=\"\"> <\/p>\n<p>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 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Base64\">Base64<\/a> encoding. Then, in your HTML file, you embed the image like this.<\/p>\n<pre>&lt;img src=\"data:image\/png;base64,...<\/pre>\n<p>Sweet!!<\/p>\n<p>I just needed to convert my images to Base64 encoding, and I simply searched the web for &#8220;matlab base64 image&#8221;. Thanks to Mike&#8217;s code, I was able to create a simple function for embedding images into a single HMTL file. Thanks, Mike!<\/p>\n<p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_base64imageencoder\/potw_base64encode_embedImage.png\" alt=\"\"> <\/p>\n<p><b>Comments<\/b><\/p>\n<p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8205#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24514-base64-image-encoder#comments\">comment<\/a> for Mike.<\/p>\n<p><script language=\"JavaScript\"> <!-- \n    function grabCode_38ea53c8426d4f319d00b031213bb7eb() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='38ea53c8426d4f319d00b031213bb7eb ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 38ea53c8426d4f319d00b031213bb7eb';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_38ea53c8426d4f319d00b031213bb7eb()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2016b<\/p>\n<p class=\"footer\">\n      Published with MATLAB&reg; R2016b<\/p>\n<\/div>\n<p><!--\n38ea53c8426d4f319d00b031213bb7eb ##### SOURCE BEGIN #####\n%%\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\n% pick this week is\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24514-base64-image-encoder Base64 Image\n% Encoder> by\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1297696 Michael\n% Katz>.\n%\n% Some of you may remember Mike from when he was one of the bloggers for\n% the <https:\/\/blogs.mathworks.com\/community\/ MATLAB Community Blog>,\n% formerly known as the MATLAB Desktop blog.\n%\n% Starting from R2016a, you can create\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/live-scripts.html\n% live MATLAB scripts>, which are executable MATLAB documents that can have\n% formatting and embedded images.\n%\n%   openExample('matlab_featured\/Creating2DPlots2Example')\n%\n% <<potw_base64encode_livescript.png>>\n%\n% You were always able to\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016b\/matlab\/matlab_prog\/publishing-matlab-code.html\n% publish> your MATLAB scripts, but with live scripts you're editing and\n% executing the final document.\n%\n% With publishing, I usually prefer to use HTML for sharing, because it\n% doesn't create pages, which sometimes cause undesireable page breaks in\n% the document. However, one pet peeve I had was that it would create\n% separate image files for the graphics.\n%\n% <<potw_base64encode_publish.png>>\n%\n% If I want to share this, I need to zip the files and have the other\n% person unzip to view the document. I much prefer a single document.\n%\n% Live scripts, publishing, HTML, single document... How do these all tie\n% together? Well, live scripts are saved with a |.mlx| extension, and you\n% can share them with other MATLAB users, but you can also choose to output\n% them as static documents, such as HTML or PDF.\n%\n% <<potw_base64encode_saveas.png>>\n%\n% When you choose HTML, it creates a single HTML file!\n%\n% <<potw_base64encode_singleHTML.png>>\n%\n% This is when I first learned that you could embed images directly into\n% your HTML file. You represent your image file (binary format) as ASCII\n% text using <https:\/\/en.wikipedia.org\/wiki\/Base64 Base64> encoding. Then,\n% in your HTML file, you embed the image like this.\n%\n%  <img decoding=\"async\" src=\"data:image\/png;base64,...\n%\n% Sweet!!\n%\n% I just needed to convert my images to Base64 encoding, and I simply\n% searched the web for \"matlab base64 image\". Thanks to Mike's code, I was\n% able to create a simple function for embedding images into a single HMTL\n% file. Thanks, Mike!\n%\n% <<potw_base64encode_embedImage.png>>\n%\n% *Comments*\n%\n% Give it a try and let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8205#respond here> or leave a\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24514-base64-image-encoder#comments comment> for Mike.\n\n##### SOURCE END ##### 38ea53c8426d4f319d00b031213bb7eb\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_base64imageencoder\/potw_base64encode_livescript.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\nJiro&#8216;s pick this week is Base64 Image Encoder by Michael Katz.<br \/>\nSome of you may remember Mike from when he was one of the bloggers for the MATLAB Community Blog, formerly known as the MATLAB&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/12\/23\/encode-images-as-base64\/\">read more >><\/a><\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8205"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8205"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8205\/revisions"}],"predecessor-version":[{"id":8216,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8205\/revisions\/8216"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}