Skip to content

Instantly share code, notes, and snippets.

@06wj
Created October 1, 2014 03:33
Show Gist options
  • Save 06wj/da6db536624a821ed3e2 to your computer and use it in GitHub Desktop.
Save 06wj/da6db536624a821ed3e2 to your computer and use it in GitHub Desktop.
image to blob
var util = {
getBlobUrl:function(img){
if(!this.canvas){
this.canvas = document.createElement("canvas");
this.ctx = this.canvas.getContext("2d");
}
this.canvas.width = img.width;
this.canvas.height = img.height;
this.ctx.drawImage(img, 0, 0);
var base64 = this.canvas.toDataURL();
base64 = base64.split(',');
var binary = atob(base64[1]);
var len = binary.length;
var buffer = new ArrayBuffer(len);
var view = new Uint8Array(buffer);
for (var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
var blob = new Blob([view], {
type: base64[0].split(':')[1].split(';')[0]
});
return URL.createObjectURL(blob);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment