Sharp: how can i crop by percentage?

Created on 26 Sep 2018  路  2Comments  路  Source: lovell/sharp

i'd like to slice an image into 2 (bottom half + top half)

question

Most helpful comment

Hello, if I've understood you correctly, a combination of metadata and extract should help here, something like (untested):

sharp(input)
  .metadata()
  .then(({ width, height }) => {
    const halfHeight = Math.round(height / 2);
    return [
      sharp(input)
        .extract({ top: 0, left: 0, width, height: halfHeight })
        .toBuffer(),
      sharp(input)
        .extract({ top: halfHeight, left: 0, width, height: height - halfHeight })
        .toBuffer()
    ];
  })
  .then(([upper, lower]) => { ... });

All 2 comments

Hello, if I've understood you correctly, a combination of metadata and extract should help here, something like (untested):

sharp(input)
  .metadata()
  .then(({ width, height }) => {
    const halfHeight = Math.round(height / 2);
    return [
      sharp(input)
        .extract({ top: 0, left: 0, width, height: halfHeight })
        .toBuffer(),
      sharp(input)
        .extract({ top: halfHeight, left: 0, width, height: height - halfHeight })
        .toBuffer()
    ];
  })
  .then(([upper, lower]) => { ... });

Hope this helped. Feel free to re-open with more details if not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vermin1337 picture vermin1337  路  3Comments

janaz picture janaz  路  3Comments

emmtte picture emmtte  路  3Comments

genifycom picture genifycom  路  3Comments

terbooter picture terbooter  路  3Comments