Sharp: How can i condition the max() or min() without breaking the chain

Created on 22 Jun 2017  路  2Comments  路  Source: lovell/sharp

Sharp(data.Body)
.resize(imageWidth, imageHeight)
(.max()<- this should be optional, for ex: if nocrop == true)
.toFormat(imageFormat)
.toBuffer()

question

Most helpful comment

I think this is pretty much your only option:

var sharpInstance = Sharp(data.Body)
  .resize(imageWidth, imageHeight);
if (nocrop) {
  sharpInstance = sharpInstance.max();
}
sharpInstance
  .toFormat(imageFormat)
  .toBuffer();

All 2 comments

I think this is pretty much your only option:

var sharpInstance = Sharp(data.Body)
  .resize(imageWidth, imageHeight);
if (nocrop) {
  sharpInstance = sharpInstance.max();
}
sharpInstance
  .toFormat(imageFormat)
  .toBuffer();

If it absolutely has to be a single chain then the following is untested but will probably work:

Sharp(data.Body)
  .resize(imageWidth, imageHeight)
  [nocrop ? 'max' : 'crop']()
  .toFormat(imageFormat)
  .toBuffer()

...but please use @papandreou's suggestion for more easily maintained code :)

Was this page helpful?
0 / 5 - 0 ratings