Created
June 23, 2019 23:06
-
-
Save ogabrielguerra/95def799ccd44399daff21c453438fe6 to your computer and use it in GitHub Desktop.
Javascript function to convert images to base64
This file contains 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
function getBase64Image(img) { | |
var canvas = document.createElement("canvas"); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
var ctx = canvas.getContext("2d"); | |
ctx.drawImage(img, 0, 0); | |
var dataURL = canvas.toDataURL("image/png"); | |
return dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); | |
} | |
var base64 = getBase64Image(document.getElementById("imageid")); | |
/* From Stackoverflow at: https://stackoverflow.com/questions/22172604/convert-image-url-to-base64 */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment