Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
JavaScript method to convert an image to data URI
const getDataUri = (img) => {
let dataUri = ''
try {
dataUri = new Buffer(img, 'binary').toString('base64')
dataUri = `data:image/png;base64,${dataUri}`
} catch {
dataUri = '' // Or, replace '' with default placholder image.
return dataUri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment