js 获取上传图片的宽度和高度的方法


 html代码:
  
       id='inputFile${index$}'
       name="uploadFile0"                                                        
    accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"
       style="display:none"                                                        
       onchange="getMapPictureSize(this, ${index$})"
       multiple/>


js代码:

function getMapPictureSize(that, index) {
    var curFile = that.files;

    var reader = new FileReader;
    reader.onload = function (evt) {
        //加载图片获取图片真实宽度和高度
        var image = new Image();
        image.onload = function () {
            $('#composeWidth_' + index).attr("value", this.width);
            $('#composeHeight_' + index).attr("value", this.height);
        };
        image.src = evt.target.result;
    };
    //先转换为图片之后,再获取宽度和高度
    reader.readAsDataURL(curFile[0]);
}

相关