用节点文本生成的示例代码

用节点文本生成的示例代码
今天,老板已经要求,在服务器上创建一个邀请卡,嗯……最简单的就是拿这个:

结果更糟糕。

Ruby的后端制作了一幅HTML图片,并说它太慢了。我下一个坑。
所以在上床睡觉之前,先做一个简单的实现。

解决

文本转换为SVG以合并图片

相关的车轮

像Node.js轻量级跨平台的图像编解码库,无需额外安装的依赖
文本到SVG文本到SVG
svg2png SVG的png图片
示例代码
严格使用;

const FS =需要('fs);
const图像=需要('images);
const texttosvg =需要('text-to-svg);
const svg2png =需要(svg2png );
const的承诺=需要('bluebird);

promise.promisifyall(FS);

const texttosvg = texttosvg.loadsync('fonts /文泉驿微米黑。TTF);

const sourceimg =图像('。/我/ webwxgetmsgimg .webp);
const swidth = sourceimg.width();
const sheight = sourceimg.height();

const svg1 = texttosvg.getsvg('wei长清人人都讲应用,{
X:0,
y:0,
FontSize:24,
主持人:最重要的,
});

const svg2 = texttosvg.getsvg('invite你参加的,{
X:0,
y:0,
FontSize:16,
主持人:最重要的,
});

const svg3 = texttosvg.getsvg('everyone告诉课程,
X:0,
y:0,
FontSize: 32,
主持人:最重要的,
});

promise.coroutine(功能* generateinvitationcard(){)
const targetimg1path = '。/我/ 1。png;
const targetimg2path = '。/我/ 2。png;
const targetimg3path = '。/我/ 3。png;
const targetimg4path = '。/我/ QRcode .webp;
const {工作,2,buffer3 } =产量promise.all(
svg2png(svg1),
svg2png(svg2),
Svg2png (svg3),
});

产量promise.all(
Fs.writeFileAsync(targetimg1path,工作),
Fs.writeFileAsync(targetimg2path,2),
Fs.writeFileAsync(targetimg3path,buffer3),
});

const target1img =图像(targetimg1path);
const t1width = target1img.width();
const t1height = target1img.height();
const offsetx1 =(swidth - t1width) / 2;
const offsety1 = 200;

const target2img =图像(targetimg2path);
const t2width = target2img.width();
const t2height = target2img.height();
const offsetx2 =(swidth - t2width) / 2;
const offsety2 = 240;

const target3img =图像(targetimg3path);
const t3width = target3img.width();
const t3height = target3img.height();
const offsetx3 =(swidth - t3width) / 2;
const offsety3 = 270;

const target4img =图像(targetimg4path);
const t4width = target4img.width();
const t4height = target4img.height();
const offsetx4 =(swidth - t4width) / 2;
const offsety4 = 400;

图像(sourceimg)
(target1img。画,offsetx1,offsety1)
(target2img。画,offsetx2,offsety2)
(target3img。画,offsetx3,offsety3)
(target4img。画,offsetx4,offsety4)
保存(,{,{质量:90 });
})(捕获)(E = console.error(E));

注意事项

文本到SVG需要中文字体支持,否则中文就会被弄乱。
我的坏电脑只花了500毫秒以上。它感到足够分享它,并希望给你一个参考
以上是本文的全部内容,希望能对您有所帮助,希望大家多多支持。
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部