Skip to content

Commit

Permalink
BUGFIX: fix specific cases where the requested croparea would be larg…
Browse files Browse the repository at this point in the history
…er than the image itself
  • Loading branch information
patricekaufmann authored Jul 11, 2023
1 parent 4e7809c commit 5478f0a
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,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 5478f0a

Please sign in to comment.