【发布时间】:2011-08-03 17:54:14
【问题描述】:
我只有一个图片的 URL。我需要仅使用 JavaScript 来确定此图像的高度和宽度。图像在页面上对用户不可见。我怎样才能得到它的尺寸?
【问题讨论】:
-
实际上this answer 是唯一正确的,因为您必须等到图像加载完毕。
标签: javascript html image
我只有一个图片的 URL。我需要仅使用 JavaScript 来确定此图像的高度和宽度。图像在页面上对用户不可见。我怎样才能得到它的尺寸?
【问题讨论】:
标签: javascript html image
新建一个Image
var img = new Image();
设置src
img.src = your_src
获取width 和height
//img.width
//img.height
【讨论】:
以下代码为页面上的每个图像添加图像属性height和width。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function addImgAttributes()
{
for( i=0; i < document.images.length; i++)
{
width = document.images[i].width;
height = document.images[i].height;
window.document.images[i].setAttribute("width",width);
window.document.images[i].setAttribute("height",height);
}
}
</script>
</head>
<body onload="addImgAttributes();">
<img src="2_01.jpg"/>
<img src="2_01.jpg"/>
</body>
</html>
【讨论】:
var img = new Image();
img.onload = function(){
var height = img.height;
var width = img.width;
// code here to use the dimensions
}
img.src = url;
【讨论】:
onload之前比设置图片的url?
onload 是一个侦听器,如果图像加载正确,它将被异步调用。如果在设置 url 之后设置监听器,图像可能会在代码到达设置 onload 之前加载,导致监听器永远不会被调用。打个比方,如果你给自己端一杯水,你是先倒水,然后再把杯子装满吗?还是先放杯子再倒水?
在此处使用 JQuery 提出和回答的类似问题:
Get width height of remote image from url
function getMeta(url){
$("<img/>").attr("src", url).load(function(){
s = {w:this.width, h:this.height};
alert(s.w+' '+s.h);
});
}
getMeta("http://page.com/img.jpg");
【讨论】:
这会使用该函数并等待它完成。
http://jsfiddle.net/SN2t6/118/
function getMeta(url){
var r = $.Deferred();
$('<img/>').attr('src', url).load(function(){
var s = {w:this.width, h:this.height};
r.resolve(s)
});
return r;
}
getMeta("http://www.google.hr/images/srpr/logo3w.png").done(function(test){
alert(test.w + ' ' + test.h);
});
【讨论】:
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%; }
【讨论】:
使用 jQuery 获取图像大小
function getMeta(url){
$("<img/>",{
load : function(){
alert(this.width+' '+this.height);
},
src : url
});
}
使用 JavaScript 获取图像大小
function getMeta(url){
var img = new Image();
img.onload = function(){
alert( this.width+' '+ this.height );
};
img.src = url;
}
使用 JavaScript(现代浏览器、IE9+)获取图像大小
function getMeta(url){
var img = new Image();
img.addEventListener("load", function(){
alert( this.naturalWidth +' '+ this.naturalHeight );
});
img.src = url;
}
将上面的内容简单地用作:getMeta("http://example.com/img.jpg");
https://developer.mozilla.org/en/docs/Web/API/HTMLImageElement
【讨论】:
如果您有输入表单中的图像文件。 你可以这样使用
let images = new Image();
images.onload = () => {
console.log("Image Size", images.width, images.height)
}
images.onerror = () => result(true);
let fileReader = new FileReader();
fileReader.onload = () => images.src = fileReader.result;
fileReader.onerror = () => result(false);
if (fileTarget) {
fileReader.readAsDataURL(fileTarget);
}
【讨论】:
结合 Promise 和打字稿输入:
/**
* Returns image dimensions for specified URL.
*/
export const getImageDimensions = (url: string): Promise<{width: number, height: number}> => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve({
width: img.width,
height: img.height,
});
img.onerror = (error) => reject(error);
img.src = url;
});
};
用法:
try {
const {width, height} = await getImageDimensions(entry.NWS_IMAGE);
console.log(`Image dimensions: ${width}px x ${height}px`);
} catch (e) {
// Could not load image from specified URL
console.error(e);
}
【讨论】: