Created
April 26, 2021 09:00
-
-
Save RamadhanAmizudin/f778d410272a63d270e441ade5619435 to your computer and use it in GitHub Desktop.
[Javascript] Get Base64 Image from URL
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
function getBase64FromImageUrl(url) { | |
var img = new Image(); | |
img.crossOrigin = "anonymous"; | |
img.onload = function() { | |
var canvas = document.createElement("canvas"); | |
canvas.width = this.width; | |
canvas.height = this.height; | |
var ctx = canvas.getContext("2d"); | |
ctx.drawImage(this, 0, 0); | |
var dataURL = canvas.toDataURL("image/png"); | |
return dataURL.replace(/^data:image\/(png|jpg);base64,/, ""); | |
}; | |
img.src = url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment