Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: help with image size

  1. #1
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with image size

    well i need to know a remote image size :/ and the image is <img src="imgurl" width="40" height="40" /> so wheni do img.width i dont get the real width.
    i tried with ajax and php but i have problems with remote images...
    so if i can make this with javascript better

    thanks in advanced

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <html>
    <head>
    <script>
    onload=function() {
    var obj = document.getElementById("img")
    alert("Width: " + obj.width + "\nHeight: " + obj.height)
    }
    </script>
    </head>
    <body>
    <img src="imgurl" id="img" width="40" height="40" />
    </body>
    </html>
    - Mike

  3. #3
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    that returns 40x40

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    isn't that what you want?
    - Mike

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    mburt: No, he wants the *original* size of the image.
    fedekiller: There are two ways we can go about this: getting the size from the image file, or creating an invisible image, letting it resize itself, then getting its calculated size. The former requires a server-side script; the latter is much trickier. Which is it to be?

    If the former, you should probably post what you put together to do this originally.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well, this seems pretty simple and works in all except IE when launched for a local page (the local activeX 'protection bar' appears to interfere). However, it will work in Opera 9, FF 1.5.0.5, local and live, and IE 6 run live or after the activeX warning has been cleared for the session. I tested all three browsers with a fresh page load (navigating from Google) after clearing the cache:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function getDim(id){
    var im=document.getElementById(id);
    var newIm=new Image();
    newIm.src=im.src;
    alert('width='+newIm.width+'\nheight='+newIm.height);
    }
    </script>
    </head>
    <body onload="getDim('theImg')">
    <img id="theImg" src="http://home.comcast.net/~jscheuer1/side/1st.jpg" width="300" height="200">
    </body>
    </html>
    The image is actually 1500x1000 and that gets reported faithfully in all three browsers as mentioned above.
    Last edited by jscheuer1; 07-30-2006 at 04:54 AM. Reason: word usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks! ^^ i will test it

    edit: it works ! :P thanks! :3
    Last edited by fedekiller; 07-30-2006 at 04:29 AM.

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Aaah! I was thinking of creating an <img> element with visibility:hidden. Of course that's a much easier way.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Aside from all the details and everything, but why would you want to know the original size of an image? Just remove the width and height elements in the tag, and then right click the image, and hit properties, and there is your dimensions. It just seems very weird. But then again, there's usually a reason behind stuff like this..
    - Mike

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Twey,

    That was somewhat my original thinking and interestingly enough, if you use just:

    Code:
    var newIm=document.createElement('img');
    in place of:

    Code:
    var newIm=new Image();
    it still works but, once I saw that, I figured, "Why not try the image object?"

    Now, you mention this is 'easy' in PHP, I'm wondering how that goes, will it work before the page is parsed? I found a way to do this in ASP before the page is parsed but, it wasn't very simple. At least the example I found that worked wasn't very simple.

    mburt,

    The OP probably wants these dimensions for use with other code. You cannot be editing the page, clicking images and writing javascript each time this situation is encountered.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •