首页 > 技术文章 > 用jquery来获得上传图片的尺寸

xiaohaifengke 2016-10-28 18:01 原文

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用jquery来获得上传图片的尺寸的</title>
</head>
<body>
    <input type="file" id="image_file" multiple>
    <script src="./lib/jquery-1.8.0.min.js"></script>
    <script>
        //获取input图片宽高和大小
        function getImageWidthAndHeight(id, callback) {
          var _URL = window.URL || window.webkitURL;
          $("#" + id).change(function (e) {
            var file, img;
            if ((file = this.files[0])) {
              img = new Image();
              img.onload = function () {
                callback && callback({"width": this.width, "height": this.height, "filesize": file.size});
              };
              img.src = _URL.createObjectURL(file);
            }
          });
        }
        
        getImageWidthAndHeight('image_file', function (obj) {
           console.log('width:'+obj.width+'-----height:'+obj.height);
          });
    </script>
    <script></script>
</body>
</html>

 

推荐阅读