Created
March 17, 2015 21:41
-
-
Save qbein/f6d24b36c6c725c645ab to your computer and use it in GitHub Desktop.
Embeds an image in svg
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
var fs = require('fs'), | |
sizeOf = require('image-size'); | |
function usage() { | |
console.log('usage: node img-to-svg.js {input}'); | |
process.exit(1); | |
} | |
function template(data) { | |
return '<svg xmlns="http://www.w3.org/2000/svg" \n' + | |
' xmlns:xlink="http://www.w3.org/1999/xlink" \n' + | |
' width="' + data.width + 'px" height="' + data.height + 'px">\n' + | |
' <g>\n' + | |
' <image x="0" y="0" width="' + data.width + 'px" height="' + data.height + 'px" xlink:href="data:image/' + ext + ';base64,' + data.base64 + '" />\n' + | |
' </g>\n' + | |
'</svg>'; | |
} | |
if(process.argv.length != 3) { | |
usage(); | |
} | |
var src = process.argv[2], | |
dest = src.replace(/\.[^\.]+$/, '.svg'), | |
ext = src.match(/\.([^\.]+)$/)[1], | |
dim = sizeOf(src); | |
fs.writeFileSync(dest, template({ | |
width: dim.width, | |
height: dim.height, | |
base64: fs.readFileSync(src).toString('base64'), | |
ext: ext | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment