1,105,332 Community Members

Finding Width and Height of an image before uploading

Member Avatar
divyakrishnan
Posting Whiz in Training
243 posts since May 2010
Reputation Points: 20 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 4 [?]
 
0
 

I want to get the width and height of an image before uploading using javascript.
I used The code shown below.
But its returning 0 value

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function form_image()
{   

var v=new Image();
v.src=document.getElementById("f1").getAttribute('src');
alert(v.height+"-"+v.width);
}
</script>
</head>

<body>



<form method="post" enctype="multipart/form-data">
<input type="file" id="f1" name="f1" onchange="form_image()"/>
<input  type="submit" />
</form>
</body>
</html>
Member Avatar
pritaeas
mod_pritaeas
11,287 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,832 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

I think you need something like this:

var uploadedFile = document.getElementById('f1');
var fileHeight = uploadedFile.files[0].height;
Member Avatar
divyakrishnan
Posting Whiz in Training
243 posts since May 2010
Reputation Points: 20 [?]
Q&As Helped to Solve: 24 [?]
Skill Endorsements: 4 [?]
 
0
 

I tried following code

function form_image1()
{   

    var uploadedFile = document.getElementById('f1');
    var fileHeight = uploadedFile.files[0].height;
    alert("fileHeight="+fileHeight);
}

Its getting output as "undefined "

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article