Sharp: Using overlayWith before a rotate call throws an error

Created on 9 Dec 2017  路  2Comments  路  Source: lovell/sharp

I'm working on a project where I need to extract a piece of an image and then mask the extracted image with a polygon shape and then rotate it. I am using an SVG string buffer as the mask.

When I run this line of code with rotate() I get an error:
"Overlay image must have same dimensions or smaller"

sharp(imageData)
    .extract({left: left, top: top, width: width, height: height})
    .overlayWith(mask, {cutout: true})
    .flip(isMirrored)
    .rotate(rotation)
    .png()
    .toFile(filePath);

When I run it without the rotate call, it runs without error. I am not sure if this is an actual bug or if I may be doing something wrong. I am assuming that the overlayWith is happening after the rotation and that may be what is causing the error.

I also want to thank you for creating this library. It is brilliant!

question

All 2 comments

Hello, your guess is correct, the overlay is applied at the end. You'll probably need to split this into two pipelines, something like:

sharp(imageData)
  .extract({left: left, top: top, width: width, height: height})
  .overlayWith(mask, { cutout: true })
  .raw()
  .toBuffer({ resolveWithObject: true })
  .then(({ data, info }) => sharp(data, { raw: info })
    .flip(isMirrored)
    .rotate(rotation)
    .png()
    .toFile(filePath)
  );

Ah thanks! I will do it that way.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

genifycom picture genifycom  路  3Comments

paulieo10 picture paulieo10  路  3Comments

terbooter picture terbooter  路  3Comments

natural-law picture natural-law  路  3Comments

AVVS picture AVVS  路  3Comments