How can I get the width and height of an image based on image path?


#1

Hi,
Is there any way to get dimensions(width and height) of the image by giving the path?

Thanks.


#2

You can use the ImageSource module.

fromFile(path).width // or .height


#3

I tried that but getting this error

TypeError: null is not an object (evaluating ‘fromFile.width’)

This is how the path looks like
https://dummy.com/1-u0s5s8DN0xl7kz9nJSuoCf6148P0uwvG.jpg


#4

You never mentioned its a remote image, you can’t read it’s width until it’s downloaded. Use fromUrl,

fromUrl(path)
.then((imageSource) => {
 imageSource.width // or .height
});

#5

Yay its working for remote images. should I use ‘fromFile’ for local paths then?


#6

Yes, fromFile is for local images.


#7

I am getting the width and height. can you confirm that in which format the width and height are Pixels or DIPS ?


#8

It should be pixels.


#9

Hi,

Looks like it is in DIPs. Also if the image is overflowing out of the parent then the image dimensions we fetching here is not correct (I believe that the extra image part which is out of the parent is not being considered as part of the whole image, in dimensions calculations).

Thanks.


#10

Just now found that, I am actually getting the 90% of the device dimension values. For example (in my case) :

deviceWidth * .9 => imageWidth (320 * .9 = 288)
deviceHeight * .9 => imageHeight (568 * .9 = 512)

This is strange!

Please help.


#11

Try to update this example, if the issue is reproduce please let us know.