Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Bail prior to establishing dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinsina committed Nov 7, 2022
1 parent 9aba1e3 commit add140d
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions packages/image/src/crop-rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,30 @@ module.exports = ({ width, height, cropDimensions }) => {
cropped: false,
});
}

const coords = ['x1', 'x2', 'y1', 'y2'];
if (coords.some(key => cropDimensions[key] == null || Number.isNaN(cropDimensions[key]))) {
return new CropRectangle({
x: 0,
y: 0,
width,
height,
cropped: false,
});
}

// @see Cygnus\ApplicationBundle\Apps\Management\Controller::cropImageAction
const scale = width / 640;
const {
x1,
x2,
y1,
y2,
} = ['x1', 'x2', 'y1', 'y2'].reduce((o, key) => {
} = coords.reduce((o, key) => {
const v = Math.round(cropDimensions[key] * scale);
return { ...o, [key]: v };
}, {});

const dimExists = dimension => (typeof dimension !== 'undefined' && dimension !== null && !Number.isNaN(Number(dimension)));
if (!dimExists(x1) || !dimExists(x2) || !dimExists(y1) || !dimExists(y2)) {
return new CropRectangle({
x: 0,
y: 0,
width,
height,
cropped: false,
});
}

return new CropRectangle({
x: x1,
Expand Down

0 comments on commit add140d

Please sign in to comment.