Forum Moderators: open

Message Too Old, No Replies

To validate image using it's height and width attributes

I want restrict the size of image to be uploaded by using javascript

         

kalucode

5:23 pm on Apr 21, 2006 (gmt 0)



Hi Guys,

I am very new to javascript and needs to restrict the size of image to be uploaded by using javascript to check the image's height and width against a spcified value before allowing it to be uploaded. Can somebody help me with the code that can help me achieve that.

Thanks in advance for your help.

Inny

4:31 am on Apr 22, 2006 (gmt 0)

10+ Year Member



you could use css in your head tags to limit size of images, as an alternative

this would go between <head> tags

<style>
img{max-width: 500px}
img{width:expression(width>500?"500":width)}
</style>

where 500px is your max allowed

rocknbil

3:33 pm on Apr 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



needs to restrict the size of image to be uploaded by using javascript to check the image's height and width

I don't think this can be done. Think of it this way: the browser's file selection tool has no distinction as to what type of file it is. It could be a PDF file which has no inherent width and height. All the file upload object does is send the form data to the server, it doesn't actually read it.

You can't even read the local file for a file size, Javascript doesn't do that. If it could, that would be one truck-sized security hole. The only solution here, one used by most message boards, is to allow the upload to proceed and examine the file with a server-side program once it' fully uploaded. A typical example is to use perl and ImageMagick (or that "other" language, PHP :-) ) to examine the file's res, width, height, file type, and act accordingly. In most cases, it's just easier to resize it while processing rather than tell the user to do it, they may not even know how.

You'll have to dig in and start learning server-side programming or buy and install software to do this.