Image upload dimension check

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gianpiero Colagiacomo

    Image upload dimension check

    Hi,

    I'm trying to use the following code (found online - can't remember where)
    to validate images being uploaded to my server:

    function checkImageUploa d (fileName) {
    if (document.layer s && location.protoc ol.toLowerCase( ) != 'file:' &&
    navigator.javaE nabled()){
    netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileRead');
    }
    var msg = '';
    var img = new Image();
    img.src = 'file:///' + fileName;
    if (img.width != 180 || img.height != 90){
    msg = "The selected image dimensions are not valid. The image must be 180
    x 90 pixels.\n\n";
    msg = msg + "Your selected image dimensions are " + img.width + " x " +
    img.height;
    alert('The required information is incomplete or contains
    errors:\t\t\t\t \t\n\n'+msg);
    return false
    } else {
    return true
    }
    }

    Trouble is it won't work in Netscape. I get 0 x 0 pixels reported as being
    the image size although the if(document.lay ers... line should be dealing
    with Netscape issues (I think)...

    What I ultimately want is to be able to make this script cross browsers
    compatible and cross platform is possible...

    Can anyone help me out? I'd prefer not to have to revert to validating the
    file AFTER having uploaded it...

    I also need to be able to make the script only accept jpg's and gif's - is
    there an easy method of achieving this?

    Cheers,

    GP


  • Nobody

    #2
    Re: Image upload dimension check


    "Gianpiero Colagiacomo" <gp@1lg.com> wrote in message
    news:bj7m59$d6m $1@lacerta.tisc alinet.it...
    | Hi,
    |
    | I'm trying to use the following code (found online - can't remember where)
    | to validate images being uploaded to my server:
    |
    | function checkImageUploa d (fileName) {
    | if (document.layer s && location.protoc ol.toLowerCase( ) != 'file:' &&
    | navigator.javaE nabled()){
    | netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileRead');
    | }

    That is some bizarre logic. What does layers have to do with whether the
    security object is present? FYI layers are only present in NS4 and OmniWeb
    (at current count.)

    | var msg = '';
    | var img = new Image();
    | img.src = 'file:///' + fileName;
    | if (img.width != 180 || img.height != 90){
    | msg = "The selected image dimensions are not valid. The image must be
    180
    | x 90 pixels.\n\n";

    No idea why NS would come up with 0x0. Have you tried using a dummy image
    instead of creating the object on the fly? In fact, that is almost
    certainly it as the created image is hidden.

    I would validate this on the server-side anyway. None of this stuff will
    work if script is turned off!




    Comment

    • Gianpiero Colagiacomo

      #3
      Re: Image upload dimension check

      Nobody - Thanks I'll try using a dummy image and see if that solves it...

      As for the 'bizarre logic' - as I said before the script came from someone
      else - I only altered it a little and that line was left alone as I had no
      idea what it was attempting to do...

      What would you suggest for checking if the security object IS present (cross
      browser)?

      Cheers,

      GP

      "Nobody" <none@nope.ne t> wrote in message
      news:LxK5b.66$K t1.34@fe3.colum bus.rr.com...[color=blue]
      >
      > "Gianpiero Colagiacomo" <gp@1lg.com> wrote in message
      > news:bj7m59$d6m $1@lacerta.tisc alinet.it...
      > | Hi,
      > |
      > | I'm trying to use the following code (found online - can't remember[/color]
      where)[color=blue]
      > | to validate images being uploaded to my server:
      > |
      > | function checkImageUploa d (fileName) {
      > | if (document.layer s && location.protoc ol.toLowerCase( ) != 'file:' &&
      > | navigator.javaE nabled()){
      > |[/color]
      netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileRead');[color=blue]
      > | }
      >
      > That is some bizarre logic. What does layers have to do with whether the
      > security object is present? FYI layers are only present in NS4 and[/color]
      OmniWeb[color=blue]
      > (at current count.)
      >
      > | var msg = '';
      > | var img = new Image();
      > | img.src = 'file:///' + fileName;
      > | if (img.width != 180 || img.height != 90){
      > | msg = "The selected image dimensions are not valid. The image must be
      > 180
      > | x 90 pixels.\n\n";
      >
      > No idea why NS would come up with 0x0. Have you tried using a dummy image
      > instead of creating the object on the fly? In fact, that is almost
      > certainly it as the created image is hidden.
      >
      > I would validate this on the server-side anyway. None of this stuff will
      > work if script is turned off!
      >
      >
      >
      >[/color]


      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Image upload dimension check

        "Gianpiero Colagiacomo" <gp@1lg.com> writes:
        [color=blue]
        > I'm trying to use the following code (found online - can't remember where)[/color]

        Never a good sign.
        [color=blue]
        > to validate images being uploaded to my server:
        >
        > function checkImageUploa d (fileName) {
        > if (document.layer s && location.protoc ol.toLowerCase( ) != 'file:' &&
        > navigator.javaE nabled()){
        > netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileRead');
        > }[/color]

        I'll assume this works for Netscape 4 with a non-file-protocol and
        java enabled.

        [color=blue]
        > img.src = 'file:///' + fileName;
        > if (img.width != 180 || img.height != 90){[/color]

        Here is a serious problem. Images are loaded asynchronelousl y. A local
        file might be loaded fast enough that you this works ... most of the
        time, but you should always wait for the image to load.

        One way to do that, is to put the rest of the code in the image's onload
        handler, and add an onerror handler for the case where it doesn't work.
        [color=blue]
        > Trouble is it won't work in Netscape.[/color]

        Which Netscape?
        There are Netscape 4 and Netscape 6/7, which are two completely unrelated
        browsers.
        [color=blue]
        > What I ultimately want is to be able to make this script cross browsers
        > compatible and cross platform is possible...[/color]

        Good luck. I don't think there is any way my browser will let you read
        a local file.
        [color=blue]
        > I also need to be able to make the script only accept jpg's and gif's - is
        > there an easy method of achieving this?[/color]

        No. You can probably check the filename's extension, but that proves nothing.

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Gianpiero Colagiacomo

          #5
          Re: Image upload dimension check

          Ok guys - I get it - check the dimensions in server-side

          Thanks...

          BTW Jim Ley - your were right - I tried using settimeout on the dimension
          check and it worked fine in NN7 and IE5.5. I didn't realise you could set
          an onload handler which obviously is the better method.

          GP

          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:ekyws8fw.f sf@hotpop.com.. .[color=blue]
          > "Gianpiero Colagiacomo" <gp@1lg.com> writes:
          >[color=green]
          > > I'm trying to use the following code (found online - can't remember[/color][/color]
          where)[color=blue]
          >
          > Never a good sign.
          >[color=green]
          > > to validate images being uploaded to my server:
          > >
          > > function checkImageUploa d (fileName) {
          > > if (document.layer s && location.protoc ol.toLowerCase( ) != 'file:' &&
          > > navigator.javaE nabled()){
          > >[/color][/color]
          netscape.securi ty.PrivilegeMan ager.enablePriv ilege('Universa lFileRead');[color=blue][color=green]
          > > }[/color]
          >
          > I'll assume this works for Netscape 4 with a non-file-protocol and
          > java enabled.
          >
          >[color=green]
          > > img.src = 'file:///' + fileName;
          > > if (img.width != 180 || img.height != 90){[/color]
          >
          > Here is a serious problem. Images are loaded asynchronelousl y. A local
          > file might be loaded fast enough that you this works ... most of the
          > time, but you should always wait for the image to load.
          >
          > One way to do that, is to put the rest of the code in the image's onload
          > handler, and add an onerror handler for the case where it doesn't work.
          >[color=green]
          > > Trouble is it won't work in Netscape.[/color]
          >
          > Which Netscape?
          > There are Netscape 4 and Netscape 6/7, which are two completely unrelated
          > browsers.
          >[color=green]
          > > What I ultimately want is to be able to make this script cross browsers
          > > compatible and cross platform is possible...[/color]
          >
          > Good luck. I don't think there is any way my browser will let you read
          > a local file.
          >[color=green]
          > > I also need to be able to make the script only accept jpg's and gif's -[/color][/color]
          is[color=blue][color=green]
          > > there an easy method of achieving this?[/color]
          >
          > No. You can probably check the filename's extension, but that proves[/color]
          nothing.[color=blue]
          >
          > /L
          > --
          > Lasse Reichstein Nielsen - lrn@hotpop.com
          > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          > 'Faith without judgement merely degrades the spirit divine.'[/color]


          Comment

          Working...