-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path批量蒙板.jsx
182 lines (152 loc) · 5.82 KB
/
批量蒙板.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#target illustrator
var doc = app.activeDocument;
var sel = doc.selection;
if (sel.length == 0) {
alert("请选择至少一个对象!");
} else {
main();
}
function main() {
var validItems = getValidItems(sel);
if (validItems.length == 0) {
alert("没有找到可以创建剪切蒙版的对象!");
return;
}
var images = File.openDialog("选择图片", "*.jpg;*.jpeg;*.png;*.gif", true);
if (images == null || images.length == 0) return;
var itemRatios = calculateItemRatios(validItems);
var imageRatios = calculateImageRatios(images);
// 创建进度条
var progressBar = new ProgressBar("处理进度", validItems.length);
matchAndCreateMasks(validItems, images, itemRatios, imageRatios, progressBar);
// 关闭进度条
progressBar.close();
alert("操作完成!");
}
function getValidItems(selection) {
var validItems = [];
for (var i = 0; i < selection.length; i++) {
var item = selection[i];
if (item.typename == "PathItem" || item.typename == "CompoundPathItem" ||
(item.typename == "GroupItem" && item.clipped)) {
validItems.push(item);
}
}
return validItems;
}
function calculateItemRatios(items) {
var ratios = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
var bounds = item.geometricBounds;
ratios.push((bounds[2] - bounds[0]) / (bounds[3] - bounds[1]));
}
return ratios;
}
function calculateImageRatios(images) {
var ratios = [];
for (var i = 0; i < images.length; i++) {
var image = images[i];
var tempItem = doc.placedItems.add();
tempItem.file = image;
ratios.push(tempItem.width / tempItem.height);
tempItem.remove();
}
return ratios;
}
function matchAndCreateMasks(items, images, itemRatios, imageRatios, progressBar) {
var usedImages = [];
var itemIndices = [];
for (var i = 0; i < items.length; i++) {
itemIndices.push(i);
}
var processedCount = 0;
while (itemIndices.length > 0 && images.length > 0) {
var bestMatch = findBestMatch(itemRatios, imageRatios);
if (bestMatch.itemIndex !== -1 && bestMatch.imageIndex !== -1) {
createClippingMask(items[itemIndices[bestMatch.itemIndex]], images[bestMatch.imageIndex]);
usedImages.push(images[bestMatch.imageIndex]);
itemIndices.splice(bestMatch.itemIndex, 1);
itemRatios.splice(bestMatch.itemIndex, 1);
images.splice(bestMatch.imageIndex, 1);
imageRatios.splice(bestMatch.imageIndex, 1);
// 更新进度条
processedCount++;
progressBar.update(processedCount);
} else {
break;
}
}
return usedImages;
}
function findBestMatch(itemRatios, imageRatios) {
var bestItemIndex = -1;
var bestImageIndex = -1;
var minDiff = Infinity;
for (var i = 0; i < itemRatios.length; i++) {
for (var j = 0; j < imageRatios.length; j++) {
var diff = Math.abs(itemRatios[i] - imageRatios[j]);
if (diff < minDiff) {
minDiff = diff;
bestItemIndex = i;
bestImageIndex = j;
}
}
}
return { itemIndex: bestItemIndex, imageIndex: bestImageIndex };
}
function createClippingMask(item, image) {
var placedItem = doc.placedItems.add();
placedItem.file = image;
// 获取路径的边界
var pathBounds = item.geometricBounds;
var pathWidth = pathBounds[2] - pathBounds[0];
var pathHeight = pathBounds[3] - pathBounds[1];
// 获取图片的边界
var imageBounds = placedItem.geometricBounds;
var imageWidth = imageBounds[2] - imageBounds[0];
var imageHeight = imageBounds[3] - imageBounds[1];
// 计算缩放比例以使图片的较短边适应路径
var widthRatio = pathWidth / imageWidth;
var heightRatio = pathHeight / imageHeight;
var scaleRatio = Math.max(widthRatio, heightRatio);
// 使用正确的缩放因子调整放置项目的大小
placedItem.resize(scaleRatio * 100, scaleRatio * 100, true, true, true, true, scaleRatio * 100, Transformation.DOCUMENTORIGIN);
// 将图片居中放置在路径内
var newImageBounds = placedItem.geometricBounds;
var newImageWidth = newImageBounds[2] - newImageBounds[0];
var newImageHeight = newImageBounds[3] - newImageBounds[1];
var deltaX = (pathWidth - newImageWidth) / 2;
var deltaY = (pathHeight - newImageHeight) / 2;
placedItem.position = [pathBounds[0] + deltaX, pathBounds[1] + deltaY];
if (item.typename === "GroupItem" && item.clipped) {
// 如果是现有的剪切组,替换原来的图片
var oldImage = item.pageItems[item.pageItems.length - 1];
if (oldImage.typename === "PlacedItem") {
oldImage.remove();
}
placedItem.move(item, ElementPlacement.PLACEATEND);
} else {
// 对于复合路径和普通路径
placedItem.move(item, ElementPlacement.PLACEBEFORE);
item.zOrder(ZOrderMethod.BRINGTOFRONT);
app.selection = null;
item.selected = true;
placedItem.selected = true;
app.executeMenuCommand('makeMask');
}
}
// 进度条类
function ProgressBar(title, max) {
this.win = new Window("palette", title, undefined, {closeButton: false});
this.win.progressBar = this.win.add("progressbar", undefined, 0, max);
this.win.progressBar.preferredSize.width = 300;
this.win.show();
this.update = function(val) {
this.win.progressBar.value = val;
this.win.update();
}
this.close = function() {
this.win.close();
}
}