Skip to main content
Inspiring
Feb 9, 2024 5:07 PM UTC
Question

Get image dimensions as float?

This is a bit backward: 

var imageHeight = app.activeDocument.height.value;
if (imageHeight == 224)
{
  // do stuff
}

Didn't work for once - I assumed that image dimensions were ints, but there we go... 

Casting it to an int worked: 

if (parseInt(imageHeight) == 224)
{
  // do stuff
}

 

Case closed.

 

But! Just out on curiousity, how do you get the float value of  app.activeDocument.height??? - it always seems to report it as in int. Assuming that's what's going wrong.

I tried casting it as float and multiplying it by 1,000,000.

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

Stephen Marsh
Community Expert
Community Expert
Feb 9, 2024 10:19 PM UTC

@Ghoul Fool this is a curious situation!

 

Did you explicitly set pixels as the unit of measure earlier in the script? Or was it just naturally assumed from the current ruler state?

 

EDIT: I just tested and even explicitly setting the units to px in the script still returns false.

 

alert(imageHeight.toSource()); even reports that the value is a number!
 
So annoying!

 

Translate
Inspiring
Feb 10, 2024 9:18 AM UTC

I'm glad you find it curious too!

There's no explicit unit set up in the script - as I use pixels  so assume that's been set and not changed.

Secondly, the images size is an odd 200 dpi,  256 x 224 pixels (w & h)

Thirrdly resizing the image to 72 dpi,  256 x 224 pixels (w & h) - the script works fine.

Translate
Legend
Feb 9, 2024 6:19 PM UTC
The "value" depends on the "height" object type, which is the same as the ruler units.
 
 
 
Translate
Inspiring
Feb 9, 2024 7:19 PM UTC

Units are pixels. Even without the "value" I get an object that's "244 px" and yet alert( app.activeDocument.height.value == 224) is false.

Translate
Legend
Feb 9, 2024 7:41 PM UTC
Yes, sometimes there is such a glitch. Using this method helps.
 
alert( Number(app.activeDocument.height.value) == 224)
Translate