Extract Width and Height of image

Discussion in 'Javascript' started by SM, Apr 30, 2007.

  1. SM

    SM Guest

    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);
    }
    ....
    SM, Apr 30, 2007
    #1
    1. Advertising

  2. SM

    -Lost Guest

    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.
    -Lost, May 1, 2007
    #2
    1. Advertising

  3. On May 1, 1:12 am, -Lost <> wrote:
    > > 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) }
    scripts.contact, May 1, 2007
    #3
  4. SM

    -Lost Guest

    scripts.contact wrote:
    > On May 1, 1:12 am, -Lost <> wrote:
    >>> 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.
    -Lost, May 1, 2007
    #4
  5. SM

    SM Guest

    On May 1, 3:24 am, "scripts.contact" <>
    wrote:
    > On May 1, 1:12 am, -Lost <> wrote:
    >
    > > > 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
    SM, May 2, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo Geshel
    Replies:
    0
    Views:
    869
    Neo Geshel
    Feb 19, 2006
  2. Sean Jorden
    Replies:
    1
    Views:
    586
    laurie
    Aug 19, 2003
  3. Lois
    Replies:
    1
    Views:
    3,194
    Ryan Stewart
    Dec 27, 2004
  4. cera
    Replies:
    1
    Views:
    1,248
    Neredbojias
    Aug 18, 2007
  5. Bernd Meier
    Replies:
    6
    Views:
    502
    GTalbot
    Jan 14, 2008
Loading...

Share This Page