简体   繁体   English

如何在jquery中获取图像高度宽度

[英]How to get image height width in jquery

I have a form where I uploads the image, in the jQuery function; 我有一个表单,我在jQuery函数中上传图像;

$('body').on('change', '#file', function () {

By using this.files[0] I am getting image name and size. 通过使用this.files[0]我得到图像名称和大小。 for this I am using. 为此,我正在使用。 this.files[0].size and this.files[0].name . this.files[0].size this.files[0].namethis.files[0].name But now I want to get height and width of image. 但是现在我想获取图像的高度和宽度。 How can I do that? 我怎样才能做到这一点?

I've got the solution by myself, it is as follows, 我自己有解决方案,如下所示,

var reader = new FileReader();
reader.onload = imageIsLoaded;
reader.readAsDataURL(this.files[0]);
 function imageIsLoaded(e) {
         var image = new Image();
         image.src = e.target.result;
         image.onload = function() {
              // access image size here 
              alert(this.width);
              alert(this.height);
         };
 };

as jquery dont know exact file type, so after Image type validation we can convert any image file to jQuery readable image, so jQuery can handle it as image. 因为jquery不知道确切的文件类型,所以在Image类型验证后我们可以将任何图像文件转换为jQuery可读图像,因此jQuery可以将其作为图像处理。 Hence for converting file to image we need reader. 因此,要将文件转换为图像,我们需要读者。

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

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