473,406 Members | 2,954 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,406 software developers and data experts.

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.
May 21 '07 #1
11 39485
dmjpro
2,476 2GB
u can check it by it's style property or by offsetHeight and offsetWidth.

kind regards.
dmjpro.
May 21 '07 #2
acoder
16,027 Expert Mod 8TB
Using the height and width properties. See link.

If you get stuck, post some code.
May 21 '07 #3
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.
May 22 '07 #4
dmjpro
2,476 2GB
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.
May 22 '07 #5
acoder
16,027 Expert Mod 8TB
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.
May 22 '07 #6
dmjpro
2,476 2GB
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.
May 22 '07 #7
Thanks for your reply.

Now i got the solution by executing the following code

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function imgdisp()
  3. {
  4. document.getElementById("img1").src=document.getElementById_("File1").value;
  5. }
  6. function disp1()
  7. {
  8. document.getElementById("img1").style.width="";
  9. document.getElementById("img1").style.width="";
  10.         var newImg = new Image();
  11.         newImg.src = document.getElementById("File1").value;
  12.         var height = newImg.height;
  13.         var width = newImg.width;
  14.         if (width>500)
  15.         {
  16.         document.getElementById("img1").style.width=500;
  17.         }
  18.         }
  19.         </script>
  20.  
In function imgdisp(), i am assigning image from client to html image tool.

Expand|Select|Wrap|Line Numbers
  1. <INPUT onpropertychange="imgdisp()" style="Z-INDEX: 101; LEFT: 160px; POSITION: absolute; TOP: 104px"
  2.                 type="file" id="File1" runat="server"> 
  3.  
Then i am calling function disp1() in html image tool as mentioned below

Expand|Select|Wrap|Line Numbers
  1.                 <IMG onresize="disp1()"  style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 160px"
  2.                 alt="" src="" id="img1">
May 23 '07 #8
use the below.

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT language="Javascript">
  2.      function funcName1() {
  3.  
  4.     imgFile.src = "../image/imageName1.gif"
  5.  
  6.     var getHeight = imgFile.height;
  7.     var getWidth = imgFile.width;
  8.  
  9.     alert(getHeight);
  10.     alert(getWidth);
  11. }
  12. </SCRIPT>
  13.  
Thats it.

Regards
micJ
Sep 13 '07 #9
acoder
16,027 Expert Mod 8TB
Welcome to TSDN!

Please remember to use code tags when posting code
[CODE=javascript]like this...[/code]

Your solution is for an already loaded file, not a file to be uploaded from the client-side.
Sep 14 '07 #10
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.

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript1.2">
  2.  
  3. var imgHeight;
  4. var imgWidth;
  5.  
  6. function findHHandWW() {
  7. imgHeight = this.width;imgWidth = this.width;return true;
  8. }
  9.  
  10. function showImage(imgPath) {
  11. var myImage = new Image();
  12. myImage.name = imgPath;
  13. myImage.onload = findHHandWW;
  14. myImage.src = imgPath;
  15. }
  16. </script>
  17.  

Thanks,

Binod Suman
Jun 4 '09 #11
acoder
16,027 Expert Mod 8TB
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.
Jun 4 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: ok | last post by:
Hello, Q: How do I get image width and height before uploading an image? This because, I want to restrict people uploading huge files. Thanks in advance
7
by: Premshree Pillai | last post by:
Hello, Is there a Py module available using which I can find the width and height of any image format? -Premshree ===== -Premshree http://www.qiksearch.com/]
4
by: Marek Mänd | last post by:
Is there any tutorials available how to create a png data totally on the client side using javascript and then show that image?
1
by: emilmgeorge | last post by:
Pls somebody give me the code for getting the height and width of a image in pixels in javascript or asp. Thanks - Emil
2
by: Atul | last post by:
I am unable to find image height and width in mozilla firefox. My code is working in IE but not in Mozilla. How can i find image width and height in mozilla? function check(sel) { if(sel != "")...
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: rameshmandapati | last post by:
hi i have a problem with Excel. iam adding data to a cell in Excel using java script var excel = new ActiveXObject ("Excel.Application"); but i want image also i search in google i...
10
by: pallavsheth | last post by:
Hi Team, Can i find the logged in user using javascript? can someone please share the code for the same?
5
Mike Kypriotis
by: Mike Kypriotis | last post by:
in php with getimagesize() for both img and swf worked well,in javascript? PS I do not want to first load it and then use its id as a reference object, I want to know because I need to know in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.