如何从其URL向显示图像分辨率

How to Show Image Resolution to from its URL

本文关键字:显示 显示图 图像 分辨率 URL      更新时间:2023-09-26

我正在创建一个网站来共享可以下载并在用户桌面上使用的壁纸。但我无法找到一种方式来向我的访客展示它的决心。是的,我可以自己写分辨率,但问题是我共享的图像太多,我不可能为每个图像写分辨率。

我可以自动创建描述,但我想显示图像分辨率。

使用JavaScript有可能实现这一点吗?如果它在加载图像后显示结果,则没有问题。

您可以在JavaScript中将其创建为img元素,而无需在页面上呈现:

var img = document.createElement('img');
img.src = "path_to_src";

现在您的图像已经创建,您可以使用img.heightimg.width来提取高度和宽度,这是在图像加载后获得的:

var x = document.createElement('img');
x.src = "http://placehold.it/128x256";
x.onload = function() {
  var html = "";
  
  html += "Height = " + x.height + "px<br>";
  html += "Width = " + x.width + "px";
  
  document.querySelector('p').innerHTML = html;
}
<p>
    <!-- Image height and width will be displayed here instead of the below text. -->
    Creating image and pulling its height and width...
</p>