简体   繁体   English

获取图像尺寸(高度和宽度)Js 或 jquery

[英]Get Image Dimensions (Height and Width) Js or jquery

I have searched for this, but every time I implement the code it return 24 as height and width as 237 when I upload 400X400 image.我已经搜索过这个,但是每次我实现代码时,当我上传400X400图像时,它返回 24 作为高度和宽度为 237。

$(document).on('change','#tt' , function(){
    var img = document.getElementById('tt');
    var nheight = img.clientHeight;
    var nwidth = img.clientWidth;
    alert(nheight)
    alert(nwidth)
});
<input type="file" id="tt" name="tt" >

Is anyone know how can I get 400 as height and 400 as width in alert box when I upload 400X400 image.有谁知道当我上传400X400图像时,如何在警报框中获得 400 作为高度和 400 作为宽度。 Any help is really much appreciated.....非常感谢任何帮助......

Onchange event of file upload creating a Image object and attach uploaded file object as src to image object and then onload event of image object find height and width of image.文件上传的 Onchange 事件创建一个 Image 对象并将上传的文件对象作为 src 附加到图像对象,然后图像对象的 onload 事件找到图像的高度和宽度。

Please check below snippet for more understanding.请查看以下代码段以获取更多理解。

 var _URL = window.URL || window.webkitURL; $(document).on('change','#tt' , function(){ var file, img; if ((file = this.files[0])) { img = new Image(); img.onload = function () { alert("width : "+this.width + " and height : " + this.height); }; img.src = _URL.createObjectURL(file); } });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" id="tt" name="tt" >

var nheight = img.clientHeight;
var nwidth = img.clientWidth;

will just get you the dimension.只会让你得到维度。

Refer this -参考这个 -

Try尝试

var x = document.getElementById("myImg").naturalWidth;

the naturalHeight property to return the original height of an image, or the height property to set or return the value of the height attribute of an image naturalHeight 属性返回图像的原始高度,或 height 属性设置或返回图像的高度属性的值

http://www.w3schools.com/jsref/prop_img_naturalwidth.asp http://www.w3schools.com/jsref/prop_img_naturalwidth.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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