1function getBase64Image(src, callback, outputFormat) {
2 const img = new Image();
3 img.crossOrigin = 'Anonymous';
4 img.onload = () => {
5 const canvas = document.createElement('canvas');
6 const ctx = canvas.getContext('2d');
7 let dataURL;
8 canvas.height = img.naturalHeight;
9 canvas.width = img.naturalWidth;
10 ctx.drawImage(img, 0, 0);
11 dataURL = canvas.toDataURL(outputFormat);
12 callback(dataURL);
13 };
14
15 img.src = src;
16 if (img.complete || img.complete === undefined) {
17 img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
18 img.src = src;
19 }
20 }
1function encodeImageFileAsURL(element) {
2 var file = element.files[0];
3 var reader = new FileReader();
4 reader.onloadend = function() {
5 console.log('RESULT', reader.result)
6 }
7 reader.readAsDataURL(file);
8}
9<input type="file" onchange="encodeImageFileAsURL(this)" />