【发布时间】:2019-01-22 20:45:58
【问题描述】:
我正在使用node-gd 处理一些图像,试图通过他们的 api 上传到 twitter。
这样做效果很好:
fs.readFileSync('test.png', { encoding: 'base64' })
但我希望有一种方法可以做到这一点,而无需将其保存到文件然后再次加载。
其他人建议这样做:
var gd = require('node-gd');
var img = gd.openPng('test.png');
var buff = Buffer.from(img.pngPtr(), 'binary');
var b64 = buff.toString('base64');
console.log(b64);
但是当我这样做时,我收到一条错误消息 TypeError: binary is not a function,这对我来说毫无意义。
完全错误:
/home/ubuntu/workspace/test.js:16
var buff = Buffer.from(img.pngPtr(), 'binary');
^
TypeError: binary is not a function
at Function.from (native)
at Function.from (native)
at Object.<anonymous> (/home/ubuntu/workspace/test.js:16:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3
【问题讨论】: