Skip to content

Commit

Permalink
Merge pull request #3578 from patricekaufmann/bugfix/pixelsnapping-cr…
Browse files Browse the repository at this point in the history
…op-overflow

BUGFIX: fix bug with pixelSnapping on certain aspectRatios and image dimensions
  • Loading branch information
mhsdesign authored Aug 2, 2024
2 parents bc789ca + 5478f0a commit 6d04033
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,14 @@ export default class ImageCropper extends PureComponent {
const normalizedAspectRatioHeight = currentAspectRatioStrategy.height / aspectRatioGcd;

// pixel perfect calculations
const naturalCropWidth = Math.floor(imageWidth * (cropArea.width / 100) / normalizedAspectRatioWidth) * normalizedAspectRatioWidth;
const naturalCropHeight = naturalCropWidth / normalizedAspectRatioWidth * normalizedAspectRatioHeight;
let naturalCropWidth = Math.floor(imageWidth * (cropArea.width / 100) / normalizedAspectRatioWidth) * normalizedAspectRatioWidth;
let naturalCropHeight = naturalCropWidth / normalizedAspectRatioWidth * normalizedAspectRatioHeight;

while (naturalCropHeight > imageHeight) {
// can't crop area larger than image itself, so keep subtracting normalized aspect ratio values until area is valid
naturalCropHeight -= normalizedAspectRatioHeight;
naturalCropWidth -= normalizedAspectRatioWidth;
}

// modify cropArea with pixel snapping values
cropArea.width = (naturalCropWidth / imageWidth) * 100;
Expand Down

0 comments on commit 6d04033

Please sign in to comment.