tags:

views:

34

answers:

3
A: 

I think this is something you should do with your server-side code. Have the page submit the form, the server can fetch it and check the size, and then the server should return an error if the image is too big.

Pointy
I'm already using Ajax to submit my forum, I tried thinking a way with PHP, but the PHP getimagesize() is for images being uploaded. I only want my users avatar to link to an external image for now, maybe later I will add the ability to upload your own image. Although if you can supply a PHP code that could check a remote file size, I would be happy to use it, I just thought javascript could come in more handy
Alex Cane
A: 

this kind of control has to be done on server-side. Javascript should not be used to get image size before form submitting.

If image is too big just discard the image and returns an error message to the user

Fabrizio Calderan
Then what PHP function could I use to check a remote file size?
Alex Cane
I don't know PHP, but a fairly good bet is to fetch the remote image, but it into a temporary file on the server, and use a function that gets the size of a local image.
Cameron
@Alex: http://it.php.net/manual/en/function.getimagesize.php
Fabrizio Calderan
Fabrizio, thank you, I just read more in depth after asking the question and now I'm sure it,s possible, I'll try it out. Thanks for all the answers!
Alex Cane
A: 

Just create a new Image object with that url and check its width and height attributes after the onload event.

var img = new Image();
img.onload = function(){
   alert(img.width);
};
img.src = "http://example.com/avatar.png";

There is no need for server-side with this, but you should note that users are able to override this since it's done solely on the browser.

Epeli
If it's important to limit image sizes and avoid having people prank the site, then there definitely is a need to check this server-side.
Pointy
I'll test both and see what fits best, thanks alot for this!
Alex Cane
this is indeed a working solution, but hope everybody will do this task on server (with js disabled users must not be allowed to upload forbidden images)
Fabrizio Calderan