convertImgToBase64URL('http://bit.ly/18g0VNp', function(base64Img){
// Base64DataURL
});
Last active
August 26, 2021 07:36
-
-
Save cawa87/1c477d44fcfdf67aea3f to your computer and use it in GitHub Desktop.
JavaScript Convert an image to a base64 url
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
/** | |
* Convert an image | |
* to a base64 url | |
* @param {String} url | |
* @param {Function} callback | |
* @param {String} [outputFormat=image/png] | |
*/ | |
function convertImgToBase64URL(url, callback, outputFormat){ | |
var img = new Image(); | |
img.crossOrigin = 'Anonymous'; | |
img.onload = function(){ | |
var canvas = document.createElement('CANVAS'), | |
ctx = canvas.getContext('2d'), dataURL; | |
canvas.height = img.height; | |
canvas.width = img.width; | |
ctx.drawImage(img, 0, 0); | |
dataURL = canvas.toDataURL(outputFormat); | |
callback(dataURL); | |
canvas = null; | |
}; | |
img.src = url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito bom! Era justamente o que eu precisava! Obrigado!