画布尺寸修改插件
This commit is contained in:
1 parent
6a800280d4
commit
d2d5d747df
1 file changed
+171
@@ -0,0 +1,171 @@
|
||||
#target photoshop
|
||||
|
||||
/*
|
||||
Uniformly resize the current canvas area.
|
||||
Scale range: 0.1 to 5
|
||||
Default scale: 2
|
||||
|
||||
Layer contents are not scaled. The canvas expands or crops
|
||||
equally from its center according to the entered scale.
|
||||
*/
|
||||
|
||||
(function () {
|
||||
function showMessage(text) {
|
||||
alert(text);
|
||||
}
|
||||
|
||||
if (app.documents.length === 0) {
|
||||
showMessage("请先打开一个文档。");
|
||||
return;
|
||||
}
|
||||
|
||||
var document = app.activeDocument;
|
||||
var currentWidth = Math.round(document.width.as("px"));
|
||||
var currentHeight = Math.round(document.height.as("px"));
|
||||
|
||||
var dialog = new Window("dialog", "修改画布大小");
|
||||
dialog.orientation = "column";
|
||||
dialog.alignChildren = "fill";
|
||||
|
||||
// Method 1: uniformly resize with a scale slider and numeric input.
|
||||
var scalePanel = dialog.add("panel", undefined, "方式一:等比例缩放");
|
||||
scalePanel.orientation = "column";
|
||||
scalePanel.alignChildren = "fill";
|
||||
|
||||
var scaleMode = scalePanel.add("radiobutton", undefined, "使用缩放倍率");
|
||||
scaleMode.value = true;
|
||||
|
||||
var scaleGroup = scalePanel.add("group");
|
||||
scaleGroup.orientation = "row";
|
||||
|
||||
var scaleSlider = scaleGroup.add("slider", undefined, 2, 0.1, 5);
|
||||
scaleSlider.preferredSize.width = 220;
|
||||
|
||||
var scaleInput = scaleGroup.add("edittext", undefined, "2");
|
||||
scaleInput.characters = 6;
|
||||
|
||||
scaleGroup.add("statictext", undefined, "倍(0.1 - 5)");
|
||||
|
||||
// Method 2: enter the target canvas width and height independently.
|
||||
var sizePanel = dialog.add("panel", undefined, "方式二:指定宽高");
|
||||
sizePanel.orientation = "column";
|
||||
sizePanel.alignChildren = "left";
|
||||
|
||||
var sizeMode = sizePanel.add("radiobutton", undefined, "使用指定宽度和高度");
|
||||
|
||||
var widthGroup = sizePanel.add("group");
|
||||
widthGroup.orientation = "row";
|
||||
widthGroup.add("statictext", undefined, "宽度:");
|
||||
var widthInput = widthGroup.add("edittext", undefined, String(currentWidth));
|
||||
widthInput.characters = 10;
|
||||
widthGroup.add("statictext", undefined, "px");
|
||||
|
||||
var heightGroup = sizePanel.add("group");
|
||||
heightGroup.orientation = "row";
|
||||
heightGroup.add("statictext", undefined, "高度:");
|
||||
var heightInput = heightGroup.add("edittext", undefined, String(currentHeight));
|
||||
heightInput.characters = 10;
|
||||
heightGroup.add("statictext", undefined, "px");
|
||||
|
||||
var buttonGroup = dialog.add("group");
|
||||
buttonGroup.alignment = "right";
|
||||
var cancelButton = buttonGroup.add("button", undefined, "取消", { name: "cancel" });
|
||||
var okButton = buttonGroup.add("button", undefined, "确定", { name: "ok" });
|
||||
|
||||
var resultWidth = null;
|
||||
var resultHeight = null;
|
||||
|
||||
function parseNumber(text) {
|
||||
return parseFloat(text.replace(",", "."));
|
||||
}
|
||||
|
||||
function formatScale(value) {
|
||||
return String(Math.round(value * 100) / 100);
|
||||
}
|
||||
|
||||
function updateControlState() {
|
||||
scaleSlider.enabled = scaleMode.value;
|
||||
scaleInput.enabled = scaleMode.value;
|
||||
widthInput.enabled = sizeMode.value;
|
||||
heightInput.enabled = sizeMode.value;
|
||||
}
|
||||
|
||||
scaleMode.onClick = function () {
|
||||
scaleMode.value = true;
|
||||
sizeMode.value = false;
|
||||
updateControlState();
|
||||
};
|
||||
|
||||
sizeMode.onClick = function () {
|
||||
sizeMode.value = true;
|
||||
scaleMode.value = false;
|
||||
updateControlState();
|
||||
};
|
||||
|
||||
scaleSlider.onChanging = function () {
|
||||
scaleInput.text = formatScale(scaleSlider.value);
|
||||
};
|
||||
|
||||
scaleInput.onChange = function () {
|
||||
var value = parseNumber(scaleInput.text);
|
||||
if (!isNaN(value) && value >= 0.1 && value <= 5) {
|
||||
scaleSlider.value = value;
|
||||
scaleInput.text = formatScale(value);
|
||||
}
|
||||
};
|
||||
|
||||
okButton.onClick = function () {
|
||||
if (scaleMode.value) {
|
||||
var scaleValue = parseNumber(scaleInput.text);
|
||||
|
||||
if (isNaN(scaleValue) || scaleValue < 0.1 || scaleValue > 5) {
|
||||
showMessage("请输入 0.1 到 5 之间的缩放倍率。");
|
||||
scaleInput.active = true;
|
||||
return;
|
||||
}
|
||||
|
||||
resultWidth = Math.max(1, Math.round(currentWidth * scaleValue));
|
||||
resultHeight = Math.max(1, Math.round(currentHeight * scaleValue));
|
||||
} else {
|
||||
var widthValue = parseNumber(widthInput.text);
|
||||
var heightValue = parseNumber(heightInput.text);
|
||||
|
||||
if (isNaN(widthValue) || widthValue <= 0) {
|
||||
showMessage("请输入有效的画布宽度。");
|
||||
widthInput.active = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (isNaN(heightValue) || heightValue <= 0) {
|
||||
showMessage("请输入有效的画布高度。");
|
||||
heightInput.active = true;
|
||||
return;
|
||||
}
|
||||
|
||||
resultWidth = Math.round(widthValue);
|
||||
resultHeight = Math.round(heightValue);
|
||||
}
|
||||
|
||||
dialog.close(1);
|
||||
};
|
||||
|
||||
cancelButton.onClick = function () {
|
||||
dialog.close(0);
|
||||
};
|
||||
|
||||
updateControlState();
|
||||
|
||||
if (dialog.show() !== 1 || resultWidth === null || resultHeight === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
document.resizeCanvas(
|
||||
new UnitValue(resultWidth, "px"),
|
||||
new UnitValue(resultHeight, "px"),
|
||||
AnchorPosition.MIDDLECENTER
|
||||
);
|
||||
} catch (error) {
|
||||
showMessage("修改画布失败:\n" + error.message);
|
||||
}
|
||||
}());
|
||||
Reference in new issue
Block a user