Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load an image from url into buffer in nodejs

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.

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

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

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

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

Thanks for your help!

like image 920
CodeMonkeyB Avatar asked Aug 16 '13 01:08

CodeMonkeyB


People also ask

What is Node js buffers How do you create it?

Buffers allocate raw memory outside the V8 heap. Buffer class is a global class so it can be used without importing the Buffer module in an application. Creating Buffers: Followings are the different ways to create buffers in Node. js: Create an uninitiated buffer: It creates the uninitiated buffer of given size.

Is buffer a node js?

The buffers module provides a way of handling streams of binary data. The Buffer object is a global object in Node. js, and it is not necessary to import it using the require keyword.


2 Answers

Try setting up request like this:

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

Setting encoding to null will cause request to output a buffer instead of a string.

like image 104
Dan Kohn Avatar answered Sep 16 '22 14:09

Dan Kohn


Use the axios:

const response = await axios.get(url,  { responseType: 'arraybuffer' }) const buffer = Buffer.from(response.data, "utf-8") 
like image 39
Depp Wang Avatar answered Sep 18 '22 14:09

Depp Wang