Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got error "extract_area: bad extract area" when using rotate() when using 0.31.0 #3352

Closed
3 tasks done
j0k3r opened this issue Sep 6, 2022 · 5 comments
Closed
3 tasks done

Comments

@j0k3r
Copy link

j0k3r commented Sep 6, 2022

Possible bug

Is this a possible bug in a feature of sharp, unrelated to installation?

  • Running npm install sharp completes without error.
  • Running node -e "require('sharp')" completes without error.

If you cannot confirm both of these, please open an installation issue instead.

Are you using the latest version of sharp?

  • I am using the latest version of sharp as reported by npm view sharp dist-tags.latest.

If you cannot confirm this, please upgrade to the latest version and try again before opening an issue.

If you are using another package which depends on a version of sharp that is not the latest, please open an issue against that package instead.

What is the output of running npx envinfo --binaries --system --npmPackages=sharp --npmGlobalPackages=sharp?

  System:
    OS: macOS 12.5
    CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
    Memory: 120.48 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 8.13.2 - ~/.nvm/versions/node/v16.15.1/bin/npm
    Watchman: 2022.09.05.00 - /usr/local/bin/watchman
  npmPackages:
    sharp: ^0.31.0 => 0.31.0

What are the steps to reproduce?

I've created a short script to reproduce the bug.
Only sharp is used as deps.

const fs = require('fs')
const sharp = require('sharp')

const image = fs.readFileSync('20171031_PAR.jpg')

const cropAndResize = async (buffer, width, height, topleftX, topleftY, ratio) => {
  const { width: actualWidth, height: actualHeight } = await sharp(buffer).rotate().metadata()

  const newTopleftX = Math.round(topleftX) / 100
  const newTopleftY = Math.round(topleftY) / 100
  const newRatio = ratio / 100
  const left = newTopleftX * actualWidth
  const top = newTopleftY * actualHeight
  const widthToCrop = Math.min(newRatio * actualWidth, actualWidth - left)
  const heightToCrop = Math.min((newRatio * actualWidth) / (width / height), actualHeight - top)

  console.log({
    left: left ? Math.round(left) : 0,
    top: top ? Math.round(top) : 0,
    width: Math.round(widthToCrop),
    height: Math.round(heightToCrop),
    actualWidth,
    actualHeight,
  })

  const newBuffer = await sharp(buffer)
    .rotate()
    .extract({
      left: left ? Math.round(left) : 0,
      top: top ? Math.round(top) : 0,
      width: Math.round(widthToCrop),
      height: Math.round(heightToCrop),
    })
    .resize({
      height: height ? Number(height) : height,
      width: Number(width),
      withoutEnlargement: true,
    })
    .jpeg({ quality: 80 })
    .toBuffer()

  return newBuffer
}

;(async () => {
  const res = await cropAndResize(image, 100, 80, 0, 19, 100)

  console.log(res)
})()

I'm using that image for my test (called 20171031_PAR.jpg in the script).
20171031_PAR

What is the expected behaviour?

It should log the Buffer after rotate + extract + resize.

Instead I got:

node:internal/process/promises:279
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: extract_area: bad extract area
]

If I remove the rotate() action (the second one used), everything went fine.

The image is 296x382.
The extract info are:

  • left: 0,
  • top: 73,
  • width: 296,
  • height: 237

So the box isn't outside the image.

There are no Orientation information in the given image, so the rotate() should do nothing (but we are using it because sometimes some images have the Orientation info).

Everything run fine on 0.30.7. The bug appears on 0.31.0

@j0k3r j0k3r added the triage label Sep 6, 2022
lovell added a commit that referenced this issue Sep 7, 2022
@lovell
Copy link
Owner

lovell commented Sep 7, 2022

Thanks for reporting this, I can reproduce, it appears to be a regression introduced by me in commit 212a6e7 (v0.31.0) relating to auto-rotation.

Commit fbd4970 should fix this, plus adds a test that would previously have failed. This will be in v0.31.1.

@j0k3r
Copy link
Author

j0k3r commented Sep 12, 2022

@lovell Thanks, I confirm the bug is fixed with your commit 👍

@lovell
Copy link
Owner

lovell commented Sep 12, 2022

Re-opening until the fix is published.

@lovell
Copy link
Owner

lovell commented Sep 29, 2022

v0.31.1 now available

@lovell lovell closed this as completed Sep 29, 2022
@intrepid-ishan
Copy link

intrepid-ishan commented Sep 30, 2022

@lovell
Few images were automatically getting rotated by 360° in the sharp version of 0.31.0

In the latest version(v0.31.1) it is fixed

Thanks👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants