Sharp: Can you use the sharp constructor with a buffer or pipe from an http request?

Created on 6 Aug 2015  路  2Comments  路  Source: lovell/sharp

Hi, I'm trying to resize an image based on a url. I have things working so that I can fetch the image from the url, copy it to the server, and then resize, but I would like to eliminate the need to copy the image onto the server.

When I try to use a buffer I get the following error:

Input file is of an unsupported image formatVipsForeignLoad

The way I'm trying to do this is like this with - https://github.com/request/request:

var sharp = require('sharp'),
        request = require('request');

request(url, function(err, res, bodyBuffer) {
    sharp(bodyBuffer)....
});

Am I using the constructor wrong? Do you think this the right approach, or do you have any alternative ideas?

Thanks for any insights!

question

Most helpful comment

https://github.com/request/request#requestoptions-callback says:

if you expect binary data, you should set encoding: null

so perhaps try:

request({url: url, encoding: null}, function(err, res, bodyBuffer) {
    sharp(bodyBuffer)....
});

All 2 comments

https://github.com/request/request#requestoptions-callback says:

if you expect binary data, you should set encoding: null

so perhaps try:

request({url: url, encoding: null}, function(err, res, bodyBuffer) {
    sharp(bodyBuffer)....
});

@lovell thanks for catching that in the docs! works perfectly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AVVS picture AVVS  路  3Comments

jaydenseric picture jaydenseric  路  3Comments

kachurovskiy picture kachurovskiy  路  3Comments

genifycom picture genifycom  路  3Comments

OleVik picture OleVik  路  3Comments