gpt4 book ai didi

javascript - 如何在 Photoshop 脚本中将图层居中?

转载 作者:行者123 更新时间:2023-12-01 00:43:17 26 4
gpt4 key购买 nike

我的 Photoshop Canvas 是 900X600。

下面的函数采用X层并制作X层副本

它需要X层复制,并在保持比例的同时将高度调整为600px。 var newdLayer

它采用 X 层,在保持比例的同时将宽度调整为 900 像素并应用高斯模糊。 var 模糊

然后合并X层X层副本

问题是,如果X层没有位于脚本开头的中心,那么它就会发生故障。

如何在脚本开头添加中心层到X层

(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;

})();

最佳答案

您需要做的是计算图层中心和文档中心之间的差异,然后根据该差异平移模糊图层,然后再进行复制。

要计算图层中心,请使用 bound[0].valuebound[1].value(从文档左上角到顶部的距离)图层的左 Angular )并添加一半的宽度和一半的高度。然后,要计算增量,请从文档中心坐标中减去图层中心坐标。

代码如下:

(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;

})();

关于javascript - 如何在 Photoshop 脚本中将图层居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57565360/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com