云函数代码:
// 云函数入口文件
const cloud = require('wx-server-sdk')
const TextToSVG = require('text-to-svg');
const svg2png = require("svg2png");
const textToSVG = TextToSVG.loadSync('FuturaNDforNike365Cn-XBd.ttf');
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
return new Promise((resolve, reject) => {
const svg1 = textToSVG.getSVG(event.words, {
x: 0,
y: 0,
fontSize: 24,
anchor: 'top',
});
svg2png(svg1).then(buffer1 => {
let wordBase64 = "data:image/png;base64," + buffer1.toString('base64');
let result = {
data: wordBase64,
msg: "调用成功",
words: event.words
}
console.log(wordBase64);
resolve(result)
})
})
}
本地调试:
调用时返回:
云端测试:
你得保证云服务器上也能读取这个文件:
TextToSVG.loadSync('FuturaNDforNike365Cn-XBd.ttf');
否则返回的是空的就会报错must be a non-empty string。
你本地调试不报错应该是本地有这个文件
添加await试下
exports.main = async (event, context) => { return await new Promise((resolve, reject) => { const svg1 = textToSVG.getSVG(event.words, { x: 0, y: 0, fontSize: 24, anchor: 'top', }); svg2png(svg1).then(buffer1 => { let wordBase64 = "data:image/png;base64," + buffer1.toString('base64'); let result = { data: wordBase64, msg: "调用成功", words: event.words } console.log(wordBase64); resolve(result) }) }) }