react-images-uploading icon indicating copy to clipboard operation
react-images-uploading copied to clipboard

Getting Image width/height with the preview

Open levelingup opened this issue 2 years ago • 2 comments

Is there a way to capture the image's width and height so then it can be used to see its aspect ratio?

levelingup avatar Sep 24 '21 02:09 levelingup

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?

Nimsrules avatar Oct 31 '21 06:10 Nimsrules

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)
}

Tpleme avatar Sep 26 '22 20:09 Tpleme