Photoshop脚本计算问题

问题描述 投票:0回答:1

下面的代码缩小了图像层的大小(即画布的整个宽度/高度)以允许边框。 borderWidth是根据canvasHeight的百分比计算的。 newWidth 是原始宽度减去 borderWidth * 2。 newHeight 根据原始画布比例进行缩放。

例如。填充 1000 像素宽 x 3000 像素高、边框宽度为 150 像素 (3000 x 0.05) 的画布的图像层将缩小到 700 像素宽 x 2100 像素高。

问题是宽度减少了canvasWidth的百分比,而不是指定的canvasHeight的百分比。

我觉得这与将标尺单位设置为百分比有关。但是,如果不这样做,调整大小命令将不起作用。

我尝试在脚本的不同区域设置 PERCENT 和 PIXELS,但没有成功。

我也开始思考我引用大小的方式是一个问题,所以我尝试使用 layer.bounds 来获取图层大小,而不是 doc.width 和 doc.height。没有区别。

我很茫然。

var doc = app.activeDocument;
var layer = doc.activeLayer;
app.preferences.rulerUnits = Units.PERCENT; 
var canvasWidth = doc.width;
var canvasHeight = doc.height;
var borderWidth = canvasHeight * 0.05;
var newWidth = canvasWidth - (2 * borderWidth);
var newHeight = newWidth * (canvasHeight/canvasWidth);

layer.resize(newWidth, newHeight, AnchorPosition.MIDDLECENTER);
javascript photoshop photoshop-script
1个回答
0
投票

如果您查看文档,您会发现图层调整大小是一个百分比,无论单位设置为何。

所以你需要计算比例百分比:

var scalePercent = (newWidth/canvasWidth)*100;

然后调整大小:

layer.resize(scalePercent, scalePercent, AnchorPosition.MIDDLECENTER);

或者整个事情:

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

var doc = app.activeDocument;
var res = doc.resolution; // not used, but otherwise useful
var layer = doc.activeLayer;
var canvasWidth = doc.width.value;
var canvasHeight = doc.height.value;
var borderWidth = canvasHeight * 0.05;
var newWidth = canvasWidth - (2 * borderWidth);
var newHeight = newWidth * (canvasHeight/canvasWidth);
var scalePercent = (newWidth/canvasWidth)*100;
layer.resize(scalePercent, scalePercent, AnchorPosition.MIDDLECENTER);

// Put the units back to how they were
app.preferences.rulerUnits = startUnits;

这会将 1000 x 300 像素的图层大小调整为 700 x 2100 像素。

© www.soinside.com 2019 - 2024. All rights reserved.