skip to Main Content

I need to open an image and check that the width and height are even numbers. If they are not -1px.

How do I check to see if a number is odd?

291px would = 290px

333px would = 332px

121px would = 120px

3

Answers


  1. You could divide the original length by two, round it down with Math.floor() and multiply the result with two to get the desired result.

    var wa = app.documents[0].width;
    var ha = app.documents[0].height;
    var wb = 2*Math.floor(wa/2);
    var hb = 2*Math.floor(ha/2);
    app.activeDocument.resizeCanvas(wb, hb);
    
    Login or Signup to reply.
  2. Untested, but you should be able to do this:

    if(x&1) {x--}
    

    That tests if the least significant bit is one (i.e. x is odd) and decrements it if so.

    Login or Signup to reply.
  3. You can check using modulus!

    // call the source document
    var srcDoc = app.activeDocument;
    
    // get original width and height
    var docWidth = srcDoc.width.value;
    var docHeight = srcDoc.height.value;
    
    if (docWidth%2 == 0) alert("Image is an even number of pixels wide);
    else alert("Image is an odd number of pixels wide);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search