Skip to content

Instantly share code, notes, and snippets.

@letanure
Last active December 20, 2015 11:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save letanure/6125171 to your computer and use it in GitHub Desktop.
Save letanure/6125171 to your computer and use it in GitHub Desktop.
image to base64
function imageToBase64(URL) {
var imagem = new Image();
imagem.src = URL;
imagem.onload = function () {
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
var cvx = canvas.getContext("2d");
cvx.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL("image/png");
console.log( dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
}
}
//ex
imageToBase64('http://jsfiddle.net/img/logo.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment