Getting width & height of an image with filereader

后端 未结 5 1340
小鲜肉
小鲜肉 2020-11-30 23:36

I am building an image resize/crop, and I\'d like to show a live preview after they\'ve edited it in a modal (bootstrap). This should work, I believe, but I just ge

5条回答
  •  执笔经年
    2020-12-01 00:20

    For me the solution of Austin didn't work, so I present the one worked for me:

    var reader = new FileReader;
    
    reader.onload = function() {
        var image = new Image();
    
        image.src = reader.result;
    
        image.onload = function() {
            alert(image.width);
        };
    
    };
    
    reader.readAsDataURL(this.files[0]);
    

    And if you find that assignment image.src = reader.result; takes place after image.onload a bit wired, I think so too.

提交回复
热议问题