Copy link to clipboard
Copied
var layer = app.activeDocument.activeLayer;
var Width = layer.bounds[2] - layer.bounds[0];
var Height = layer.bounds[3] - layer.bounds[1];
alert(Width + "\n" + Height)
The dimensions obtained by the above code are 207 and 765, but the actual dimensions are 567 and 850, because the image contains transparent areas. How can I get the actual dimensions? Thank you, everyone
Because I clicked on File, Place Embedded, and selected a PNG file, but this PNG file may contain transparent pixels on the top, bottom, left, and right. Then I need to freely transform it proportionally to match the current document size, so I must first get the current smart object layer's size in the current document.
By @w30290083o9nn
Having an embedded PNG complicates the layer or channel based hacks that I would employ when using a PSD/PSB.
...
Copy link to clipboard
Copied
Is the smart object raster? If so, when you edit it - what is the canvas size?
P.S. You should also explicitly work in pixels, either directly or temporarily setting ruler units before getting the bounds, just in case the current ruler units change.
Copy link to clipboard
Copied
The current layer is a smart object layer, not rasterized.
The ruler is in pixel units
Copy link to clipboard
Copied
The current layer is a smart object layer, not rasterized.
I can see that, but is it a raster object that Photoshop can edit/open, or is it a vector object that would be opened in an external editor such as Illustrator?
The ruler is in pixel units
By @w30290083o9nn
My point was that this shouldn't be assumed. I don't know if you only posted a snippet of code, are you accounting for this or not?
Copy link to clipboard
Copied
It is the width and height displayed when the current layer is in free transform.
Copy link to clipboard
Copied
Is the smart object raster? If so, when you edit it - what is the canvas size?
By @Stephen Marsh
I was referring to something like this:
#target photoshop
if (app.documents.length) {
var lyr = app.activeDocument.activeLayer;
if (lyr.kind == LayerKind.SMARTOBJECT) {
var result = getSmartObjectCanvasBounds(lyr);
alert("Smart object canvas size: " + result.width + " - " + result.height + " px");
} else {
alert("Active layer is not a smart object.");
}
}
function getSmartObjectCanvasBounds(layer) {
if (layer.kind != LayerKind.SMARTOBJECT) {
throw new Error("Selected layer is not a smart object");
}
// Remember current doc
var mainDoc = app.activeDocument;
// Edit contents (opens PSB)
executeAction(stringIDToTypeID("placedLayerEditContents"), undefined, DialogModes.NO);
smartObjDoc = app.activeDocument;
// Get the full canvas bounds, explicitly in pixels
var w = smartObjDoc.width.as("px");
var h = smartObjDoc.height.as("px");
// Close without saving, return to main doc
smartObjDoc.close(SaveOptions.DONOTSAVECHANGES);
app.activeDocument = mainDoc;
return { width: w, height: h, bounds: [0, 0, w, h] };
}
Copy link to clipboard
Copied
I want to get the current smart object layer's size in the current document, not the original size inside the smart object. If it's about getting the size inside the smart object, the code below can do that.Still, thank you for providing the code, but it's not what I was looking for. I really appreciate it
#target photoshop
app.runMenuItem(stringIDToTypeID('placedLayerEditContents'));
var activeBounds = app.activeDocument.activeLayer.bounds;
app.activeDocument.close();
alert(activeBounds);
Copy link to clipboard
Copied
I want to get the current smart object layer's size in the current document, not the original size inside the smart object. If it's about getting the size inside the smart object, the code below can do that.Still, thank you for providing the code, but it's not what I was looking for. I really appreciate it
#target photoshop app.runMenuItem(stringIDToTypeID('placedLayerEditContents')); var activeBounds = app.activeDocument.activeLayer.bounds; app.activeDocument.close(); alert(activeBounds);
By @w30290083o9nn
This code incorrectly gets the layer bounds, excluding the transparency and doesn't take SO scaling into account.
Presuming that the SO has not been scaled, to get the canvas size you need to do what my previous script shows, which is the same size as what the transform of the smart object shows.
However, if the smart object has been scaled, then I believe that you need to do something else.
Copy link to clipboard
Copied
Because I clicked on File, Place Embedded, and selected a PNG file, but this PNG file may contain transparent pixels on the top, bottom, left, and right. Then I need to freely transform it proportionally to match the current document size, so I must first get the current smart object layer's size in the current document.
Copy link to clipboard
Copied
Because I clicked on File, Place Embedded, and selected a PNG file, but this PNG file may contain transparent pixels on the top, bottom, left, and right. Then I need to freely transform it proportionally to match the current document size, so I must first get the current smart object layer's size in the current document.
By @w30290083o9nn
Having an embedded PNG complicates the layer or channel based hacks that I would employ when using a PSD/PSB.
AFAIK, there are only hacks, as this isn't covered by the DOM and I am unaware of AM alternatives.
Good luck!
Copy link to clipboard
Copied
Okay, thank you
Copy link to clipboard
Copied
If you're willing to use the script to convert the embedded PNG to PSB, then the hacks I have in mind can be used.
Copy link to clipboard
Copied
In fact, my current document is already a PSB file. Since the embedded PNG contains transparent pixels, if I rasterize it, the free transform will cause blurring. Therefore, I have to look for a way to get the actual dimensions of the smart object layer as displayed in the current document.
Copy link to clipboard
Copied
In fact, my current document is already a PSB file. Since the embedded PNG contains transparent pixels, if I rasterize it, the free transform will cause blurring. Therefore, I have to look for a way to get the actual dimensions of the smart object layer as displayed in the current document.
By @w30290083o9nn
You misunderstood.
As PNG can't use layers (or alpha channels), the hack that I have in mind can't be used.
The previous linked topic was to a script that I wrote that will automatically create an embedded PSB smart object from a JPEG, PNG etc. It is not rastrerized or otherwise changed from the original apart from the file format now being PSB.
The hack that I have in mind would go like:
... Your existing code ...
1) Select embedded PNG layer
2) Convert to embedded PSB via my code
3) Edit the PSB, add a temporary solid fill layer (1% opacity), save, close
4) Get the layer bounds, which now contain no transparency and will now report the correct bounds, store this as a variable
... Your code to resize the layer ...
5) Edit the PSB, remove the temorary solid fill layer, save, close
... Your code carries on doing what it does ...
Or something like that.
Copy link to clipboard
Copied
I roughly understand your point. Editing the PSB, adding a temporary solid fill layer, saving, closing, and then getting the current layer's size is obviously feasible. However, after getting the size, it would require entering the smart object again to delete the temporary fill layer and then save the smart object, which significantly increases the script's runtime. Nevertheless, the method is feasible, and I really appreciate your suggestion. Thank you.
Copy link to clipboard
Copied
Yes, it's all a hack, it's all we have unless someone has a better idea or esoteric action manager code.
Copy link to clipboard
Copied
The edited canvas size of the original smart object, including transparency padding (500x500px):
Using transform, the scaled placed size, now 259px rather than the original 500px total size:
The script correctly reports the same scaled 259 total pixel size, not 500px:
I think this is code better than my previous suggestions, you were right to look at layer bounds, but not by opening the SO, just checking the bounds of the active layer. This includes transparency and includes the scaled size. So much simpler than my previous suggestions.
#target photoshop
// Get the currently selected layer
var layer = app.activeDocument.activeLayer;
// Check if the layer is a Smart Object
if (layer.kind == LayerKind.SMARTOBJECT) {
// Get the bounds of the Smart Object layer (left, top, right, bottom)
var layerBounds = layer.bounds;
// Calculate the width and height of the Smart Object layer
var layerWidth = layerBounds[2].as("px") - layerBounds[0].as("px"); // right - left
var layerHeight = layerBounds[3].as("px") - layerBounds[1].as("px"); // bottom - top
// Display the dimensions
alert("Smart Object Dimensions (including transparency):\nWidth: " + layerWidth + " px" + " \nHeight: " + layerHeight + " px");
} else {
alert("The selected layer is not a Smart Object.");
}
Copy link to clipboard
Copied
Thank you for providing the code. Why is the size not accurate when I use this code? Under free transform, the width of this layer is 567 and the height is 850, but the script retrieves the width as 207 and the height as 765
Find more inspiration, events, and resources on the new Adobe Community
Explore Now