Determining image file size + dimensions via Javascript?

后端 未结 11 1097
执念已碎
执念已碎 2020-11-22 07:00

As part of a web app, once images have been downloaded and rendered on a web page, I need to determine an image\'s file size (kb) and resolution within the browser context (

11条回答
  •  逝去的感伤
    2020-11-22 07:43

    Getting the Original Dimensions of the Image

    If you need to get the original image dimensions (not in the browser context), clientWidth and clientHeight properties do not work since they return incorrect values if the image is stretched/shrunk via css.

    To get original image dimensions, use naturalHeight and naturalWidth properties.

    var img = document.getElementById('imageId'); 
    
    var width = img.naturalWidth;
    var height = img.naturalHeight;
    

    p.s. This does not answer the original question as the accepted answer does the job. This, instead, serves like addition to it.

提交回复
热议问题