whirlpool6 Posted August 21, 2007 Share Posted August 21, 2007 hello everyone, i am using javascript and php in my web application. before i display images, i would like to get their width and height. i used the getimagesize() function of php, but there are time when the width and height returned are blanks. i dont know how and i dont know why but there are really times like this. because of this, i started to search for a workaround when this scenario happens. i placed an onload function in my img tag. in that function i used this.width and this.height to get the size. but then, they returned zeroes. i dont know what to do now. can anybody please help me? how do i get the image width and height using either php or javascript? thanks a lot. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted August 21, 2007 Share Posted August 21, 2007 the images need to be in the dom for js to access them if they are, use offsetHeight and offseWidth example var x = document.getElementsByTagName('img'); var c = x.length; for(i = 0; i < c; i++){ alert(x.src+' '+x.offsetHeight); } Quote Link to comment Share on other sites More sharing options...
whirlpool6 Posted August 22, 2007 Author Share Posted August 22, 2007 hello guys, i am really not interested in getting the width and height of the image per se. but im interested in getting their height and width on or after they load. i have to do this to display the image correctly. if the image width is bigger than that of the height, the image is to be displayed with a width of 100. else, its height will be 90. so this has to be done before the image is displayed. i have placed a function to get the width and height on the onload of the image but unfortunately, they return zero. why is this so guys? thanks a lot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.