Skip to content

Commit

Permalink
fix square crops having mismatching width/height values
Browse files Browse the repository at this point in the history
Rounding errors often mean that a square crop appears to
generate non square outputs. Usually only out by a few pixels
but its noticeable with a square as you're expecting `w === h`
to be shown in the cropper tool.
  • Loading branch information
13twelve committed Dec 5, 2024
1 parent 90da826 commit 4bc06c4
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion frontend/js/utils/cropper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,37 @@
* Convert crop values between to given range
*/
export const cropConversion = (data, dest, origin) => {
return {
const d = {
x: Math.round(data.x * dest.width / origin.width),
y: Math.round(data.y * dest.height / origin.height),
width: Math.round(data.width * dest.width / origin.width),
height: Math.round(data.height * dest.height / origin.height)
}

// Mike ([email protected]) --
//
// fixing the case of the square...
// rounding errors often mean that a square crop appears to
// generate non square outputs. Usually only out by a few pixels
// but its noticeable with a square as you're expecting w === h
//
// first checking if the crop shape is (nearly) square
// (its subject to its own rounding errors...)
if (Math.abs(data.width - data.height) < 2) {
// storing the difference between calculated width and height
const diff = d.width - d.height;
// setting height to equal width
d.height = d.width;
// we may have made the final crop taller,
// which means we might try and crop dimensions lower than the original image height
// so compensating, if we've cropped lower than the diff
if (diff > 0 && d.y > diff) {
d.y = d.y - diff;
}
}
// -- Mike ([email protected])

return d;
}

export default {
Expand Down

0 comments on commit 4bc06c4

Please sign in to comment.