Extract Width and Height of image

S

SM

Hello,
I've created a Unordered list with images using the Javascript DOM,
and i want to set up the width and height attributes of the image...
The problem is that most images vary in size.
Here's my question:
Knowing the filepath of the image, is it possible to extract the width
and height? If yes, how...

Thanks
Marco

....
var ul = document.createElement('ul');

for(var i=0; i<lstThumbnail.length; i++)
{
var path = lstThumbnail.path;

var li = document.createElement('li');
var a = document.createElement('a');

//assign 'href' attribute to element a
a.setAttribute('href', '#');

//create img element, assign the attributes: src, alt, width,
height
var img = document.createElement('img');
img.setAttribute('src', path);
img.setAttribute('alt', noimageTxt);

img.setAttribute('width', ???); <--- EXTRACT WIDTH AUTOMATIC
img.setAttribute('height', ???); <--- EXTRACT HEIGHT AUTOMATIC

//add <img> to <a>
a.appendChild(img);

//add <a> <li>
li.appendChild(a);

//add <li> to <ul>
ul.appendChild(li);
}
....
 
L

-Lost

SM said:
Hello,
I've created a Unordered list with images using the Javascript DOM,
and i want to set up the width and height attributes of the image...
The problem is that most images vary in size.
Here's my question:
Knowing the filepath of the image, is it possible to extract the width
and height? If yes, how...

No.

<snip>
 
L

-Lost

scripts.contact said:
var img=new Image();
img.src="filepath of image"
img.onload=function(){ alert('Width: '+img.width+', Height:
'+img.height) }

Oops! :\

I have no clue what I was thinking.

Hrmm... maybe I was thinking of reading document.images[0].width or
..height before the image loaded?

No, I am just inebriated.
 
S

SM

var img=new Image();
img.src="filepath of image"
img.onload=function(){ alert('Width: '+img.width+', Height:
'+img.height) }

So it is posible, cool !
I will try it!
I was also looking for a preload function and i believe your answer
does both... preload and size retrieval

Thanks
Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,589
Members
45,100
Latest member
MelodeeFaj
Top