Skip to main content
Inspiring
June 5, 2026
Answered

There is an issue with Free Transform not matching the document dimensions.

  • June 5, 2026
  • 3 replies
  • 63 views

There's an issue with this Photoshop script. When opening a Photoshop.psd file containing layers labeled A2 and A3, the current document dimensions are 2484 pixels wide by 3512 pixels high. The script is designed to scale the active layer to match the document size exactly. While the A3 smart object layer scales correctly with this script, the A2 layer ends up with dimensions of 2482 pixels wide by 3510 pixels high after running—slightly off from the document size. I don't want the layer to be rasterized; although using Free Transform after rasterization produces correct dimensions, I need to keep it as a smart object layer. Running the script twice in succession does yield the correct result, but it's inefficient. I'd appreciate any alternative solutions—thank you all for your help.

var doc = app.activeDocument;
var length = parseFloat(doc.width); // Get document width
var width = parseFloat(doc.height); // Get document height

var layer = doc.activeLayer;

// Get and adjust layer size (based on document width and height)
var layerBounds = layer.bounds;
var layerWidth = layerBounds[2].value - layerBounds[0].value;
var layerHeight = layerBounds[3].value - layerBounds[1].value;

var newWidth = (length / layerWidth) * 100; // Adjust layer width based on document width
var newHeight = (width / layerHeight) * 100; // Adjust layer height based on document height

layer.resize(newWidth, newHeight); // Free transform to adjust layer size

// Re-acquire the scaled layer bounds
layerBounds = layer.bounds;

// Calculate layer center point coordinates
var layerCenterX = (layerBounds[2].value + layerBounds[0].value) / 2;
var layerCenterY = (layerBounds[3].value + layerBounds[1].value) / 2;

// Calculate document center point coordinates
var documentCenterX = doc.width.value / 2;
var documentCenterY = doc.height.value / 2;

// Calculate horizontal and vertical distance to move
var deltaX = documentCenterX - layerCenterX;
var deltaY = documentCenterY - layerCenterY;

// Move layer to document center point
layer.translate(deltaX, deltaY);
// Scale image to match document dimensions and center it in the document

 

    Correct answer c.pfaffenbichler

    I just noticed that the Smart Object in question is not clean – it has been scaled (so to speak) and its edge pixels are already affected by resampling. 

    By virtue of it having a different resolution than the containing document it may claim to have a 100%-scale but pixel-wise it doesn’t. 

     

    Please try inserting 

    try {executeAction( stringIDToTypeID( "placedLayerResetTransforms" ), undefined, DialogModes.NO )} catch (e) {};

    before you collect the Smart Object’s bounds. 

    3 replies

    c.pfaffenbichler
    Community Expert
    c.pfaffenbichlerCommunity ExpertCorrect answer
    Community Expert
    June 7, 2026

    I just noticed that the Smart Object in question is not clean – it has been scaled (so to speak) and its edge pixels are already affected by resampling. 

    By virtue of it having a different resolution than the containing document it may claim to have a 100%-scale but pixel-wise it doesn’t. 

     

    Please try inserting 

    try {executeAction( stringIDToTypeID( "placedLayerResetTransforms" ), undefined, DialogModes.NO )} catch (e) {};

    before you collect the Smart Object’s bounds. 

    Stephen Marsh
    Community Expert
    Community Expert
    June 7, 2026

    @c.pfaffenbichler - I think that does the trick, well done! Much better than what I came up with when inspecting the SO layer discrepancy.

     

    The original image inside the smart object is much smaller than the target canvas size. I haven't tested a downsize with a larger image, which would be a better proposition than scaling up.

    Stephen Marsh
    Community Expert
    Community Expert
    June 7, 2026

    @w30290083o9nn 

     

    The Properties panel for layer "A2" reports 1280 x 1776 px.

     

    If one duplicates this layer to a new document, the properties are 1280 x 1776 px, the same as the original, however, the content is bounded by a lot of transparent pixels. If one then trims the transparency, the layer bounds are reported as 1279 x 1775 px. This one-pixel discrepancy in X and Y makes all the difference.

     

    If one then duplicates the transparency trimmed duplicate document layer back to the original document and then transforms this layer with your code, the resulting size is correct.

    Inspiring
    June 7, 2026

    ok,thank you

    Stephen Marsh
    Community Expert
    Community Expert
    June 7, 2026

    @w30290083o9nn - You're welcome!

     

    Does that give you enough to go by?

    c.pfaffenbichler
    Community Expert
    Community Expert
    June 5, 2026

    I suspect this is a (not quite obvious) rounding issue. 

     

    As a work-around you could run the operation twice within one Script or possibly employ a temporary second file cropped to the SO’s dimensions, scale that and duplicating the now scaled SO into the original image. 

     

    You could also try whether AM code (as recorded with ScriptingListener.plugin) might work better. 

    Inspiring
    June 5, 2026

    thank you