简体   繁体   中英

Getting width and height from a picture using Javascript

如何使用 Javascript 从存储在目录中的图片获取宽度和高度?

From a web browser:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Image Dimensions</title>
</head>

<body>
  <img id="myImage" src="image.png">

  <script>
  var img = document.getElementById("myImage");
  img.onload = function (event) {
    console.log(`natural: ${img.naturalWidth}, ${img.naturalHeight}`);
    console.log(`width,height: ${img.width}, ${img.height}`);
    console.log(`offset: ${img.offsetWidth}, ${img.offsetHeight}`);
  }
  </script>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM