0

I'm having problems figuring out why my layer does not resize to match the dimensions of the source layer. I set signatureLayer equal to the layer named "signature" and pass it and the layer I want changed into the function changeSizeto. It looks like it's taking the dimensions of the background layer instead. I can't see where I messed up. This is just a small bit of the script I'm working on but essentially I will be reading in an image location from a txt file, resizing it according to the source layer "signature" dimensions, saving the psd out as an image, then read in another image location and repeat.

#target photoshop

// must have source psd open to start with.

//pref pixels
app.preferences.rulerUnits = Units.PIXELS;

// call the source document
var srcDoc = app.activeDocument;
var signatureLayer = srcDoc.layers.getByName("signature");  

//var sourceFile= new File("E:/Signatures/sig1.tif");
var sourceFile= new File("C:/Users/harry/OneDrive/Desktop/Replace/test.jpg");
var idPlc = charIDToTypeID( "Plc " );
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc3.putPath( idnull, sourceFile);
var idFTcs = charIDToTypeID( "FTcs" );
var idQCSt = charIDToTypeID( "QCSt" );
var idQcsa = charIDToTypeID( "Qcsa" );
desc3.putEnumerated( idFTcs, idQCSt, idQcsa );
executeAction( idPlc, desc3, DialogModes.NO );

var replacementImage = srcDoc.layers[0]

changeSizeto(signatureLayer,replacementImage)

function changeSizeto(source,chngLayer){
    startRulerUnits = app.preferences.rulerUnits;
    //pref pixels
    app.preferences.rulerUnits = Units.PIXELS

    // Calculate length and width based on the rectangular bounds of the selected layer
    var sourceHeight = source.bounds[2].value-source.bounds[0].value; //Grab the length
    var sourceWidth = source.bounds[3].value-source.bounds[1].value; //Grab the width

    var newWidth=sourceWidth;
    var newHeight=sourceHeight;

    chngLayer.resize(newWidth,newHeight,AnchorPosition.MIDDLECENTER);

}

Edit:

test

0

First of all, your sourceHeight and sourceWidth are mixed up: 2 and 0 are X bounds, so they should be in sourceWidth and 3 and 1 are Y bounds — that's the sourceHeight.

Next, you're feeding pixel values to .resize, but here's what JS Scripting Reference says about this method:

Resizes the layer to the specified dimensions (as a percentage of its current size) and places it in the specified position.

So you need to calculate a percentage you want to resize your layer to based on source layer size. Something like this:

function changeSizeto(source, chngLayer)
{
    startRulerUnits = app.preferences.rulerUnits;
    //pref pixels
    app.preferences.rulerUnits = Units.PIXELS

    // Calculate length and width based on the rectangular bounds of the selected layer
    var sourceWidth = source.bounds[2].value - source.bounds[0].value; //Grab the length
    var sourceHeight = source.bounds[3].value - source.bounds[1].value; //Grab the width

    var changeWidth = chngLayer.bounds[2].value - chngLayer.bounds[0].value; //Grab the length
    var changeHeight = chngLayer.bounds[3].value - chngLayer.bounds[1].value; //Grab the width

    var newWidth = sourceWidth / changeWidth * 100;
    var newHeight = sourceHeight / changeHeight * 100;

    chngLayer.resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
}
  • I had forgotten to change the indices for the sourceHeight and sourceWidth since I copied that bit of code from a different project. However, the replacement image is not changing size. I've included a screenshot of the result. The white rectangle is the source layer and the black rectangle is the layer that is to be resized. – roldy Mar 3 at 8:41
  • It works for me just fine: take.ms/fRfJl Have you tried to debug values that go to .resize to see what's happening? – Sergey Kritskiy Mar 3 at 8:52
  • I figured out what I was doing wrong. Works perfect now, thanks. – roldy Mar 3 at 18:49
  • If the answer worked for you, don't forget to accept it. Thanks! – Sergey Kritskiy Mar 3 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.