Skip to main content
Participant
October 6, 2021
Question

How can I get to original dimension of image in Script?

  • October 6, 2021
  • 2 replies
  • 431 views

I tried to get original dimension of placedItem in Script.

How can I get?

Sorry, I can't write English well.

This topic has been closed for replies.

2 replies

Community Expert
October 7, 2021

How about the following

var obj = app.selection[0]
var bb = obj.boundingBox
var h = Math.abs(bb[2]-bb[0])
var w = Math.abs(bb[3]-bb[1])
alert("height " + h + " width " + w)

-Manan

-Manan
toedaAuthor
Participant
October 7, 2021

thx,  Manan.

But, result is not original size.

I meaned "original size".

 

I have a image that width of 767 px.

 

const item = layer.placedItems.add();

item.file = new File('sample.png'); //767px

alert(item.width);

 

but that's 184px.

your code's result is also 184px.

 

thx.

Community Expert
October 7, 2021

Seems to work for me, i integrated your code into mine to replicate exactly what you do. See the screengrab at the following link

https://www.dropbox.com/s/cug3ou1e64dlndj/Screen%20Recording%202021-10-07%20at%2010.38.41%20AM.mov?dl=0

Let me know if I am misunderstanding something

-Manan

-Manan
Charu Rajput
Community Expert
Community Expert
October 6, 2021

If I understand your question correctly then you need to get width and height of the the item.

So you can get this, by using width and height property.

 

Steps to follow.

1. Place item.

2. Make sure you do not perform any transformation on it.

3. Select the placed item.

4. Run following line of code, it will give you width and height

var _selectedItem = app.selection[0]
alert(_selectedItem.width)
alert(_selectedItem.height)

Let us know if this is what are you looking for.

Best regards
toedaAuthor
Participant
October 6, 2021

thx, Charu.

I meaned "original size".

I have a image that width of 767 px.

 

const item = layer.placedItems.add();

item.file = new File('sample.png'); //767px

alert(item.width);

 

but that's 184px.

 

I know this is 72dpi and 300dpi magic.

 

I wanna know to get 767px in script.

Charu Rajput
Community Expert
Community Expert
October 6, 2021

Hi,

Got it. Try following snippet.

var layer = app.activeDocument.activeLayer;
var _item = layer.placedItems.add();
var _file = new File('sample.png');
_item.file = _file;
var _originalDimesnions = getOriginalWidthAndHeight(_item.file)
alert(_originalDimesnions[0]);
alert(_originalDimesnions[1]);

function getOriginalWidthAndHeight(imageFile) {
    imageFile.open('r');
    try {
        while (imageFile.tell() < imageFile.length) {
            var line = imageFile.readln();
            var idxOf = (line.indexOf('<exif:PixelXDimension>') > -1) ? line.indexOf('<exif:PixelXDimension>') : line.indexOf('<exif:PixelYDimension>');
            if (idxOf > -1) {
                if (line.indexOf('<exif:PixelXDimension>') > -1)
                    _width = parseInt(line.substring(line.indexOf(">") + 1, line.lastIndexOf("<")));
                else
                    _height = parseInt(line.substring(line.indexOf(">") + 1, line.lastIndexOf("<")));
            }
        }
    } catch (ex) {
        alert('Error');
    }
    finally {
        imageFile.close();
        return ([_width, _height]);
    }
}

 

I hope this gives orginal width and height.

Best regards