Discover
Discover

Reputation: 197

How do you center a layer in a Photoshop script?

My Photoshop Canvas is 900X600.

The function below takes Layer X and makes Layer X copy.

It takes Layer X copy and while maintaining the ratio adjusts the height to 600px. var newdLayer

It takes Layer X and while maintaining the ratio adjusts the width to 900px and applies the Gaussian Blur. var blur.

It then merges Layer X and Layer X copy.

The problem is that if Layer X is not centered in the beginning of the script then it malfunctions.

How to add center layer to Layer X in the beginning of the script?

(function (){

var docRef = activeDocument
var blur = docRef.activeLayer;
var newdLayer = blur.duplicate();

    var startRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    // since we resize based on the initial size of the source layer, 
    // we don't need to get the bounds twice
    var bounds = blur.bounds;
    var height = bounds[3].value - bounds[1].value;
    var width = bounds[2].value - bounds[0].value;
    // declare 2 different vars for your sizes (there are better ways to do this, but
    // since you say you aren't a JavaScript pro, I figured I'd keep it simple)
    var newSize600 = (100 / height) * 600;
    var newSize900 = (100 / width) * 900;
    // resize your layers
    newdLayer.resize(newSize600, newSize600, AnchorPosition.MIDDLECENTER);
    blur.resize(newSize900, newSize900, AnchorPosition.MIDDLECENTER);
    // apply blur
    blur.applyGaussianBlur(5);

    // below creates the group, moves the layers to it and merges them. Feel free to just include this part
    // at the end of your function if you don't want to use the modified code above. 

    // create a new layer set
    var groupOne = docRef.layerSets.add();

    // move the blur layer inside the layer set and name the layer for posterity
    blur.move(groupOne, ElementPlacement.INSIDE);
    blur.name = "blur";

    // move the newdLayer inside and rename
    newdLayer.move(groupOne, ElementPlacement.INSIDE);
    newdLayer.name = "newdLayer";

    // merge the layer set and name the new layer
    var mergedGroup = groupOne.merge();
    mergedGroup.name = "newdLayer + blur";

    app.preferences.rulerUnits = startRulerUnits;

})();

Upvotes: 1

Views: 1613

Answers (1)

Sergey Kritskiy
Sergey Kritskiy

Reputation: 2269

What you need to do is to calculate difference between layer center and document center and then translate the blur layer by that difference before making a copy.

To calculate the layer center you take bound[0].value and bound[1].value (distances from the left top corner of the document to the top left corner of the layer) and add half of the width and half of the height. And then to calculate the deltas you subtract layer center coordinates from document center coordinate.

Here's the code:

(function()
{

    var startRulerUnits = app.preferences.rulerUnits;
    app.preferences.rulerUnits = Units.PIXELS;

    var docRef = activeDocument;
    var blur = docRef.activeLayer;

    // since we resize based on the initial size of the source layer, 
    // we don't need to get the bounds twice
    var bounds = blur.bounds;
    var height = bounds[3].value - bounds[1].value;
    var width = bounds[2].value - bounds[0].value;

    /////////////////////////////////////////////////////////////////////////////////////
    // Centering the layer

    // Getting center coordinates of the document
    var docCenterW = docRef.width.as("px") / 2;
    var docCenterH = docRef.height.as("px") / 2;

    // getting values to translate the layer. 
    var deltaX = Math.round(docCenterW - (bounds[0].value + width / 2));
    var deltaY = Math.round(docCenterH - (bounds[1].value + height / 2));

    blur.translate(deltaX, deltaY);
    /////////////////////////////////////////////////////////////////////////////////////

    var newdLayer = blur.duplicate();

    // declare 2 different vars for your sizes (there are better ways to do this, but
    // since you say you aren't a JavaScript pro, I figured I'd keep it simple)
    var newSize600 = (100 / height) * 600;
    var newSize900 = (100 / width) * 900;
    // resize your layers
    newdLayer.resize(newSize600, newSize600, AnchorPosition.MIDDLECENTER);
    blur.resize(newSize900, newSize900, AnchorPosition.MIDDLECENTER);
    // apply blur
    blur.applyGaussianBlur(5);

    // below creates the group, moves the layers to it and merges them. Feel free to just include this part
    // at the end of your function if you don't want to use the modified code above. 

    // create a new layer set
    var groupOne = docRef.layerSets.add();

    // move the blur layer inside the layer set and name the layer for posterity
    blur.move(groupOne, ElementPlacement.INSIDE);
    blur.name = "blur";

    // move the newdLayer inside and rename
    newdLayer.move(groupOne, ElementPlacement.INSIDE);
    newdLayer.name = "newdLayer";

    // merge the layer set and name the new layer
    var mergedGroup = groupOne.merge();
    mergedGroup.name = "newdLayer + blur";

    app.preferences.rulerUnits = startRulerUnits;

})();

Upvotes: 3

Related Questions