Skip to content

Instantly share code, notes, and snippets.

@oliyh
Created November 7, 2015 22:17
Show Gist options
  • Select an option

  • Save oliyh/db3d1a582aefe6d8fee9 to your computer and use it in GitHub Desktop.

Select an option

Save oliyh/db3d1a582aefe6d8fee9 to your computer and use it in GitHub Desktop.

Revisions

  1. oliyh created this gist Nov 7, 2015.
    11 changes: 11 additions & 0 deletions image-url-to-data-uri.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    // hard won knowledge from http://stackoverflow.com/questions/20035615/using-raw-image-data-from-ajax-request-for-data-uri
    var xmlHTTP = xhr.XMLHttpRequest();
    xmlHTTP.open('GET', url, true);
    xmlHTTP.responseType = 'arraybuffer';
    xmlHTTP.onload = function(e) {
    var arr = new Uint8Array(this.response);
    var raw = String.fromCharCode.apply(null,arr);
    var b64 = base64.encode(raw);
    var dataURL="data:image/png;base64," + b64;
    };
    xmlHTTP.send();