Nodejs图片处理模块sharp

sharp类库中,没有文字输出到图片上的功能,找到个text-to-svg库,先生成svg再合并到图片上

let sharp = require('sharp');
let TextToSVG = require('text-to-svg');
let textToSVG = TextToSVG.loadSync('./test.ttf');

let attributes = {fill:'#0E50B2'};
let options = {x: 0, y: 0, fontSize: 40, anchor: 'top', attributes: attributes};
let svg = textToSVG.getSVG('生成图片测试啊',options);
//console.log(svg);

let svgBuffer = Buffer.from( svg);


sharp({
	  create: {
	    width: 800,
	    height: 900,
	    channels: 4,
	    background: { r: 255, g: 0, b: 0, alpha: 0.5 }
	  }
	})
    .composite([{
      input: svgBuffer,
       gravity: 'southeast'
    }])
    .toFile('output2.png')
	.then(info => {
		console.log("成功");
	})
    .catch(err => { 
    	console.log("失败");
    });

你可能感兴趣的:(Node.js)