Created
July 24, 2019 03:47
-
-
Save anchetaWern/7c0bb346ea0195b943f9e0c4143fd678 to your computer and use it in GitHub Desktop.
get image aspect ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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