Last active
December 22, 2015 06:59
-
-
Save IskenHuang/6434813 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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