react-images-uploading
react-images-uploading copied to clipboard
Getting Image width/height with the preview
Is there a way to capture the image's width and height so then it can be used to see its aspect ratio?
I too am looking for an option to capture the dimensions in absolute pixels to compare with widow's innerHeight & innerWidth. @levelingup did you find a way?
You could do the following to get all the info from the file:
const getImageProperties = (file) => {
const picture = URL.createObjectURL(new Blob(file, { type: file[0].type }))
const image = new Image();
image.onload = () => {
file[0].width = image.width;
file[0].height = image.height;
const GCD = findImageGCD(image.width, image.height);
file[0].aspectRatio = `${image.width / GCD}:${image.height / GCD}`
}
image.src = picture;
}
//Function tho get the aspect ratio
const findImageGCD = (w, h) => {
if (h === 0) return w
return findImageGCD(h, w % h)
}