Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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