Skip to content

Instantly share code, notes, and snippets.

@IskenHuang
Last active December 22, 2015 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IskenHuang/6434813 to your computer and use it in GitHub Desktop.
Save IskenHuang/6434813 to your computer and use it in GitHub Desktop.
/**
* This is simple transfer image to data uri(base64 encode) nodejs command line tool
*
* require
* install nodejs v0.8+
*
* how to use
* node img YOUR_IMAGE_FILE_PATH
*
* sample
* node img logo.png
*
*/
var param = process.argv[2],
sp = param.split(/\./ig),
fs = require('fs');
console.log('File name: ', param, sp[sp.length-1]);
console.log('============================== BEGIN =================================');
fs.readFile( param , function(err, data) {
var base64data = new Buffer(data).toString('base64'),
imgString = 'data:image/' + sp[sp.length-1] + ';base64,' + base64data;
// copy to clipboard
pbcopy(imgString);
console.log(imgString);
console.log('============================== END =================================');
});
/**
* copy string to clipboard
* mac only
*
* @param {String} data put string to clipboard
* @return {String} return original param
*/
function pbcopy(data) {
var proc = require('child_process').spawn('pbcopy');
proc.stdin.write(data);
proc.stdin.end();
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment