简体   繁体   English

在上传javascript期间获取图像宽度和高度

[英]getting image width and height during upload javascript

Im having trouble obtaining the width and height of an image during the upload function. 我无法在上传功能期间获取图像的widthheight

Here's the current code I have: 这是我现有的代码:

// internal function that creates an input element
var file=newElement("input",0,"");

// sets the input element as type file
file.type="file";
file.multiple="multiple";
file.name="photoupload[]";
file.accept="image/*";

file.onchange=function(e){
  // internal function that creates an image element
  var img=newElement("img",0,"");
  img.src=URL.createObjectURL(this);
  img.onload=function(){
   var cw=img.clientWidth;
   var ch=img.clientHeight;
   alert(cw);alert(ch);
  }
}
file.click();

Not quite sure how to fix this. 不太确定如何解决这个问题。 Any help appreciated. 任何帮助赞赏。

Swap the onload to before the change of src and use width and height. 在更改src之前将onload交换为使用宽度和高度。

The onload needs to exist before the src is changed 在更改src之前,onload需要存在

I assume newElement is a function that does a document.createElement - if it does not return an image object, you need to move the onload to that function 我假设newElement是一个执行document.createElement的函数 - 如果它没有返回一个图像对象,你需要将onload移动到该函数

// internal function that creates an input element
var file=newElement("input",0,"");

// sets the input element as type file
file.type="file";
file.multiple="multiple";
file.name="photoupload[]";
file.accept="image/*";

file.onchange=function(e){
  // internal function that creates an image element
  var img=newElement("img",0,"");
  img.onload=function(){
   var cw=img.width;
   var ch=img.height;
   console.log(cw,ch);
  }
  img.src=URL.createObjectURL(this);
}
file.click();

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

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