Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Image Size in FireFox

Status
Not open for further replies.

choioilachoi

Programmer
Feb 22, 2008
5
0
0
AU
Hi all,

I'm getting image size in FF but I couldn't.

image = new Image();
image.src = 'location';

then image.width and image.height just return 0

It's working with IE but FF. I have already searched but can't find any solution. Please help me, thanks very much :)
 
Hi

You need to do an onload on the image to get the width & height.

This doesn't work for locally loaded images.

var image=new Image();
image.src="image.onload=function() {
alert('W:'+image.width+', H:'+image.height)
}

Thanks
Smoogan
 
No, I'm checking the width and height of local file... So I can't use ur way?
 
<html>
<body>
<input type="file" id ="imageid" />
<input type="button" onClick="check()" value="check button"/>
</body>
</html>

<script language="javascript" type="text/javascript">
<!--

function hello() {
alert('hello');
}

function check() {
image2 = new Image();

image2.onload = function() {
hello()
}

image2.src = document.getElementById("imageid").value.replace(/^\s*|\s*$/g,"");




}
-->
</script>

Can you please try above source code? In FF, nothing happened when I clicked "check" button.
 
Why do you have the script outside the HTML tags?

Your image.onload makes no sense at all, since the hello() function can't be called until after the page is loaded.

Maybe you should give us an idea what exactly you're trying to do (real life, not made up examples of your code) and we can help more.

Lee
 
I wanna check the image size before uploading to the server. Here is my code until now

<html>
<body>
<input type="file" id ="imageid" />
<input type="button" onClick="check()" value="check button"/>
</body>
</html>

<script language="javascript" type="text/javascript">
<!--

function check() {
image2 = new Image();
var temp = "file://" + document.getElementById("imageid").value.replace(/^\s*|\s*$/g,"");
image2.src = temp;

alert(image2.width);



}
-->
</script>

It's working with IE, FF, Safari. However, it's just working in local. when I uploaded it into the real server, it didn't work...
 
What are you using to save the file to the server? You need either ASP.NET, PHP, Coldfusion, or an add-in for classic ASP to do that.

What code do you have to display the image on the server on a web page?

Lee
 
Hi trollacioud,

Is it important? I just want to check the image size by javascript before uploading to server.. I dont want to use server side to check the image size, I want to use JS...

Thanks,
 
Maybe image is not completed? Try this:
Code:
image2.src = temp;
alert(image2.width);
alert(image2.complete);
alert(image2.width);


 
If local image file is an important ingredient to the page, you can do this.
[tt]
<script language="javascript" type="text/javascript">
var image = new Image();
image.src = 'location'; //location is figurative here
if (/msie/i.test(window.navigator.userAgent) && /file:\/\//.test(image.src)) {
window.onload=handler;
} else {
image.onload=handler;
}
function handler() {
alert('W:'+image.width+', H:'+image.height)
}
</script>
[/tt]
If window.onload is already set up somewhere, put the functional line calling handler() into it if the condition(s) are met.

 
You wouldn't use "file://" in the path on the server's page, but the relative path of the image. I wondered if the image actually exists on the server, and how you were getting it there since you're using a file input on the local page.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top