Checking image size

Discussion in 'Javascript' started by Antonie C Malan Snr, Sep 27, 2004.

  1. Dear All,

    I tried posting this before, but it seems to have disappeared.

    I need to check image sizes before the user uploads them. This works in
    the Mozilla family on second and subsequent iterations:

    if(form.photo1.value != ""){
    var piccie = new Image();
    piccie.src = "file://" + form.photo1.value;
    if(piccie.height > 244 || piccie.height < 236 || piccie.width > 324
    || piccie.width < 316){
    alert("Your photo 1 is " + piccie.width + " pixels wide and " +
    piccie.height + " pixels high.\n The required dimensions are 320w
    x 240h. Please scale your picture");
    return false;
    }
    }

    This means the user must try twice on the Mozilla family of browsers.
    The first time a hight and width of 0 are returned.

    On Opera it does not work at all. The "file://" is needed when this
    runs on the server.

    I don't know about MSIE as I don't have a server setup on the Windows
    side of my computer and very rarely go there.

    Any ideas on this? I'd be grateful.

    Chris
     
    Antonie C Malan Snr, Sep 27, 2004
    #1
    1. Advertising

  2. Antonie C Malan Snr

    Grant Wagner Guest

    Antonie C Malan Snr wrote:

    > I need to check image sizes before the user uploads them. This works in
    > the Mozilla family on second and subsequent iterations:
    >
    > if(form.photo1.value != ""){
    > var piccie = new Image();
    > piccie.src = "file://" + form.photo1.value;


    At this point in your code, piccie will not have loaded yet. It's height and width attributes could be
    anything from 0 to undefined to some random value.

    > if(piccie.height > 244 || piccie.height < 236 || piccie.width > 324
    > || piccie.width < 316){
    > alert("Your photo 1 is " + piccie.width + " pixels wide and " +
    > piccie.height + " pixels high.\n The required dimensions are 320w
    > x 240h. Please scale your picture");
    > return false;
    > }
    > }
    >
    > This means the user must try twice on the Mozilla family of browsers.
    > The first time a hight and width of 0 are returned.


    Because in Gecko-based browsers, the height and width attributes are the value of the previous image,
    in your case, there is no previous image, so height and width are 0.

    > On Opera it does not work at all. The "file://" is needed when this
    > runs on the server.


    No, it really isn't. If it's coming from a Web server, then you either don't need the protocol at all,
    or it should be http:

    Oh I see what you are doing, you are trying to determine the size of an image a user has selected?

    You simply can not do that in a Web browser in the default security environment. It would require
    having access to the local file system, and you do not have access to the local file system.

    It happens to work in Mozilla because the entire page is coming from the local file system, so the
    script on the page assumes the image is just loading from the same source as the rest of the page.

    > I don't know about MSIE as I don't have a server setup on the Windows
    > side of my computer and very rarely go there.


    Internet Explorer has nothing to do with having a Web server set up. It is a Web browser.

    > Any ideas on this? I'd be grateful.


    Yes, what you are atttempting to do - determine the dimensions of an image that has not been uploaded
    to the server - using client-side JavaScript in the default security environment is impossible.

    You will have to allow the user to upload the image, and then determine the dimensions on the server.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Sep 27, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGFyYWtp?=

    Image size problem. Image Compression

    =?Utf-8?B?UGFyYWtp?=, Jun 24, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    499
    Kevin Spencer
    Jun 24, 2005
  2. Darrel
    Replies:
    4
    Views:
    393
    Rob Meade
    Jul 20, 2006
  3. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,643
    Michael Jung
    May 25, 2008
  4. Bob Richardson
    Replies:
    3
    Views:
    247
    Bob Richardson
    Mar 8, 2005
  5. bluedolphin
    Replies:
    1
    Views:
    164
Loading...

Share This Page