繁体   English   中英

如何从此函数获取图像的宽度和高度?

[英]How to get width and height of image from this function?

我如何才能在下面的代码之外获取wh值。 我需要获取选择上传的每个图像的宽度和高度。

     var widt;
            var hit;
            reader.readAsDataURL(file);
            reader.onload = function (_file) {
                image.src = _file.target.result;              // url.createObjectURL(file);
                image.onload = function () {
                    var w = this.width,
                        h = this.height,
                        t = file.type,                           // ext only: // file.type.split('/')[1],
                        n = file.name
                    widt = w;
                    hit = h;
                };
            };
console.log(widt);

在这里console.log返回undefined

您的变量名是widt并且您正在控制台日志中使用width 尝试:

console.log(widt);

另外,将该控制台日志放入image.onload函数以获取正确的响应。

您在以下代码行的末尾错过了分号

  n = file.name
  1. “ console.log(widh);” 更改为“ console.log(widt);”
  2. 把“ console.log(widt);” 进入“ image.onload”功能。

码:

image.onload = function () {
  var w = this.width,
      h = this.height,
      t = file.type,                           
      n = file.name;
  widt = w;
  hit = h;
  console.log(widt);
};

暂无
暂无

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

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