Skip to content

Instantly share code, notes, and snippets.

@semagarcia
Last active November 9, 2015 09:23
Show Gist options
  • Save semagarcia/f9ecdaf0f76a2fa3ef9d to your computer and use it in GitHub Desktop.
Save semagarcia/f9ecdaf0f76a2fa3ef9d to your computer and use it in GitHub Desktop.
How to convert an image into base64 string
<img id="image" src="http://www.gravatar.com/avatar/90348ht093409g7wetdgf?s=128&d=identicon&r=PG">
<canvas id="canvasElement"/>
<div id="base64string" />
var canvasEl = document.getElementById("canvasElement");
var ctx = canvasEl.getContext("2d");
var img = document.getElementById("image");
ctx.drawImage(img, 15, 15);
alert(canvasEl.toDataURL());
document.getElementById('base64string').innerHTML = img;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment