如何获得自然图像的宽度和高度? [英] How to get Natural image width and height?
问题描述
大家好吧
我正在寻找图片的自然尺寸
我发现这个功能:
while((imgHeight = image.getHeight(this))== -1){
//循环直到图像加载
>
}
while((imgWidth = image.getWidth(this))== -1){
//循环直到图像加载
}
但是firefox错误控制台写了getWidth不是一个有效的函数
请你告诉我代码是否有问题,或者如何用其他方法获得图像的宽度和高度?
你非常感谢
Hi at all
I am looking for the natural size of an image
I found this function :
while ((imgHeight = image.getHeight(this)) == -1 ) {
// loop until image loaded
}
while ((imgWidth = image.getWidth(this)) == -1 ) {
// loop until image loaded
}
But firefox errors console write that getWidth is not a valid function
Pleae can you tell me if there is a wrong into my code or how can I get the
width and height of an images with others methods?
Thenk you very much
推荐答案
" Ben" < be*@nospam.itwrote in message
news:Tz ******************** @ twister2.libero.it ...
"Ben" <be*@nospam.itwrote in message
news:Tz********************@twister2.libero.it...
大家好
我正在寻找图片的自然尺寸
Hi at all
I am looking for the natural size of an image
你是否尝试在图像上添加一些onload处理程序?
让我们说变量图像存储引用你的dom图像节点。试试这个:
image.onload = function(){
imgHeight = this.height;
imgWidth = this.width ;
}
现在只是从头开始写,但是这样的事情应该有用......
欢呼
did you try to append some onload handler to the image?
let''s say variable image stores reference to your dom image node. try this:
image.onload = function() {
imgHeight = this.height;
imgWidth = this.width;
}
just writing from head now, but something like this should work...
cheers
Benaécrit:
Ben a écrit :
>
但firefox错误控制台写道getWidth不是有效函数
>
But firefox errors console write that getWidth is not a valid function
你编码了吗?
或你使用原型的库吗?
< html>
< style type =" text / css">
img {width:100px; }
< / style>
< script type =" text / javascript">
function getSize(imgId){
var i = document.getElementById(imgId);
var siz = function(what){
what.widss = what.naturalWidth? what.naturalWidth:what.width;
what.heigt = what.naturalHeight? what.naturalHeight:what.height;
返回什么;
}
i = siz(i);
if(!i.widss)setTimeout(function(){getSize(imgId)},200);
else
return [i.widss,i.heigt];
}
window.onload = function(){
alert(getSize(''V_2'')。join('''' ));
}
< / script>
以100px宽度显示的所有图像:
< img src =" http://i11.ebayimg.com/01/i/000/ed/13/575b_1.JPG" ALT ="" id =" V_1"
onload =" alert(this.width +''''+ this.height);">
< img src = " HTTP://i16.ebayimg.com/01/i/000/ed/13/562b_1.JPG" ALT ="" id =" V_2">
< img src =" http://i3.ebayimg.com/03/i/000/e2/fe/aa08_1.JPG" ALT ="" id =" V_3">
图片尺寸#:
< select
onclick =" alert(getSize(''' V _''+(+ this.selectedIndex + 1))。join(''''))">
< option> 1
< option> ; 2
< option> 3
< / select>
< / html>
-
sm
did you code it ?
or do you use prototype''s library ?
<html>
<style type="text/css">
img { width: 100px; }
</style>
<script type="text/javascript">
function getSize(imgId) {
var i = document.getElementById(imgId);
var siz = function(what) {
what.widss = what.naturalWidth? what.naturalWidth : what.width;
what.heigt = what.naturalHeight? what.naturalHeight : what.height;
return what;
}
i = siz(i);
if(!i.widss) setTimeout(function(){getSize(imgId)},200);
else
return [i.widss, i.heigt];
}
window.onload = function() {
alert(getSize(''V_2'').join('' ''));
}
</script>
All images displayed in 100px width :
<img src="http://i11.ebayimg.com/01/i/000/ed/13/575b_1.JPG" alt="" id="V_1"
onload="alert(this.width+'' ''+this.height);">
<img src="http://i16.ebayimg.com/01/i/000/ed/13/562b_1.JPG" alt="" id="V_2">
<img src="http://i3.ebayimg.com/03/i/000/e2/fe/aa08_1.JPG" alt="" id="V_3">
size of image #:
<select
onclick="alert(getSize(''V_''+(+this.selectedIndex+1 )).join('' ''))">
<option>1
<option>2
<option>3
</select>
</html>
--
sm
dino @ wrkaécrit:
dino @ wrk a écrit :
>
让我们说变量图像存储对dom图像节点的引用。试试这个:
image.onload = function(){
imgHeight = this.height;
imgWidth = this.width ;
}
现在只是从头开始写,但是这样的东西应该有效......
>
let''s say variable image stores reference to your dom image node. try this:
image.onload = function() {
imgHeight = this.height;
imgWidth = this.width;
}
just writing from head now, but something like this should work...
如果图像大小由CSS调整,你会得到CSS的大小
更喜欢尝试:naturalWidth naturalHeight
(测试之后)属性已知)
-
sm
if image was sized by CSS you''ll get the CSS''s size
prefer to try : naturalWidth naturalHeight
(after testing this attributes are known)
--
sm
这篇关于如何获得自然图像的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!