jquery - How do i get the image dimensions (height, width) from a file by using javascript -


i being very stupid. cant work life. trying set imgwidth same width image width of file (pic_real_width).

function calculate() {     var pic_real_width, pic_real_height;     $("<img/>").attr("src", $("#pp_photo_popup")[0].src).load(function() {         pic_real_width = this.width;   // note: $(this).width() not         pic_real_height = this.height; // work in memory images.     });     var imgwidth =  } 

all in trying make function real dimensions of file on hard disk.

bind load event first, , processing needs image loaded inside of load event.

function calculate(callback) {     $("<img/>").load(function() {         var pic_real_width = this.width;   // note: $(this).width() not         var pic_real_height = this.height; // work in memory images.         callback(pic_real_width,pic_real_height);     }).attr("src", $("#pp_photo_popup")[0].src); }  calculate(function(width,height) {     console.log(width,height); }); 

if set src first, versions of ie trigger load event synchronously, triggering before have bound it. that's why swapped order. additionally, added callback parameter because assume later in function going return width/height, won't work due asynchronous nature of load events.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -