例如:https://preview.redd.it/04o5k2n06zd71.jpg?width=960&crop=smart&auto=webp&s=7baa25e7e1bde3436cb2a265e2fc8c6cb9a758ee
我如何下载/加载图像到缓冲区
注意:结局总是可以不同的。
下面是使用nodejs的got()
库的实现:
const got = require('got');
const url =
'https://preview.redd.it/04o5k2n06zd71.jpg?width=960&crop=smart&auto=webp&s=7baa25e7e1bde3436cb2a265e2fc8c6cb9a758ee';
got(url, { responseType: 'buffer' }).then(result => {
// result.body contains a Buffer object with the image in it
console.log(`content-type: ${result.headers['content-type']}`);
console.log(`isBuffer: ${Buffer.isBuffer(result.body)}`);
console.log(result.body);
}).catch(err => {
console.log(err);
});
你可以直接用nodejs运行这段代码(在安装got()
库之后)。当我运行它时,它生成如下输出:
content-type: image/jpeg
isBuffer: true
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e2 02 1c 49 43 43 5f 50 52 4f 46 49 4c 45 00 01 01 00 00 02 0c 6c 63 6d 73 02 10 00 00 ... 89335 more bytes>