javascript - 如何从 URL 显示图像分辨率

标签 javascript html image

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

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

用 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>

关于javascript - 如何从 URL 显示图像分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26385770/

相关文章:

javascript - 如何在纯js中克隆图像

javascript - 纯 Javascript 图像处理库(二进制形式,不通过 DOM)

javascript - 显示 JavaScript 数组值

Html 元关键字应该包含逗号吗?

c# - 通过TCP套接字将图像从C#服务器发送到Java客户端

javascript - 基于复选框选择jquery更新文本框

html - Firefox 位置 :absolute and width: 100% on display:table-cell weird behaviour

php - 滚动条出现在我的网站内

javascript - jQuery Toggle (Expanding Div) 与文本的不一致过渡

javascript - Grails 下 AngularJS 应用程序中部分 html 的正确路径是什么