Created
June 19, 2021 20:49
-
-
Save ShapeGroup/abc785b1c47c83c17c76182f71f6f65c to your computer and use it in GitHub Desktop.
js-kit-image-url-to-base-sixty-four
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
//// JS KIT - read a in b64 an image url | |
//// MIT LICENCE | |
//// Credit: alberto marà | |
// how to use: | |
// imageurltob64('jpg', paths.uploads + "staff/" + gud.picture, img64data => { | |
// yourimage.src = img64data; | |
// }); | |
const imageurltob64 = (ext,url,back) => { | |
let img = new Image(); | |
img.setAttribute('crossOrigin', 'anonymous'); | |
img.onload = function() { | |
let canvas, ctx, dataURL; | |
canvas = document.createElement("canvas"); | |
canvas.width = this.width; | |
canvas.height = this.height; | |
ctx = canvas.getContext("2d"); | |
ctx.drawImage(this, 0, 0); | |
dataURL = canvas.toDataURL(string('image/'+ext)); | |
return back(dataURL); | |
}; | |
img.src = url; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment