Created
June 13, 2013 13:31
-
-
Save branneman/5773699 to your computer and use it in GitHub Desktop.
Node.js — Serve generated PNG from SVG, specifying dimensions in the url/filename. This node.js express server generates 'checkmark.svg.107x94.png' from 'checkmark.svg' with Inkscape CLI
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
var fs = require('fs'), | |
exec = require('child_process').exec, | |
express = require('express'), | |
app = express(); | |
app.get('/static/img/*.svg.*.png', function(req, res) { | |
var pngFile = 'src' + req.url, | |
svgFile = pngFile.substring(0, pngFile.indexOf('.svg') + 4); | |
if (!fs.existsSync(svgFile)) { | |
return res.render('404', { | |
baseUrl: (new Array(req.url.split('/').length)).join('../') | |
}); | |
} | |
if (fs.existsSync(pngFile)) { | |
return res.sendfile(pngFile); | |
} | |
var size = pngFile.split('.')[pngFile.split('.').length - 2].split('x'), | |
cmd = '"C:\\Program Files (x86)\\Inkscape\\inkscape.com" -z -e "' + pngFile + '" -w ' + size[0] + ' -h ' + size[1] + ' "' + svgFile + '"'; | |
exec(cmd, function(err, stdout) { | |
if (err) throw err; | |
res.sendfile(pngFile); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment