convert an image into Base64 string using JavaScript
function getBase64Image(src, callback, outputFormat) {
const img = new Image();
img.crossOrigin = 'Anonymous';
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
let dataURL;
canvas.height = img.naturalHeight;
canvas.width = img.naturalWidth;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
};
img.src = src;
if (img.complete || img.complete === undefined) {
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
img.src = src;
}
}
Also in JavaScript:
- checking object is empty in JS
- how to run a vue js hello world app in vue version 3
- JsonConvert.DeserializeObject convert into dynamic datatable
- js only first 50 letters
- how to check if map is map javascript
- react native loading
- how to use redirect in react
- angular transition animation
- node_modules/react-native-paper/lib/module/core/Provider.js
- javascript detect touch
- nodejs btoa
- how to load localt ext file in js
- express payloadtoolarge request entity too large bodyParser raw-body hydra-express
- how to display current date and time in angular
- jquery enable textbox
- how to print NODE_PATH
- javascript json to array
- add discord.js library
- button copy javascript
- javascript element by class
- download comma separated file, react
- how to check if user has installed pwa
- ioredis cluster example
- remove last element from array javascript