Sharp: resize() after composite() cause error

Created on 29 Mar 2019  路  6Comments  路  Source: lovell/sharp

Hi,

There is a wierd issue when Im trying to resize() after composite(), the source is:

sharp({
    create: {
        width: 789,
        height: 789,
        channels: 4,
        background: '#bfff00'
    }
}).png().composite([{ input: content }]).resize(192).toBuffer((err, data) => {
    if (err)
        return callback(err);
    this.emitFile(target, data);
    callback(null, '');
});

And it halted with Image to composite must have same dimensions or smaller.

If I deleted .resize(192), it works fine.

If I deleted .png(), the same error would be occurred again.

question

Most helpful comment

You'll need to break this into two pipelines, something like (untested):

sharp({
  create: {
    width: 789,
    height: 789,
    channels: 4,
    background: '#bfff00'
  }
})
  .composite([{
    input: content
  }])
  .png()
  .toBuffer()
  .then(data => sharp(data)
    .resize(192)
    .toBuffer()
  )
  .then( ... );

All 6 comments

You'll need to break this into two pipelines, something like (untested):

sharp({
  create: {
    width: 789,
    height: 789,
    channels: 4,
    background: '#bfff00'
  }
})
  .composite([{
    input: content
  }])
  .png()
  .toBuffer()
  .then(data => sharp(data)
    .resize(192)
    .toBuffer()
  )
  .then( ... );

Yup. I finally solved that with two instances :-p

I've encountered the same problem when performing resizing after composition.

Thanks @lovell , this solution indeed works.

But I'm still curious about why a naive combination like

sharp(data).composite(...).resize(...)

doesn't work

I also ran into this issue.

I think, the issue is, that the call order is not used in the different transformations. So it is not important, if you do resize and composite or composite and resize.

Think @johannwagner is correct above, but regardless of the call order, to me it would seem to make more sense if the resize() is applied to the final composite image, not just the input base image, but not sure how others feel about that....?

This is somewhat non-intuitive; a logical expectation would be that compositing images together would also allow them being processed together.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

janaz picture janaz  路  3Comments

Andresmag picture Andresmag  路  3Comments

OleVik picture OleVik  路  3Comments

genifycom picture genifycom  路  3Comments

kachurovskiy picture kachurovskiy  路  3Comments