Can Javascript access native image size?

前端 未结 5 1165
暖寄归人
暖寄归人 2021-01-03 20:30

I\'m writing a jQuery function where I\'d like to access both the native size of an image, and the size specified for it on the page. I\'d like to set a variable for each.

5条回答
  •  孤城傲影
    2021-01-03 21:00

    My solution would be to write a web service that gets/downloads the image, and then gets its resolution and returns it as {width: x,height:y}. Then you call it with $.ajax or equivalent to retrieve this.

    Alternatively you could add the image to a hidden div using

    // e.g. don't set width and height
    $("#hiddendiv").html(""); 
    

    And then get the div's width/height though I haven't tried it.

提交回复
热议问题