简体   繁体   English

无法在javascript中获取图像的高度和宽度

[英]Can't get height and width of image in javascript

I am trying to retreive the actual height and width of an image through javascript. 我试图通过javascript来检索图像的实际高度和宽度。 The image source being used in my case is a BLOB which gets generated as soon as a user inserts a file. 在我的案例中使用的图像源是BLOB,一旦用户插入文件就会生成BLOB。

Heres my code: 继承我的代码:

    var img = document.createElement('img');
    var blob = URL.createObjectURL(e.target.files[0]);
    img.src = blob;
    console.log(img);
    var w = img.width;
    var h = img.height;
    console.log("NEW IMAGE width", w);
    console.log("NEW IMAGE height: ", h);

Here are the logs: 这是日志:

<img src=​"blob:​http%3A/​/​localhost%3A3000/​af3e5a35-8c1c-40b5-ad5a-379c3ab1ec1d">​
NEW IMAGE width 0
NEW IMAGE height:  0

the blob is correct since I can view the image in my browser. blob是正确的,因为我可以在浏览器中查看图像。

When I create another image in my console with the same blob and try to retreive the height and width everything works just fine. 当我在我的控制台中使用相同的blob创建另一个图像并尝试检索高度和宽度时,一切正常。

But when I try to run it like that in my onChange event form the input i just get 0 for height and width. 但是当我尝试在我的onChange事件表单中运行它时,输入我只得到0的高度和宽度。

you should get size after image loaded: 你应该在图像加载后获得大小:

    img.onload = getSize;//measure after loading or you will get 0

例子和结果

Need to take the width and height after the image got loaded. 图像加载后需要采取宽度和高度。 So need to give some time to load the image.Else you will get zero size always.Take the width/height inside onload event. 因此需要花一些时间来加载图像。这样你总是会得到零大小。在onload事件中取宽度/高度。

var img = document.createElement('img');
    var blob = URL.createObjectURL(e.target.files[0]);
    img.src = blob;
    img.onload = function() {
      var w = img.width;
      var h = img.height;
      console.log("NEW IMAGE width", w);
      console.log("NEW IMAGE height: ", h);
    }

Try loading it first: 尝试先加载它:

img.onload(function() {
    var w = img.width;
    var h = img.height;

    console.log("NEW IMAGE width", w);
    console.log("NEW IMAGE height: ", h);
});

How to get height and width of the image in blob objeact? 如何在blob objeact中获取图像的高度和宽度? example: 例:

 $(function(){ $("input[name=file_name]").on('change',function(){ var url = window.URL || window.webkitURL || window.mozURL; var src = url.createObjectURL($(this)[0].files[0]); $("#box").html('<img src="'+src+'">'); $("#box img").attr('width','100'); $('img')[0].onload = function() { console.log($(this).height()); //get image height by jQuery console.log($(this)[0].height)// get image height by javascript console.log($(this)[0].naturalHeight);//get real height by javascript /** * if you want to get the image's width,you can use following code : * $(this).width(); * $(this)[0].width; * $(this)[0].naturalWidth; */ }; }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form method="post" enctype="multipart/form-data"> <input type="file" name="file_name"> </form> <div id="box"></div> 

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

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