Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  137] [ 6]  / answers: 1 / hits: 23215  / 9 Years ago, tue, january 19, 2016, 12:00:00

I am trying to retreive the actual height and width of an image through javascript.
The image source being used in my case is a BLOB which gets generated as soon as a user inserts a file.



Heres my code:



    var img = document.createElement('img');
var blob = URL.createObjectURL(e.target.files[0]);
img.src = blob;
console.log(img);
var w = img.width;
var h = img.height;
console.log(NEW IMAGE width, w);
console.log(NEW IMAGE height: , h);


Here are the logs:



<img src=​blob:​http%3A/​/​localhost%3A3000/​af3e5a35-8c1c-40b5-ad5a-379c3ab1ec1d>​
NEW IMAGE width 0
NEW IMAGE height: 0


the blob is correct since I can view the image in my browser.



When I create another image in my console with the same blob and try to retreive the height and width everything works just fine.



But when I try to run it like that in my onChange event form the input i just get 0 for height and width.


More From » javascript

 Answers
6

you should get size after image loaded:



    img.onload = getSize;//measure after loading or you will get 0


example


[#63658] Sunday, January 17, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;