Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
Oct 06, 2021 Oct 06, 2021

I tried to get original dimension of placedItem in Script.

How can I get?

Sorry, I can't write English well.

TOPICS
Scripting
454
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Oct 06, 2021 Oct 06, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 06, 2021 Oct 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2021 Oct 06, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 06, 2021 Oct 06, 2021

thx, Charu.

I understand to get XMP. 

but, 

imageFile.readln()

is garbled characters.

 

thx, great idea.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2021 Oct 06, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 06, 2021 Oct 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 06, 2021 Oct 06, 2021
LATEST

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?d...

Let me know if I am misunderstanding something

-Manan

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines