博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node生成图片
阅读量:7116 次
发布时间:2019-06-28

本文共 3747 字,大约阅读时间需要 12 分钟。

问题描述

最进公司在做的小程序,但是小程序分享只能是图片,不能自定义样式 但是我们还有个需求,就是分享出去的 必须要让人直观的看出求职者的 基本信息,功夫不负有心人,最后给解决了!

直接上代码

const fs = require("fs");const images = require("images");const TextToSVG = require("text-to-svg");const svg2png = require("svg2png");const Promise = require("bluebird");const path = require("path");const imgPath = (value) => { return path.join(__dirname, value) };Promise.promisifyAll(fs);/** * 具体转化流程 * 文字转svg -> svg转png -> 合并图片 *  * 具体所需判断 * 头像             没有头像判断 男女 显示默认头像 * 姓名              * 所在公司         长度显示13位 多余后 三个点显示      没有公司 显示所毕业院校 * 薪资             没有薪资 显示 所学专业 */let name = "刘雯卿";let company = "58同城";let maxPay = "8000元/月" // 引入默认背景图const sourceImg = images(imgPath('./work/default.png'));const sWidth = sourceImg.width();const sHeight = sourceImg.height();// 设置字体颜色以及基准点const textBoldToSVG = TextToSVG.loadSync(imgPath('fonts/PINGFANG_HEAVY.TTF'));const textToSVG = TextToSVG.loadSync(imgPath('fonts/PINGFANG_REGULAR.TTF')); const svg1 = textBoldToSVG.getSVG(name, {    x: 0,                       // 文本开头的水平位置(默认值:0)    y: 0,                       // 文本的基线的垂直位置(默认值:0)    fontSize: 50,               // 文本的大小(默认:)72    // letterSpacing: "",          // 设置字母的间距    anchor: 'top',              // 坐标中的对象锚点});const svg2 = textToSVG.getSVG(company, {    x: 0,    y: 0,    fontSize: 38,    anchor: 'top',});const svg3 = textBoldToSVG.getSVG(maxPay, {    x: 0,    y: 0,    fontSize: 38,    anchor: 'top',});// 设置颜色const svgOne = svg1.replace(`xmlns="http://www.w3.org/2000/svg"`, `fill="#fff" xmlns="http://www.w3.org/2000/svg"`);const asdTwo = svg2.replace(`xmlns="http://www.w3.org/2000/svg"`, `fill="#fff" xmlns="http://www.w3.org/2000/svg"`);const asdThree = svg3.replace(`xmlns="http://www.w3.org/2000/svg"`, `fill="#fff" xmlns="http://www.w3.org/2000/svg"`);Promise.coroutine(function* generateInvitationCard() {    // 定义路径    const targetImg1Path = './work/1.png';    const targetImg2Path = './work/2.png';    const targetImg3Path = './work/3.png';    const targetImg5Path = './work/man@124.png';        // 默认男女头像    const targetImg4Path = './work/woman@124.png';      // 默认男女头像    // 转buffer    const [buffer1, buffer2, buffer3] = yield Promise.all([        svg2png(svgOne),        svg2png(asdTwo),        svg2png(asdThree),    ]);    // buffer 写入图片    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 = 285;    const target3Img = images(targetImg3Path);    const t3Width = target3Img.width();    const t3Height = target3Img.height();    const offsetX3 = (sWidth - t3Width) / 2;    const offsetY3 = 360;    const target4Img = images(targetImg4Path);    const t4Width = target4Img.width();    const t4Height = target4Img.height();    const offsetX4 = (sWidth - t4Width) / 2;    const offsetY4 = 34;    // 写入图片    images(sourceImg)                               // 从文件中加载图片        // .size(400)                               // 几何缩放图像到400像素宽度        .draw(images(target1Img), offsetX1, offsetY1)        .draw(images(target2Img), offsetX2, offsetY2)        .draw(images(target3Img), offsetX3, offsetY3)        .draw(images(target4Img), offsetX4, offsetY4)        .save('./work/card.png', {                  // 将图像保存到一个文件中,质量为90            quality: 90         });})().catch(e => {    console.error(e)});复制代码

转载地址:http://pdzel.baihongyu.com/

你可能感兴趣的文章
RSA加密算法实现 Java
查看>>
【cocos2dx进阶】调试篇(3)cocos2dx的Log改造
查看>>
CentOS中vsftp安装与配置
查看>>
linux exec的用法
查看>>
我的友情链接
查看>>
Spark Graphx编程指南
查看>>
配置tomcat
查看>>
基于glusterfs和gearman的离线任务运算分布式化方案介绍
查看>>
小学生信息技术课的有效教学
查看>>
天堂与地狱的区别
查看>>
java io小实例
查看>>
127小时
查看>>
Windows Server 2008 R2 SP1中的具体改进
查看>>
Autoit 自动化安装软件
查看>>
shell 脚本-----循环数组
查看>>
Merge into 详细介绍
查看>>
MySQL Server参数优化 - innodb_file_per_table(独立表空间)
查看>>
ubuntu中文出现乱码
查看>>
Linux系统命令之tcpdump
查看>>
第 八 天 : 复 习 中 ( 一 )
查看>>