Sharp: Get image width and height from stream

Created on 20 Apr 2017  路  9Comments  路  Source: lovell/sharp

Hi! Thanks for an excellent module!

I'm trying to read image width and height from a stream, but nothing happens.
In the code below nothing is printed to console.
If I remove .pipe(metaReader) the response is printed etc.

What am I missing?

    const request = require('request')
    const sharp = require('sharp')

    let image

    const metaReader = sharp()
      .on('info', info => {
        image = info
      })
      .on('error', err => {
        console.log(err)
      })

    request
      .get('https://images-na.ssl-images-amazon.com/images/M/MV5BMTUyMjM2NTgwNl5BMl5BanBnXkFtZTgwMTUzOTUwMjI@._V1_SX1777_CR0,0,1777,999_AL_.jpg')
      .pipe(metaReader)
      .on('response', function(response) {
        console.log(response) // 200
      })
      .on('error', err => {
        console.log(err)
      })
      .on('end', () => {
        console.log('end')
      })
question

Most helpful comment

You'll need to tell metaReader what to do. If you're expecting metadata about the input image then try something like (untested):

const metaReader = sharp()
  .metadata()
  .then(info => {
    console.log(info)
  })
  .catch(err => {
    console.log(err)
  })

All 9 comments

Hello, it looks like you're attempting to register the on handlers with sharp rather than request.

Perhaps try the following (untested):

    request
      .get('https://images-na.ssl-images-amazon.com/images/M/MV5BMTUyMjM2NTgwNl5BMl5BanBnXkFtZTgwMTUzOTUwMjI@._V1_SX1777_CR0,0,1777,999_AL_.jpg')
      .on('response', function(response) {
        console.log(response) // 200
      })
      .on('error', err => {
        console.log(err)
      })
      .on('end', () => {
        console.log('end')
      })
      .pipe(metaReader)

Thanks for the response! Changed the code to this:

const request = require('request')
const sharp = require('sharp')

const metaReader = sharp()
  .on('info', info => {
    console.log(info)
  })
  .on('error', err => {
    console.log(err)
  })

request
  .get('https://images-na.ssl-images-amazon.com/images/M/MV5BMTUyMjM2NTgwNl5BMl5BanBnXkFtZTgwMTUzOTUwMjI@._V1_SX1777_CR0,0,1777,999_AL_.jpg')
  .on('error', err => {
    console.log(err)
  })
  .on('end', () => {
    console.log('end')
  })
  .pipe(metaReader)

Now end is printed, but nothing else.

You'll need to tell metaReader what to do. If you're expecting metadata about the input image then try something like (untested):

const metaReader = sharp()
  .metadata()
  .then(info => {
    console.log(info)
  })
  .catch(err => {
    console.log(err)
  })
const request = require('request')
const sharp = require('sharp')

const metaReader = sharp()
  .metadata()
  .then(info => {
    console.log(info)
  })

request
  .get('https://images-na.ssl-images-amazon.com/images/M/MV5BMTUyMjM2NTgwNl5BMl5BanBnXkFtZTgwMTUzOTUwMjI@._V1_SX1777_CR0,0,1777,999_AL_.jpg')
  .on('error', err => {
    console.log(err)
  })
  .on('end', () => {
    console.log('end')
  })
  .pipe(metaReader)
$ node test.js
stream.js:45
  dest.on('drain', ondrain);
       ^

TypeError: dest.on is not a function
    at Request.Stream.pipe (stream.js:45:8)
    at Request.pipe (node_modules/request/request.js:1412:34)

Sharp is returning a Promise in this instance.

This issue is not a big problem for me, if can drop it if you'd like.

Try:

const metaReader = sharp()

metaReader
  .metadata()
  .then(info => {
    console.log(info)
  })

That worked, thanks.

end
{ format: 'jpeg',
  width: 1777,
  height: 999,
  space: 'srgb',
  channels: 3,
  hasProfile: false,
  hasAlpha: false }

Does Sharp need to download the full image to read dimensions? Or just the first bytes?

Sorry, missed your question (too many GH notifications)... you should be able to get away with passing a truncated Buffer with only the header to read image dimensions, but watch out for things like large ICC profiles that can be part of the header. Hope this helps.

Thanks for excellent support and your time!

@lovell I am trying to upload an image and get the image dimension in Nodejs. How can I assign values of the image dimension to a variable?

const metaReader = sharp()
metaReader
  .metadata()
  .then(info => {
    console.log(info)
  })
let metainfo = stream.pipe(metaReader)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

genifycom picture genifycom  路  3Comments

henbenla picture henbenla  路  3Comments

OleVik picture OleVik  路  3Comments

AVVS picture AVVS  路  3Comments

natural-law picture natural-law  路  3Comments