473,385 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Extract Width and Height of image

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[i].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 <imgto <a>
a.appendChild(img);

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

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

Apr 30 '07 #1
4 4035
SM wrote:
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>

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 1 '07 #2
On May 1, 1:12 am, -Lost <maventheextrawo...@techie.comwrote:
Knowing the filepath of the image, is it possible to extract the width
and height? If yes, how...

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

May 1 '07 #3
scripts.contact wrote:
On May 1, 1:12 am, -Lost <maventheextrawo...@techie.comwrote:
>>Knowing the filepath of the image, is it possible to extract the width
and height? If yes, how...
No.

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.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 1 '07 #4
SM
On May 1, 3:24 am, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On May 1, 1:12 am, -Lost <maventheextrawo...@techie.comwrote:
Knowing the filepath of the image, is it possible to extract the width
and height? If yes, how...
No.

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

May 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex Shi | last post by:
In php, is there a way obtain the width of a charactor of a certain font? Alex -- ================================================== Cell Phone Batteries at 30-50%+ off retail prices!...
0
by: SteveJ | last post by:
All, Can someone help me solve the next step. First of all let me say I'm new to php. I pieced the following code together from samples I found on the net and a book I bought called PHP...
2
by: Stephen Weatherly | last post by:
Could anyone please help me with a problem I am having with my table widths??? If I have 2 images within a td tag, but using CSS relative positioning I position one over the top of the second (I...
11
by: Norman L. DeForest | last post by:
Am I misunderstanding the CSS specifications or is Firefox (version 1.0.6) (and Opera) doing the wrong thing? It appears that Firefox 1.0.6 includes the border in width calculations for tables...
2
by: Robson Carvalho Machado | last post by:
Dear friends, I'm dynamically creating a Hyperlink with spacer.gif as ImageURL that is an 1px transparent image only to determine link position, but as I create this link dynamically I could not...
7
by: Neo Geshel | last post by:
Greetings. I have managed to stitch together an awesome method of posting text along with an image to a database, in a way that allows an unlimited number of previews to ensure that text and...
1
by: comp.lang.php | last post by:
Sorry folks, I'm on a roll!!! I need to calculate the height or width of an image if you change either the width or height and don't change BOTH of them at the same time, this is to "vector" the...
8
by: Kentor | last post by:
Hello, I have users that submit images to my website. By default I choose to make the width bigger than the height because usually pictures are taken horizontally... But in some cases people take...
1
epots9
by: epots9 | last post by:
I have a image inside of a div <div id="image"> <div id="loader"> <img id="loaderImage" src="assets/loader.gif" alt="loading..." /> </div> <div id="loaded"> <img id="picture" src="" alt=""...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.