javascript - HTML5 - how to get image dimension -


i have script, it's used fetch width , height of browser uploaded image.

reference: http://renevier.net/misc/resizeimg.html

function createreader(file) {     reader.onload = function(evt) {         var image = new image();         image.onload = function(evt) {             var width = this.width;             var height = this.height;             alert (width); // produce 198         };         image.src = evt.target.result;      };     reader.readasdataurl(file); }  (var = 0, length = input.files.length; < length; i++) {     createreader(input.files[i]); } 

i want access value width , height outside createreader function. how can that?

change "createreader" pass in handler function called when image available:

function createreader(file, whenready) {     reader.onload = function(evt) {         var image = new image();         image.onload = function(evt) {             var width = this.width;             var height = this.height;             if (whenready) whenready(width, height);         };         image.src = evt.target.result;      };     reader.readasdataurl(file); } 

now when call it, can pass in function whatever want done image dimensions:

  createreader(input.files[i], function(w, h) {     alert("hi width " + w + " , height " + h);   }); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -