nodejs imagemagick converting svg to png adds a white background, how to keep it transparent?

心已入冬 提交于 2019-12-10 12:01:37

问题


I'm trying to use imagemagick to convert svgs to pngs, rescale them and put them in a response stream.

I'm using https://www.npmjs.com/package/imagemagick like this:

var imageUri = __dirname + '/images/' + project + '/' + image + '.svg';

console.log(imageUri);

var svg = fs.readFileSync(imageUri, 'utf8');
res.writeHead(200, {'Content-Type': 'image/png' });

var size = '' + (100 * scale);

var conv = im.convert(['svg:-', '-resize', size + 'x' + size, 'png:-']);
conv.on('data', function(data) {
    res.write(data, 'binary');
}); 
conv.on('end', function() {
    res.end();
});                                                                                
conv.stdin.write(svg);
conv.stdin.end();

Only problem is that it adds a background when I was hoping for something transparent. Any ideas of how I can fix this?

来源:https://stackoverflow.com/questions/33435877/nodejs-imagemagick-converting-svg-to-png-adds-a-white-background-how-to-keep-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!