{"id":1520,"date":"2013-10-30T20:28:48","date_gmt":"2013-10-30T20:28:48","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=1520"},"modified":"2013-10-30T20:28:48","modified_gmt":"2013-10-30T20:28:48","slug":"data-uris-using-generating","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/","title":{"rendered":"Data URI&#8217;s &#8211; Using and Generating"},"content":{"rendered":"<p>A recent project of mine needed an image embedding into some HTML via JavaScript. Rather than use a separate image, I decided to embed it directly using a <a href=\"https:\/\/developer.mozilla.org\/en\/docs\/data_URIs\">data URI<\/a>.<\/p>\n<p>An image in a data URI is the MIME type of the image and it&#8217;s content encoded with base64 into a string. This is great as it cuts down HTTP requests but does cause the initial page weight to increase and be difficult to update as each change means the image needs re-encoding. Modern browsers support data URI&#8217;s very well, but older browsers such as IE 7 and below won&#8217;t like it.<\/p>\n<h2>Examples Using A Data URI Encoded Image<\/h2>\n<h3>HTML Example<\/h3>\n<p>Here&#8217;s how I can embed the image of a red cross into an HTML &lt;img&gt; tag.<\/p>\n<pre class=\"lang:xhtml decode:true \" >&lt;img src=\"data:image\/gif;base64,R0lGODlhFAAUAJEAAP\/9\/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC\/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw==\" alt=\"red cross\" width=\"20\" height=\"20\" \/&gt;\n<\/pre>\n<h3>CSS Example<\/p>\n<h3>\n<p>Here&#8217;s how I can embed the image of a red cross into background of an HTML element using CSS.<\/p>\n<pre class=\"lang:css decode:true \" >body { \n  background: url(data:image\/gif;base64,R0lGODlhFAAUAJEAAP\/9\/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC\/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw==) no-repeat left center;\n}\n<\/pre>\n<h3>JavaScript Example<\/h3>\n<p>Here&#8217;s how I can add an image element with the red cross in to an HTML page using JavaScript.<\/p>\n<pre class=\"lang:js decode:true \" >var imagestring = \"data:image\/gif;base64,R0lGODlhFAAUAJEAAP\/9\/fYQEPytrflWViH5BAAAAAAALAAAAAAUABQAQAJKhI+pGe09lnhBnEETfodatVHNh1BR+ZzH9LAOCYrVYpiAfWWJOxrC\/5MASbyZT4d6AUIBlUYGoR1FsAXUuTN5YhxAEYbrpKRkQwEAOw==\";\nvar image = new Image();\nimage.src = imagestring;\nimage.onload = function() {\n  document.body.appendChild(image);  \n}<\/pre>\n<h2>Encoding An Image To A Data URI<\/h2>\n<p>It&#8217;s easy to create the encoded image string using PHP as it comes with a Base64 encoder as part of the language. Automatically detecting the MIME type of an image is a bit harder, but we can use finfo_file with comes as an extension to PHP 5.3 and above to do this.<\/p>\n<p>So assuming the filename of the image is in the variable $filename we can use the following code to get the mimetype, read the image and encode it to a data URI string.<\/p>\n<pre class=\"lang:php decode:true \" >$finfo = finfo_open(FILEINFO_MIME_TYPE);\n$mimetype = finfo_file($finfo, $filename);\nfinfo_close($finfo);\n\n$contents = file_get_contents($filename);\necho \"data:\" . $mimetype . \";base64,\" . base64_encode($contents);\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>We&#8217;ve seen it&#8217;s easy to embed encoded images into code. I have wrapped the encoding routine into a command line PHP script and placed it on GitHub as <a href=\"https:\/\/github.com\/robertprice\/php-base64-encode\">php-base64-encode<\/a> so it&#8217;s easy to quickly generate data URI&#8217;s.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A recent project of mine needed an image embedding into some HTML via JavaScript. Rather than use a separate image, I decided to embed it directly using a data URI. An image in a data URI is the MIME type of the image and it&#8217;s content encoded with base64 into a string. This is great &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Data URI&#8217;s &#8211; Using and Generating&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[14,23,31,50],"class_list":["post-1520","post","type-post","status-publish","format-standard","hentry","category-dev","tag-css","tag-html","tag-javascript","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Data URI&#039;s - Using and Generating - Robert Price<\/title>\n<meta name=\"description\" content=\"How to generate and use data URI&#039;s. Examples given for using an encoded image in HTML, JavaScript and CSS plus showing how to create your own using PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Data URI&#039;s - Using and Generating - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to generate and use data URI&#039;s. Examples given for using an encoded image in HTML, JavaScript and CSS plus showing how to create your own using PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2013-10-30T20:28:48+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Data URI&#8217;s &#8211; Using and Generating\",\"datePublished\":\"2013-10-30T20:28:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/\"},\"wordCount\":319,\"keywords\":[\"CSS\",\"HTML\",\"JavaScript\",\"PHP\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/\",\"name\":\"Data URI's - Using and Generating - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2013-10-30T20:28:48+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to generate and use data URI's. Examples given for using an encoded image in HTML, JavaScript and CSS plus showing how to create your own using PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/data-uris-using-generating\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data URI&#8217;s &#8211; Using and Generating\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Data URI's - Using and Generating - Robert Price","description":"How to generate and use data URI's. Examples given for using an encoded image in HTML, JavaScript and CSS plus showing how to create your own using PHP.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/","og_locale":"en_GB","og_type":"article","og_title":"Data URI's - Using and Generating - Robert Price","og_description":"How to generate and use data URI's. Examples given for using an encoded image in HTML, JavaScript and CSS plus showing how to create your own using PHP.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/","og_site_name":"Robert Price","article_published_time":"2013-10-30T20:28:48+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Data URI&#8217;s &#8211; Using and Generating","datePublished":"2013-10-30T20:28:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/"},"wordCount":319,"keywords":["CSS","HTML","JavaScript","PHP"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/","name":"Data URI's - Using and Generating - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2013-10-30T20:28:48+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to generate and use data URI's. Examples given for using an encoded image in HTML, JavaScript and CSS plus showing how to create your own using PHP.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/data-uris-using-generating\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Data URI&#8217;s &#8211; Using and Generating"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/1520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=1520"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/1520\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=1520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=1520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=1520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}