Skip to content

Instantly share code, notes, and snippets.

@anchetaWern
Created July 24, 2019 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchetaWern/7c0bb346ea0195b943f9e0c4143fd678 to your computer and use it in GitHub Desktop.
Save anchetaWern/7c0bb346ea0195b943f9e0c4143fd678 to your computer and use it in GitHub Desktop.
get image aspect ratio
function getRandomInt(min, max) {
let sign = Math.random() < 0.5 ? -1 : 1;
min = Math.ceil(min);
max = Math.floor(max);
return (Math.floor(Math.random() * (max - min + 1)) + min) * sign;
}
function getNewHeight(oldWidth, oldHeight, newWidth) {
const aspectRatio = (oldWidth / oldHeight);
const newHeight = (newWidth / aspectRatio);
return Math.floor(newHeight);
}
const images_with_dimensions = [];
images.forEach((item) => {
const { width: oldWidth, height: oldHeight } = Image.resolveAssetSource(item);
const newWidth = base_width - getRandomInt(5, 30);
const newHeight = getNewHeight(oldWidth, oldHeight, newWidth);
images_with_dimensions.push({
source: item,
width: newWidth,
height: newHeight,
org_width: oldWidth,
org_height: oldHeight
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment