如何将图像从url加载到nodejs中的缓冲区中

时间:2023-01-24 16:11:40

I am new to nodejs and am trying to set up a server where i get the exif information from an image. My images are on S3 so I want to be able to just pass in the s3 url as a parameter and grab the image from it.

我是nodejs的新手,我正在尝试建立一个服务器,我从图像中获取exif信息。我的图像在S3上,所以我希望能够将s3 url作为参数传递并从中获取图像。

I am u using the ExifImage project below to get the exif info and according to their documentation:

我使用下面的ExifImage项目来获取exif信息并根据他们的文档:

"Instead of providing a filename of an image in your filesystem you can also pass a Buffer to ExifImage."

“您可以将缓冲区传递给ExifImage,而不是在文件系统中提供图像文件名。”

How can I load an image to the buffer in node from a url so I can pass it to the ExifImage function

如何从URL中将图像加载到节点中的缓冲区,以便将其传递给ExifImage函数

ExifImage Project: https://github.com/gomfunkel/node-exif

ExifImage项目:https://github.com/gomfunkel/node-exif

Thanks for your help!

谢谢你的帮助!

3 个解决方案

#1


29  

Try setting up request like this:

尝试设置这样的请求:

var request = require('request').defaults({ encoding: null });
request.get(s3Url, function (err, res, body) {
      //process exif here
});

This will cause request to output a buffer instead of a string.

这将导致请求输出缓冲区而不是字符串。

#2


5  

Use the request library.

使用请求库。

request('<s3imageurl>', function(err, response, buffer) {
    // Do something
});

Also, node-image-headers might be of interest to you. It sounds like it takes a stream, so it might not even have to download the full image from S3 in order to process the headers.

此外,您可能会对node-image-headers感兴趣。听起来它需要一个流,所以甚至可能不必从S3下载完整的图像来处理标题。

Updated with correct callback signature.

更新了正确的回调签名。

#3


0  

I was able to solve this only after reading that encoding: null is required and providing it as an parameter to request.

我只有在读取了编码后才能解决这个问题:需要null并将其作为请求参数提供。

This will download the image from url and produce a buffer with the image data.

这将从url下载图像并生成带有图像数据的缓冲区。

Using the request library -

使用请求库 -

const request = require('request');

let url = 'http://website.com/image.png';
request({ url, encoding: null }, (err, resp, buffer) => {
     // Use the buffer
     // buffer contains the image data
     // typeof buffer === 'object'
});

Note: omitting the encoding: null will result in an unusable string and not in a buffer. Buffer.from won't work correctly too.

注意:省略encoding:null将导致不可用的字符串而不是缓冲区。 Buffer.from也无法正常工作。

This was tested with Node 8

这是使用Node 8测试的

#1


29  

Try setting up request like this:

尝试设置这样的请求:

var request = require('request').defaults({ encoding: null });
request.get(s3Url, function (err, res, body) {
      //process exif here
});

This will cause request to output a buffer instead of a string.

这将导致请求输出缓冲区而不是字符串。

#2


5  

Use the request library.

使用请求库。

request('<s3imageurl>', function(err, response, buffer) {
    // Do something
});

Also, node-image-headers might be of interest to you. It sounds like it takes a stream, so it might not even have to download the full image from S3 in order to process the headers.

此外,您可能会对node-image-headers感兴趣。听起来它需要一个流,所以甚至可能不必从S3下载完整的图像来处理标题。

Updated with correct callback signature.

更新了正确的回调签名。

#3


0  

I was able to solve this only after reading that encoding: null is required and providing it as an parameter to request.

我只有在读取了编码后才能解决这个问题:需要null并将其作为请求参数提供。

This will download the image from url and produce a buffer with the image data.

这将从url下载图像并生成带有图像数据的缓冲区。

Using the request library -

使用请求库 -

const request = require('request');

let url = 'http://website.com/image.png';
request({ url, encoding: null }, (err, resp, buffer) => {
     // Use the buffer
     // buffer contains the image data
     // typeof buffer === 'object'
});

Note: omitting the encoding: null will result in an unusable string and not in a buffer. Buffer.from won't work correctly too.

注意:省略encoding:null将导致不可用的字符串而不是缓冲区。 Buffer.from也无法正常工作。

This was tested with Node 8

这是使用Node 8测试的