• 技术文章 >web前端 >js教程

    node操作文字生成图片

    php中世界最好的语言php中世界最好的语言2018-04-16 14:51:18原创1118

    这次给大家带来node操作文字生成图片,node操作文字生成图片的注意事项有哪些,下面就是实战案例,一起来看一下。

    解决思路

    文字转svg -> svg转png -> 合并图片

    相关轮子

    1. images Node.js 轻量级跨平台图像编解码库,不需要额外安装依赖

    2. text-to-svg 文字转svg

    3. svg2png svg转png图片

    示例代码

    'use strict';
    const fs = require('fs');
    const images = require('images');
    const TextToSVG = require('text-to-svg');
    const svg2png = require("svg2png");
    const Promise = require('bluebird');
    Promise.promisifyAll(fs);
    const textToSVG = TextToSVG.loadSync('fonts/文泉驿微米黑.ttf');
    const sourceImg = images('./i/webwxgetmsgimg.jpg');
    const sWidth = sourceImg.width();
    const sHeight = sourceImg.height();
    const svg1 = textToSVG.getSVG('魏长青-人人讲App', {
     x: 0,
     y: 0,
     fontSize: 24,
     anchor: 'top',
    });
    const svg2 = textToSVG.getSVG('邀请您参加', {
     x: 0,
     y: 0,
     fontSize: 16,
     anchor: 'top',
    });
    const svg3 = textToSVG.getSVG('人人讲课程', {
     x: 0,
     y: 0,
     fontSize: 32,
     anchor: 'top',
    });
    Promise.coroutine(function* generateInvitationCard() {
     const targetImg1Path = './i/1.png';
     const targetImg2Path = './i/2.png';
     const targetImg3Path = './i/3.png';
     const targetImg4Path = './i/qrcode.jpg';
     const [buffer1, buffer2, buffer3] = yield Promise.all([
      svg2png(svg1),
      svg2png(svg2),
     svg2png(svg3),
     ]);
     yield Promise.all([
      fs.writeFileAsync(targetImg1Path, buffer1),
      fs.writeFileAsync(targetImg2Path, buffer2),
      fs.writeFileAsync(targetImg3Path, buffer3),
     ]);
     const target1Img = images(targetImg1Path);
     const t1Width = target1Img.width();
     const t1Height = target1Img.height();
     const offsetX1 = (sWidth - t1Width) / 2;
     const offsetY1 = 200;
     const target2Img = images(targetImg2Path);
     const t2Width = target2Img.width();
     const t2Height = target2Img.height();
     const offsetX2 = (sWidth - t2Width) / 2;
     const offsetY2 = 240;
     const target3Img = images(targetImg3Path);
     const t3Width = target3Img.width();
     const t3Height = target3Img.height();
     const offsetX3 = (sWidth - t3Width) / 2;
     const offsetY3 = 270;
     const target4Img = images(targetImg4Path);
     const t4Width = target4Img.width();
     const t4Height = target4Img.height();
     const offsetX4 = (sWidth - t4Width) / 2;
     const offsetY4 = 400;
     images(sourceImg)
     .draw(target1Img, offsetX1, offsetY1)
     .draw(target2Img, offsetX2, offsetY2)
     .draw(target3Img, offsetX3, offsetY3)
     .draw(target4Img, offsetX4, offsetY4)
     .save('./i/card.png', { quality : 90 });
    })().catch(e => console.error(e));

    注意事项

    text-to-svg需要中文字体的支持,不然中文会乱码

    在我的破电脑上执行一次只花了500多毫秒,感觉足够了,分享出来希望能给大家一个参照

    相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

    推荐阅读:

    JS的正则表达式应用

    JS中typeof和类型判断(附代码)

    JS怎样检测浏览器内的脚本

    以上就是node操作文字生成图片的详细内容,更多请关注php中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:node 图片 生成
    上一篇:AngularJs用户登录时交互及验证步奏详解 下一篇:正则的使用案例及基本语法
    大前端线上培训班

    相关文章推荐

    • javascript暂停功能如何实现• javascript如何求图形的面积• javascript object怎么转json• javascript怎么设置字体大小• javascript如何改变HTML内容

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网