[removed] Get image dimensions

后端 未结 8 1816
攒了一身酷
攒了一身酷 2020-11-29 19:11

I only have a URL to an image. I need to determine the height and width of this image using only JavaScript. The image cannot be visible to the user on the page. How can I g

8条回答
  •  囚心锁ツ
    2020-11-29 20:02

    naturalWidth and naturalHeight

    var img = document.createElement("img");
    img.onload = function (event)
    {
        console.log("natural:", img.naturalWidth, img.naturalHeight);
        console.log("width,height:", img.width, img.height);
        console.log("offsetW,offsetH:", img.offsetWidth, img.offsetHeight);
    }
    img.src = "image.jpg";
    document.body.appendChild(img);
    
    // css for tests
    img { width:50%;height:50%; }
    

提交回复
热议问题