Sharp: "Error: Input buffer contains unsupported image format" for some remote images

Created on 7 Jun 2018  路  5Comments  路  Source: lovell/sharp

var resizer = sharp().resize( 300, 300 ).withoutEnlargement().max().toFile( 'public/img_thumbs/' + thumb_name, ( err, info ) =>
{
       console.log( err );
});

request( url ).pipe( resizer );

Any idea why I'm getting "Error: Input buffer contains unsupported image format" for remote images like these?

https://scontent.fsof3-1.fna.fbcdn.net/v/t1.0-9/34457169_729308874126794_510102304989380608_n.jpg?_nc_cat=0&oh=fec2a8806865bcbe2009a7a8108f543d&oe=5BAE65A5

https://i.redditmedia.com/xqXYvpuqZNxcLleQ-xQvT09_oLNiUuXi9RHTP6q15r8.jpg?fit=crop&crop=faces%2Centropy&arh=2&w=960&s=86914a55b26ff161fd02d0491f798438

I realize that maybe it's more of a request module issue but I can't figure out how to fix it specifically with sharp, besides the request response.headers['content-type'] returns correctly "image/jpeg".

question

Most helpful comment

@pig-cop Could you share how you actually solved it? I'm having the same issue.

All 5 comments

Hello, please see #930

I checked out the request documentation and tried that before posting here, but still no luck with those urls. Code as follows:

var resizer = sharp().resize( 300, 300 ).toFile( 'public/imgs/image.jpg', ( err, info ) =>
{
       console.log( err );
});
request( { url, encoding: null } ).pipe( resizer );

This still outputs "Input buffer contains unsupported image format".

It is worth mentioning that using the image referenced in #930
(https://padletuploads.blob.core.windows.net/dev/2/1d5f5e1d281a2145aae25595d050e77b/JPG_Image.jpg) I have no issues at all.

I solved it and it was the craziest thing!
Those url strings, coming from html input forms, had url encoded ampersands!

@pig-cop Could you share how you actually solved it? I'm having the same issue.

Just in case anyone stumbled over this problem.

I used superagent to pipe the response directly to sharp.
I had the problem that this unhandled error crashed express every time someone uploaded a faulty image. try/catch didn't help.

Here is how I fixed it:

const request = require('superagent');
// ... 
request({url, method: 'GET'}).pipe(transformer).on('error', (e) => {
  console.log(e);
}).pipe(res);

This will gracefully handle the error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhump picture zhump  路  3Comments

natural-law picture natural-law  路  3Comments

kachurovskiy picture kachurovskiy  路  3Comments

tomercagan picture tomercagan  路  3Comments

genifycom picture genifycom  路  3Comments