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

Fix square crops having mismatching width/height values #2706

Open
wants to merge 1 commit into
base: 3.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading