Skip to content

Instantly share code, notes, and snippets.

@gfcarvalho
Created March 12, 2014 07:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save gfcarvalho/9502621 to your computer and use it in GitHub Desktop.
Save gfcarvalho/9502621 to your computer and use it in GitHub Desktop.
Converting a local image to base64 using JavaScript (canvas + regexp)
function imageToBase64(img)
{
var canvas, ctx, dataURL, base64;
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL("image/png");
base64 = dataURL.replace(/^data:image\/png;base64,/, "");
return base64;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment