Last active
February 11, 2016 20:29
-
-
Save betacar/5ee2cc92adeab3cace30 to your computer and use it in GitHub Desktop.
Transform images to base64 strings
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
const fs = require('fs'); | |
const regex = /(\.|\/)(gif|jpe?g|png|txt)$/i; | |
const dir = './'; | |
const encoding = 'base64'; | |
fs.readdir(dir, (err, files) => { | |
if (err) return err; | |
files.forEach((file) => { | |
// Skip current file | |
if (file === __filename.slice(__dirname.length + 1)) return; | |
fs.readFile(`${dir}${file}`, {encoding}, (err, str) => { | |
if (err) return err; | |
const ext = regex.exec(file)[2]; | |
console.log('################################################################################'); | |
console.log(file); | |
console.log(`data:image/${ext};base64,${str}`); | |
console.log('################################################################################'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment