Created
October 1, 2014 03:33
-
-
Save 06wj/da6db536624a821ed3e2 to your computer and use it in GitHub Desktop.
image to blob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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