How to find image width and height using javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PerumalSamy
    New Member
    • Feb 2007
    • 64

    How to find image width and height using javascript

    Hi, I am developing program using asp.net in which i am using javascript coding in particular part.

    I need to find height and width for loaded image file.

    How can i write coding for it.
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    u can check it by it's style property or by offsetHeight and offsetWidth.

    kind regards.
    dmjpro.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Using the height and width properties. See link.

      If you get stuck, post some code.

      Comment

      • PerumalSamy
        New Member
        • Feb 2007
        • 64

        #4
        I need to find height and width for uploaded images.

        Ex: if uploaded image file is C:\Documents and Settings\My Documents\My Pictures\globe_ img.gif, then i need to find the image height and width for the image globe_img.gif by assigning to local image variables.

        Any idea.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          it needs advanced but non-standard JS.
          that means from client side the file properties sholud be opened.
          and mind it that is possible using standard JS.
          in IE it is possible using ActivexObject.
          and it is not standard JS.


          kind regards.
          dmjpro.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by PerumalSamy
            I need to find height and width for uploaded images.

            Ex: if uploaded image file is C:\Documents and Settings\My Documents\My Pictures\globe_ img.gif, then i need to find the image height and width for the image globe_img.gif by assigning to local image variables.

            Any idea.
            I'm afraid this is not possible. I had an image preview script, but could only get the width and height of local images by performing a quick upload to a temporary directory and then reading the values from the uploaded image. IE6 allowed you to read the properties of local files. I'm not sure about IE7. Firefox and other browsers don't.

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              after uploading it is possible ... but he wants to validate the height and width from the client side.

              that's why i wrote this.

              kind regards.
              dmjpro.

              Comment

              • PerumalSamy
                New Member
                • Feb 2007
                • 64

                #8
                Thanks for your reply.

                Now i got the solution by executing the following code

                [CODE=javascript]<script language="javas cript">
                function imgdisp()
                {
                document.getEle mentById("img1" ).src=document. getElementById_ ("File1").value ;
                }
                function disp1()
                {
                document.getEle mentById("img1" ).style.width=" ";
                document.getEle mentById("img1" ).style.width=" ";
                var newImg = new Image();
                newImg.src = document.getEle mentById("File1 ").value;
                var height = newImg.height;
                var width = newImg.width;
                if (width>500)
                {
                document.getEle mentById("img1" ).style.width=5 00;
                }
                }
                </script>
                [/CODE]

                In function imgdisp(), i am assigning image from client to html image tool.

                Code:
                <INPUT onpropertychange="imgdisp()" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 104px"
                				type="file" id="File1" runat="server">
                Then i am calling function disp1() in html image tool as mentioned below

                Code:
                				<IMG onresize="disp1()"  style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 160px"
                				alt="" src="" id="img1">
                Last edited by Dormilich; Jun 4 '09, 07:18 PM. Reason: [HTML] tags are out of order now

                Comment

                • micjohnson
                  New Member
                  • Sep 2007
                  • 1

                  #9
                  use the below.

                  [CODE=javascript]<SCRIPT language="Javas cript">
                  function funcName1() {

                  imgFile.src = "../image/imageName1.gif"

                  var getHeight = imgFile.height;
                  var getWidth = imgFile.width;

                  alert(getHeight );
                  alert(getWidth) ;
                  }
                  </SCRIPT>
                  [/CODE]
                  Thats it.

                  Regards
                  micJ
                  Last edited by acoder; Sep 14 '07, 11:08 AM. Reason: Added code tags

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Welcome to TSDN!

                    Please remember to use code tags when posting code
                    &#91;CODE=javas cript]like this...[/CODE]

                    Your solution is for an already loaded file, not a file to be uploaded from the client-side.

                    Comment

                    • BinodSuman
                      New Member
                      • Jun 2009
                      • 1

                      #11
                      I was searching a solution to get height and widht of an image using javascript. I got many but all those solution only worked when image present in browser cache.

                      Finally I got one solution to get image height and width even image does not exist in browser cache. Just I want to share all of you.

                      Code:
                      <script type="text/javascript" language="javascript1.2">
                      
                      var imgHeight;
                      var imgWidth;
                      
                      function findHHandWW() {
                      imgHeight = this.width;imgWidth = this.width;return true;
                      }
                      
                      function showImage(imgPath) {
                      var myImage = new Image();
                      myImage.name = imgPath;
                      myImage.onload = findHHandWW;
                      myImage.src = imgPath;
                      }
                      </script>

                      Thanks,

                      Binod Suman
                      Last edited by acoder; Jun 4 '09, 08:07 PM.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        That's fine, but you could always let the image load in the background instead. If you need the dimensions straight away, you'd need the image to be loaded already, otherwise they'd be a delay unless it's a small image or a very fast connection.

                        Comment

                        Working...