@imageconvertit/mcp-server 0.0.0-stage → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +134 -3
- package/package.json +50 -6
- package/src/authorize.js +86 -0
- package/src/index.js +734 -0
- package/src/lib/batch.js +151 -0
- package/src/lib/cloudAuth.js +177 -0
- package/src/lib/imageOps.js +272 -0
- package/src/lib/rembg.js +38 -0
- package/src/lib/runAi.js +312 -0
- package/src/tools/ai.js +319 -0
- package/src/tools/core.js +7 -0
- package/src/tools/ecommerce.js +607 -0
- package/src/tools/format.js +534 -0
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* E-Commerce & Creators — production MCP tools.
|
|
3
|
+
* Ports website rembg + canvasOps behavior to Node (sharp + @imgly).
|
|
4
|
+
* All batch paths authorize via runFolderBatch / authorizeBatch.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from 'node:fs/promises';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import sharp from 'sharp';
|
|
10
|
+
import { runFolderBatch, outPath } from '../lib/batch.js';
|
|
11
|
+
import { cutoutPngBuffer } from '../lib/rembg.js';
|
|
12
|
+
import {
|
|
13
|
+
parseHex,
|
|
14
|
+
encodeToFile,
|
|
15
|
+
alignCutoutOnCanvas,
|
|
16
|
+
adjustBcs,
|
|
17
|
+
gridPosition,
|
|
18
|
+
escapeXml,
|
|
19
|
+
findContentBottom,
|
|
20
|
+
readOptionalFile,
|
|
21
|
+
normalizeFormat,
|
|
22
|
+
extFor,
|
|
23
|
+
} from '../lib/imageOps.js';
|
|
24
|
+
|
|
25
|
+
/** Bulk Background Remover → transparent PNG */
|
|
26
|
+
export async function batchRemoveBackground(env, args) {
|
|
27
|
+
return runFolderBatch(env, {
|
|
28
|
+
inputDir: args.inputDir,
|
|
29
|
+
outputDir: args.outputDir,
|
|
30
|
+
processOne: async (file, outputDir) => {
|
|
31
|
+
const buf = await cutoutPngBuffer(file);
|
|
32
|
+
const out = outPath(file, outputDir, '.png');
|
|
33
|
+
await fs.writeFile(out, buf);
|
|
34
|
+
return { out };
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Amazon/Shopify White Background
|
|
41
|
+
* Website defaults: 1000 / 2000 / 2400 — MCP accepts those + custom size.
|
|
42
|
+
* Output PNG (marketplace-safe) or JPEG if requested.
|
|
43
|
+
*/
|
|
44
|
+
export async function batchWhiteBackground(env, args) {
|
|
45
|
+
const size = Math.min(4000, Math.max(500, Number(args.size) || 2000));
|
|
46
|
+
const format = normalizeFormat(args.format || 'png');
|
|
47
|
+
const padding = args.padding != null ? Number(args.padding) : 0.05;
|
|
48
|
+
return runFolderBatch(env, {
|
|
49
|
+
inputDir: args.inputDir,
|
|
50
|
+
outputDir: args.outputDir,
|
|
51
|
+
processOne: async (file, outputDir) => {
|
|
52
|
+
const cut = await cutoutPngBuffer(file);
|
|
53
|
+
const buf = await alignCutoutOnCanvas(cut, {
|
|
54
|
+
targetSize: size,
|
|
55
|
+
padding,
|
|
56
|
+
padColor: args.background || '#FFFFFF',
|
|
57
|
+
alignment: 'center',
|
|
58
|
+
format: format === 'jpeg' ? 'jpeg' : 'png',
|
|
59
|
+
quality: args.quality ?? 92,
|
|
60
|
+
});
|
|
61
|
+
const out = outPath(file, outputDir, extFor(format === 'jpeg' ? 'jpeg' : 'png'));
|
|
62
|
+
await fs.writeFile(out, buf);
|
|
63
|
+
return { out };
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Bulk Background Colour Changer — cutout + pad to targetSize */
|
|
69
|
+
export async function batchBackgroundColor(env, args) {
|
|
70
|
+
const size = Math.min(4000, Math.max(256, Number(args.size) || Number(args.targetSize) || 2000));
|
|
71
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
72
|
+
return runFolderBatch(env, {
|
|
73
|
+
inputDir: args.inputDir,
|
|
74
|
+
outputDir: args.outputDir,
|
|
75
|
+
processOne: async (file, outputDir) => {
|
|
76
|
+
const cut = await cutoutPngBuffer(file);
|
|
77
|
+
const buf = await alignCutoutOnCanvas(cut, {
|
|
78
|
+
targetSize: size,
|
|
79
|
+
padding: args.padding != null ? Number(args.padding) : 0.05,
|
|
80
|
+
padColor: args.color || '#FFFFFF',
|
|
81
|
+
alignment: 'center',
|
|
82
|
+
format,
|
|
83
|
+
quality: args.quality ?? 90,
|
|
84
|
+
});
|
|
85
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
86
|
+
await fs.writeFile(out, buf);
|
|
87
|
+
return { out };
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Align Product Photos — REAL bbox crop + pad (website canvas_align_centre).
|
|
94
|
+
* Not an alias of white-background marketing sizes only.
|
|
95
|
+
*/
|
|
96
|
+
export async function batchAlignCenter(env, args) {
|
|
97
|
+
const size = Math.min(4000, Math.max(256, Number(args.size) || Number(args.targetSize) || 1000));
|
|
98
|
+
const format = normalizeFormat(args.format || 'png');
|
|
99
|
+
return runFolderBatch(env, {
|
|
100
|
+
inputDir: args.inputDir,
|
|
101
|
+
outputDir: args.outputDir,
|
|
102
|
+
processOne: async (file, outputDir) => {
|
|
103
|
+
const cut = await cutoutPngBuffer(file);
|
|
104
|
+
const buf = await alignCutoutOnCanvas(cut, {
|
|
105
|
+
targetSize: size,
|
|
106
|
+
padding: args.padding != null ? Number(args.padding) : 0.05,
|
|
107
|
+
padColor: args.background || '#FFFFFF',
|
|
108
|
+
alignment: args.alignment || 'center',
|
|
109
|
+
format: format === 'jpeg' ? 'jpeg' : 'png',
|
|
110
|
+
quality: args.quality ?? 92,
|
|
111
|
+
});
|
|
112
|
+
const out = outPath(file, outputDir, extFor(format === 'jpeg' ? 'jpeg' : 'png'));
|
|
113
|
+
await fs.writeFile(out, buf);
|
|
114
|
+
return { out };
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Add Shadow — drop | floating | long (website canvas_add_shadow).
|
|
121
|
+
*/
|
|
122
|
+
export async function batchAddShadow(env, args) {
|
|
123
|
+
const shadowType = String(args.shadowType || 'drop');
|
|
124
|
+
const intensity = Math.min(100, Math.max(0, Number(args.intensity) ?? 70));
|
|
125
|
+
const blur = Math.min(100, Math.max(0, Number(args.blur) ?? 35));
|
|
126
|
+
const angle = Number(args.angle) || 145;
|
|
127
|
+
const distance = Math.min(50, Math.max(0, Number(args.distance) ?? 12));
|
|
128
|
+
const outputSize = Math.min(4000, Math.max(400, Number(args.outputSize) || 1000));
|
|
129
|
+
const bg = parseHex(args.background || args.bgColor || '#FFFFFF');
|
|
130
|
+
const shadowColor = parseHex(args.shadowColor || '#000000');
|
|
131
|
+
const format = normalizeFormat(args.format || 'png');
|
|
132
|
+
|
|
133
|
+
return runFolderBatch(env, {
|
|
134
|
+
inputDir: args.inputDir,
|
|
135
|
+
outputDir: args.outputDir,
|
|
136
|
+
processOne: async (file, outputDir) => {
|
|
137
|
+
const cut = await cutoutPngBuffer(file);
|
|
138
|
+
const meta = await sharp(cut).metadata();
|
|
139
|
+
const bw = meta.width || 1;
|
|
140
|
+
const bh = meta.height || 1;
|
|
141
|
+
const W = outputSize;
|
|
142
|
+
const H = outputSize;
|
|
143
|
+
const scaleToFit = Math.min((W * 0.76) / bw, (H * 0.76) / bh);
|
|
144
|
+
const pw = Math.max(1, Math.round(bw * scaleToFit));
|
|
145
|
+
const ph = Math.max(1, Math.round(bh * scaleToFit));
|
|
146
|
+
const px = Math.round((W - pw) / 2);
|
|
147
|
+
const py = Math.round((H - ph) / 2);
|
|
148
|
+
const product = await sharp(cut).resize({ width: pw, height: ph, fit: 'fill' }).png().toBuffer();
|
|
149
|
+
|
|
150
|
+
const blurPx = Math.max(2, Math.round((blur / 100) * W * 0.10));
|
|
151
|
+
const distPx = Math.round((distance / 100) * H * 0.55);
|
|
152
|
+
const shadowAlpha = intensity / 100;
|
|
153
|
+
|
|
154
|
+
const silhouette = await sharp(product)
|
|
155
|
+
.ensureAlpha()
|
|
156
|
+
.tint({ r: shadowColor.r, g: shadowColor.g, b: shadowColor.b })
|
|
157
|
+
.modulate({ brightness: 0 })
|
|
158
|
+
.png()
|
|
159
|
+
.toBuffer();
|
|
160
|
+
|
|
161
|
+
const layers = [];
|
|
162
|
+
if (shadowType === 'long') {
|
|
163
|
+
const rad = ((angle - 90) * Math.PI) / 180;
|
|
164
|
+
const totalX = Math.cos(rad) * distPx;
|
|
165
|
+
const totalY = Math.sin(rad) * distPx;
|
|
166
|
+
const steps = 40;
|
|
167
|
+
for (let i = steps; i >= 1; i--) {
|
|
168
|
+
const t = i / steps;
|
|
169
|
+
const faded = await sharp(silhouette)
|
|
170
|
+
.composite([{
|
|
171
|
+
input: Buffer.from([0, 0, 0, Math.round(255 * (shadowAlpha / steps))]),
|
|
172
|
+
raw: { width: 1, height: 1, channels: 4 },
|
|
173
|
+
tile: true,
|
|
174
|
+
blend: 'dest-in',
|
|
175
|
+
}])
|
|
176
|
+
.png()
|
|
177
|
+
.toBuffer();
|
|
178
|
+
layers.push({
|
|
179
|
+
input: faded,
|
|
180
|
+
left: Math.round(px + totalX * t),
|
|
181
|
+
top: Math.round(py + totalY * t),
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
} else if (shadowType === 'floating') {
|
|
185
|
+
const offY = Math.round(ph * 0.12 + distPx * 0.3);
|
|
186
|
+
const soft = await sharp(silhouette)
|
|
187
|
+
.blur(blurPx * 2)
|
|
188
|
+
.composite([{
|
|
189
|
+
input: Buffer.from([0, 0, 0, Math.round(255 * shadowAlpha)]),
|
|
190
|
+
raw: { width: 1, height: 1, channels: 4 },
|
|
191
|
+
tile: true,
|
|
192
|
+
blend: 'dest-in',
|
|
193
|
+
}])
|
|
194
|
+
.png()
|
|
195
|
+
.toBuffer();
|
|
196
|
+
layers.push({ input: soft, left: px, top: py + offY });
|
|
197
|
+
} else {
|
|
198
|
+
const rad = ((angle - 90) * Math.PI) / 180;
|
|
199
|
+
const offX = Math.round(Math.cos(rad) * distPx);
|
|
200
|
+
const offY = Math.round(Math.sin(rad) * distPx);
|
|
201
|
+
const soft = await sharp(silhouette)
|
|
202
|
+
.blur(blurPx)
|
|
203
|
+
.composite([{
|
|
204
|
+
input: Buffer.from([0, 0, 0, Math.round(255 * shadowAlpha)]),
|
|
205
|
+
raw: { width: 1, height: 1, channels: 4 },
|
|
206
|
+
tile: true,
|
|
207
|
+
blend: 'dest-in',
|
|
208
|
+
}])
|
|
209
|
+
.png()
|
|
210
|
+
.toBuffer();
|
|
211
|
+
layers.push({ input: soft, left: px + offX, top: py + offY });
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
layers.push({ input: product, left: px, top: py });
|
|
215
|
+
|
|
216
|
+
// Clamp composite offsets (sharp rejects negatives)
|
|
217
|
+
const safeLayers = [];
|
|
218
|
+
for (const L of layers) {
|
|
219
|
+
const lm = await sharp(L.input).metadata();
|
|
220
|
+
const lw = lm.width || 1;
|
|
221
|
+
const lh = lm.height || 1;
|
|
222
|
+
let left = L.left;
|
|
223
|
+
let top = L.top;
|
|
224
|
+
let extract = null;
|
|
225
|
+
if (left < 0) {
|
|
226
|
+
extract = { ...(extract || {}), left: -left };
|
|
227
|
+
left = 0;
|
|
228
|
+
}
|
|
229
|
+
if (top < 0) {
|
|
230
|
+
extract = { ...(extract || {}), top: -top };
|
|
231
|
+
top = 0;
|
|
232
|
+
}
|
|
233
|
+
let input = L.input;
|
|
234
|
+
if (extract) {
|
|
235
|
+
const el = extract.left || 0;
|
|
236
|
+
const et = extract.top || 0;
|
|
237
|
+
const ew = Math.max(1, lw - el);
|
|
238
|
+
const eh = Math.max(1, lh - et);
|
|
239
|
+
if (left >= W || top >= H) continue;
|
|
240
|
+
input = await sharp(L.input)
|
|
241
|
+
.extract({ left: el, top: et, width: Math.min(ew, W - left), height: Math.min(eh, H - top) })
|
|
242
|
+
.png()
|
|
243
|
+
.toBuffer();
|
|
244
|
+
} else if (left + lw > W || top + lh > H) {
|
|
245
|
+
const ew = Math.min(lw, W - left);
|
|
246
|
+
const eh = Math.min(lh, H - top);
|
|
247
|
+
if (ew < 1 || eh < 1) continue;
|
|
248
|
+
input = await sharp(L.input).extract({ left: 0, top: 0, width: ew, height: eh }).png().toBuffer();
|
|
249
|
+
}
|
|
250
|
+
safeLayers.push({ input, left, top });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const pipeline = sharp({
|
|
254
|
+
create: {
|
|
255
|
+
width: W, height: H, channels: 3,
|
|
256
|
+
background: { r: bg.r, g: bg.g, b: bg.b },
|
|
257
|
+
},
|
|
258
|
+
}).composite(safeLayers);
|
|
259
|
+
|
|
260
|
+
const out = outPath(file, outputDir, extFor(format === 'jpeg' ? 'jpeg' : 'png'));
|
|
261
|
+
await encodeToFile(pipeline, out, format === 'jpeg' ? 'jpeg' : 'png', args.quality ?? 92);
|
|
262
|
+
return { out };
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Blur Background — blur oriented frame; composite cutout without stretching */
|
|
268
|
+
export async function batchBlurBackground(env, args) {
|
|
269
|
+
const sigma = Math.min(40, Math.max(2, Number(args.sigma) || 12));
|
|
270
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
271
|
+
return runFolderBatch(env, {
|
|
272
|
+
inputDir: args.inputDir,
|
|
273
|
+
outputDir: args.outputDir,
|
|
274
|
+
processOne: async (file, outputDir) => {
|
|
275
|
+
const oriented = await sharp(file).rotate().toBuffer({ resolveWithObject: true });
|
|
276
|
+
const W = oriented.info.width || 1;
|
|
277
|
+
const H = oriented.info.height || 1;
|
|
278
|
+
const cut = await cutoutPngBuffer(file);
|
|
279
|
+
const cutMeta = await sharp(cut).metadata();
|
|
280
|
+
let cutNorm = cut;
|
|
281
|
+
// imgly usually returns same size; never stretch with fit:fill (distorts subject)
|
|
282
|
+
if ((cutMeta.width || 0) !== W || (cutMeta.height || 0) !== H) {
|
|
283
|
+
cutNorm = await sharp(cut)
|
|
284
|
+
.resize({ width: W, height: H, fit: 'contain', background: { r: 0, g: 0, b: 0, alpha: 0 } })
|
|
285
|
+
.png()
|
|
286
|
+
.toBuffer();
|
|
287
|
+
}
|
|
288
|
+
const blurred = await sharp(await sharp(file).rotate().toBuffer()).blur(sigma).toBuffer();
|
|
289
|
+
const pipeline = sharp(blurred).composite([{ input: cutNorm, left: 0, top: 0 }]);
|
|
290
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
291
|
+
await encodeToFile(pipeline, out, format, args.quality ?? 90);
|
|
292
|
+
return { out };
|
|
293
|
+
},
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Catalog Watermarking — text tile AND/OR image watermark file.
|
|
299
|
+
*/
|
|
300
|
+
export async function batchWatermark(env, args) {
|
|
301
|
+
const text = String(args.text || 'CONFIDENTIAL').slice(0, 80);
|
|
302
|
+
const opacity = Math.min(1, Math.max(0.05, Number(args.opacity) ?? 0.18));
|
|
303
|
+
const fontSize = Math.min(120, Math.max(12, Number(args.fontSize) || 32));
|
|
304
|
+
const tile = args.tile !== false;
|
|
305
|
+
const color = parseHex(args.color || '#000000');
|
|
306
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
307
|
+
let watermarkImage = null;
|
|
308
|
+
if (args.watermarkPath) {
|
|
309
|
+
watermarkImage = await readOptionalFile(args.watermarkPath);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
return runFolderBatch(env, {
|
|
313
|
+
inputDir: args.inputDir,
|
|
314
|
+
outputDir: args.outputDir,
|
|
315
|
+
processOne: async (file, outputDir) => {
|
|
316
|
+
const meta = await sharp(file).rotate().metadata();
|
|
317
|
+
const w = meta.width || 1;
|
|
318
|
+
const h = meta.height || 1;
|
|
319
|
+
const composites = [];
|
|
320
|
+
|
|
321
|
+
if (watermarkImage) {
|
|
322
|
+
const sizePct = Math.min(40, Math.max(5, Number(args.imageSizePct) || 20));
|
|
323
|
+
const targetW = Math.max(8, Math.round(w * (sizePct / 100)));
|
|
324
|
+
let logo = await sharp(watermarkImage)
|
|
325
|
+
.resize({ width: targetW, withoutEnlargement: true })
|
|
326
|
+
.ensureAlpha()
|
|
327
|
+
.png()
|
|
328
|
+
.toBuffer();
|
|
329
|
+
logo = await sharp(logo)
|
|
330
|
+
.composite([{
|
|
331
|
+
input: Buffer.from([255, 255, 255, Math.round(255 * opacity)]),
|
|
332
|
+
raw: { width: 1, height: 1, channels: 4 },
|
|
333
|
+
tile: true,
|
|
334
|
+
blend: 'dest-in',
|
|
335
|
+
}])
|
|
336
|
+
.png()
|
|
337
|
+
.toBuffer();
|
|
338
|
+
const lm = await sharp(logo).metadata();
|
|
339
|
+
const pos = gridPosition(args.position || 'bottom-right', w, h, lm.width || 1, lm.height || 1, args.padding ?? 16);
|
|
340
|
+
composites.push({ input: logo, ...pos });
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (!watermarkImage || args.text) {
|
|
344
|
+
let svg;
|
|
345
|
+
if (tile) {
|
|
346
|
+
const step = fontSize * 6;
|
|
347
|
+
const nodes = [];
|
|
348
|
+
for (let tileY = -step; tileY < h + step; tileY += step) {
|
|
349
|
+
for (let tileX = -step; tileX < w + step; tileX += step) {
|
|
350
|
+
nodes.push(
|
|
351
|
+
`<text x="${tileX}" y="${tileY}" fill="rgba(${color.r},${color.g},${color.b},${opacity})" font-size="${fontSize}" font-family="Arial,sans-serif" font-weight="700" transform="rotate(-45 ${tileX} ${tileY})" text-anchor="middle">${escapeXml(text)}</text>`,
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
svg = `<svg width="${w}" height="${h}" xmlns="http://www.w3.org/2000/svg">${nodes.join('')}</svg>`;
|
|
356
|
+
} else {
|
|
357
|
+
const pos = gridPosition(args.position || 'bottom-right', w, h, fontSize * text.length * 0.55, fontSize, args.padding ?? 24);
|
|
358
|
+
svg = `<svg width="${w}" height="${h}" xmlns="http://www.w3.org/2000/svg">
|
|
359
|
+
<text x="${pos.left}" y="${pos.top + fontSize}" fill="rgba(${color.r},${color.g},${color.b},${opacity})" font-size="${fontSize}" font-family="Arial,sans-serif" font-weight="700">${escapeXml(text)}</text>
|
|
360
|
+
</svg>`;
|
|
361
|
+
}
|
|
362
|
+
composites.push({ input: Buffer.from(svg), top: 0, left: 0 });
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const pipeline = sharp(file).rotate().composite(composites);
|
|
366
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
367
|
+
await encodeToFile(pipeline, out, format, args.quality ?? 90);
|
|
368
|
+
return { out };
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Add Text — 9-grid + optional background pill */
|
|
374
|
+
export async function batchTextOverlay(env, args) {
|
|
375
|
+
const text = String(args.text || '').slice(0, 200);
|
|
376
|
+
if (!text) return { ok: false, error: 'text is required.', processed: 0 };
|
|
377
|
+
const fontSize = Math.min(200, Math.max(10, Number(args.fontSize) || 48));
|
|
378
|
+
const opacity = Math.min(1, Math.max(0.1, Number(args.opacity) || 1));
|
|
379
|
+
const color = parseHex(args.color || '#FFFFFF');
|
|
380
|
+
const bgBox = args.backgroundBox ? parseHex(args.backgroundBox) : null;
|
|
381
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
382
|
+
const position = args.position || 'bottom-center';
|
|
383
|
+
|
|
384
|
+
return runFolderBatch(env, {
|
|
385
|
+
inputDir: args.inputDir,
|
|
386
|
+
outputDir: args.outputDir,
|
|
387
|
+
processOne: async (file, outputDir) => {
|
|
388
|
+
const meta = await sharp(file).rotate().metadata();
|
|
389
|
+
const w = meta.width || 1;
|
|
390
|
+
const h = meta.height || 1;
|
|
391
|
+
const approxW = Math.min(w - 16, Math.round(fontSize * text.length * 0.55));
|
|
392
|
+
const approxH = Math.round(fontSize * 1.4);
|
|
393
|
+
const pos = gridPosition(position, w, h, approxW, approxH, args.padding ?? 24);
|
|
394
|
+
const box = bgBox
|
|
395
|
+
? `<rect x="${pos.left - 8}" y="${pos.top - 8}" width="${approxW + 16}" height="${approxH + 8}" rx="8" fill="rgba(${bgBox.r},${bgBox.g},${bgBox.b},0.65)"/>`
|
|
396
|
+
: '';
|
|
397
|
+
const svg = `<svg width="${w}" height="${h}" xmlns="http://www.w3.org/2000/svg">
|
|
398
|
+
${box}
|
|
399
|
+
<text x="${pos.left}" y="${pos.top + fontSize}" fill="rgba(${color.r},${color.g},${color.b},${opacity})" font-size="${fontSize}" font-family="Arial,Helvetica,sans-serif" font-weight="700">${escapeXml(text)}</text>
|
|
400
|
+
</svg>`;
|
|
401
|
+
const pipeline = sharp(file).rotate().composite([{ input: Buffer.from(svg), top: 0, left: 0 }]);
|
|
402
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
403
|
+
await encodeToFile(pipeline, out, format, args.quality ?? 90);
|
|
404
|
+
return { out };
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** Adjust — brightness/contrast/saturation (+ optional hue via sharp, + sharpen) */
|
|
410
|
+
export async function batchAdjust(env, args) {
|
|
411
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
412
|
+
return runFolderBatch(env, {
|
|
413
|
+
inputDir: args.inputDir,
|
|
414
|
+
outputDir: args.outputDir,
|
|
415
|
+
processOne: async (file, outputDir) => {
|
|
416
|
+
let buf = await sharp(file).rotate().png().toBuffer();
|
|
417
|
+
buf = await adjustBcs(buf, {
|
|
418
|
+
brightness: Number(args.brightness) || 0,
|
|
419
|
+
contrast: Number(args.contrast) || 0,
|
|
420
|
+
saturation: Number(args.saturation) || 0,
|
|
421
|
+
});
|
|
422
|
+
let pipeline = sharp(buf);
|
|
423
|
+
if (Number.isFinite(Number(args.hue)) && Number(args.hue) !== 0) {
|
|
424
|
+
pipeline = pipeline.modulate({ hue: Number(args.hue) });
|
|
425
|
+
}
|
|
426
|
+
if (args.sharpen) pipeline = pipeline.sharpen();
|
|
427
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
428
|
+
await encodeToFile(pipeline, out, format, args.quality ?? 90);
|
|
429
|
+
return { out };
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Logo overlay — local logoPath, 9-grid, opacity 0–100 like website */
|
|
435
|
+
export async function batchLogoOverlay(env, args) {
|
|
436
|
+
let logoPath;
|
|
437
|
+
try {
|
|
438
|
+
logoPath = await readOptionalFile(args.logoPath);
|
|
439
|
+
} catch {
|
|
440
|
+
return { ok: false, error: `logoPath not found: ${args.logoPath}`, processed: 0 };
|
|
441
|
+
}
|
|
442
|
+
if (!logoPath) return { ok: false, error: 'logoPath (absolute path) is required.', processed: 0 };
|
|
443
|
+
|
|
444
|
+
const sizePct = Math.min(40, Math.max(5, Number(args.sizePct) || 20));
|
|
445
|
+
// Website uses 10–100 percent; also accept 0–1 fractions from agents
|
|
446
|
+
const rawOp = args.opacity == null ? 80 : Number(args.opacity);
|
|
447
|
+
const opacity = rawOp <= 1
|
|
448
|
+
? Math.min(1, Math.max(0.1, rawOp))
|
|
449
|
+
: Math.min(1, Math.max(0.1, rawOp / 100));
|
|
450
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
451
|
+
|
|
452
|
+
return runFolderBatch(env, {
|
|
453
|
+
inputDir: args.inputDir,
|
|
454
|
+
outputDir: args.outputDir,
|
|
455
|
+
processOne: async (file, outputDir) => {
|
|
456
|
+
const meta = await sharp(file).rotate().metadata();
|
|
457
|
+
const w = meta.width || 1;
|
|
458
|
+
const h = meta.height || 1;
|
|
459
|
+
const targetW = Math.max(8, Math.round(w * (sizePct / 100)));
|
|
460
|
+
let logo = await sharp(logoPath)
|
|
461
|
+
.resize({ width: targetW, withoutEnlargement: true })
|
|
462
|
+
.ensureAlpha()
|
|
463
|
+
.png()
|
|
464
|
+
.toBuffer();
|
|
465
|
+
if (opacity < 1) {
|
|
466
|
+
logo = await sharp(logo)
|
|
467
|
+
.composite([{
|
|
468
|
+
input: Buffer.from([255, 255, 255, Math.round(255 * opacity)]),
|
|
469
|
+
raw: { width: 1, height: 1, channels: 4 },
|
|
470
|
+
tile: true,
|
|
471
|
+
blend: 'dest-in',
|
|
472
|
+
}])
|
|
473
|
+
.png()
|
|
474
|
+
.toBuffer();
|
|
475
|
+
}
|
|
476
|
+
const lm = await sharp(logo).metadata();
|
|
477
|
+
const pos = gridPosition(args.position || 'bottom-right', w, h, lm.width || 1, lm.height || 1, args.padding ?? 16);
|
|
478
|
+
const pipeline = sharp(file).rotate().composite([{ input: logo, ...pos }]);
|
|
479
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
480
|
+
await encodeToFile(pipeline, out, format, args.quality ?? 90);
|
|
481
|
+
return { out };
|
|
482
|
+
},
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Reflections — website-style content-bottom + gradient fade */
|
|
487
|
+
export async function batchReflection(env, args) {
|
|
488
|
+
const intensity = Math.min(100, Math.max(10, Number(args.intensity) ?? 60)) / 100;
|
|
489
|
+
const heightPct = Math.min(80, Math.max(10, Number(args.height) ?? Number(args.heightPct) ?? 40));
|
|
490
|
+
const offset = Math.max(0, Number(args.offset) ?? Number(args.gap) ?? 4);
|
|
491
|
+
const bg = parseHex(args.background || args.padColor || '#FFFFFF');
|
|
492
|
+
const format = normalizeFormat(args.format || 'png');
|
|
493
|
+
|
|
494
|
+
return runFolderBatch(env, {
|
|
495
|
+
inputDir: args.inputDir,
|
|
496
|
+
outputDir: args.outputDir,
|
|
497
|
+
processOne: async (file, outputDir) => {
|
|
498
|
+
// Prefer cutout so reflection anchors to product; fall back to original
|
|
499
|
+
let basePng;
|
|
500
|
+
try {
|
|
501
|
+
basePng = await cutoutPngBuffer(file);
|
|
502
|
+
} catch {
|
|
503
|
+
basePng = await sharp(file).rotate().ensureAlpha().png().toBuffer();
|
|
504
|
+
}
|
|
505
|
+
const meta = await sharp(basePng).metadata();
|
|
506
|
+
const srcW = meta.width || 1;
|
|
507
|
+
const srcH = meta.height || 1;
|
|
508
|
+
const contentBottom = await findContentBottom(basePng);
|
|
509
|
+
const contentH = contentBottom + 1;
|
|
510
|
+
const reflH = Math.max(1, Math.round(contentH * heightPct / 100));
|
|
511
|
+
const totalH = contentBottom + 1 + offset + reflH;
|
|
512
|
+
const outSize = Math.max(srcW, totalH);
|
|
513
|
+
const xOff = Math.round((outSize - srcW) / 2);
|
|
514
|
+
|
|
515
|
+
const flipped = await sharp(basePng).flip().png().toBuffer();
|
|
516
|
+
const flipSrcY = Math.max(0, srcH - 1 - contentBottom);
|
|
517
|
+
const safeReflH = Math.min(reflH, srcH - flipSrcY);
|
|
518
|
+
let reflection = await sharp(flipped)
|
|
519
|
+
.extract({ left: 0, top: flipSrcY, width: srcW, height: safeReflH })
|
|
520
|
+
.ensureAlpha()
|
|
521
|
+
.png()
|
|
522
|
+
.toBuffer();
|
|
523
|
+
|
|
524
|
+
// Gradient fade via SVG mask approx: multiply alpha by linear gradient
|
|
525
|
+
const fadeSvg = Buffer.from(`<svg width="${srcW}" height="${safeReflH}" xmlns="http://www.w3.org/2000/svg">
|
|
526
|
+
<defs><linearGradient id="g" x1="0" y1="0" x2="0" y2="1">
|
|
527
|
+
<stop offset="0%" stop-color="white" stop-opacity="${intensity}"/>
|
|
528
|
+
<stop offset="40%" stop-color="white" stop-opacity="${intensity * 0.85}"/>
|
|
529
|
+
<stop offset="75%" stop-color="white" stop-opacity="${intensity * 0.35}"/>
|
|
530
|
+
<stop offset="100%" stop-color="white" stop-opacity="0"/>
|
|
531
|
+
</linearGradient></defs>
|
|
532
|
+
<rect width="100%" height="100%" fill="url(#g)"/>
|
|
533
|
+
</svg>`);
|
|
534
|
+
reflection = await sharp(reflection)
|
|
535
|
+
.composite([{ input: fadeSvg, blend: 'dest-in' }])
|
|
536
|
+
.png()
|
|
537
|
+
.toBuffer();
|
|
538
|
+
|
|
539
|
+
const pipeline = sharp({
|
|
540
|
+
create: {
|
|
541
|
+
width: outSize, height: outSize, channels: 3,
|
|
542
|
+
background: { r: bg.r, g: bg.g, b: bg.b },
|
|
543
|
+
},
|
|
544
|
+
}).composite([
|
|
545
|
+
{ input: basePng, left: xOff, top: 0 },
|
|
546
|
+
{ input: reflection, left: xOff, top: contentBottom + 1 + offset },
|
|
547
|
+
]);
|
|
548
|
+
|
|
549
|
+
const out = outPath(file, outputDir, extFor(format === 'jpeg' ? 'jpeg' : 'png'));
|
|
550
|
+
await encodeToFile(pipeline, out, format === 'jpeg' ? 'jpeg' : 'png', args.quality ?? 92);
|
|
551
|
+
return { out };
|
|
552
|
+
},
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** Barcode & QR */
|
|
557
|
+
export async function generateBarcodeQr(env, args) {
|
|
558
|
+
const { authorizeBatch } = await import('../authorize.js');
|
|
559
|
+
let bwipjs;
|
|
560
|
+
try {
|
|
561
|
+
bwipjs = (await import('bwip-js')).default;
|
|
562
|
+
} catch {
|
|
563
|
+
return { ok: false, error: 'bwip-js missing — reinstall package dependencies.', processed: 0 };
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const values = Array.isArray(args.values) && args.values.length
|
|
567
|
+
? args.values.map(String)
|
|
568
|
+
: [String(args.value || '')].filter(Boolean);
|
|
569
|
+
if (!values.length) return { ok: false, error: 'value or values[] required.', processed: 0 };
|
|
570
|
+
if (values.length > 50) return { ok: false, error: 'Max 50 codes per call.', processed: 0 };
|
|
571
|
+
|
|
572
|
+
const auth = await authorizeBatch({
|
|
573
|
+
licenseKey: env.ICI_LICENSE_KEY,
|
|
574
|
+
fileCount: values.length,
|
|
575
|
+
apiBase: env.ICI_API_BASE,
|
|
576
|
+
});
|
|
577
|
+
if (!auth.allowed) {
|
|
578
|
+
return { ok: false, error: auth.error, paywall: true, processed: 0, remaining: auth.remaining };
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
const outputDir = path.resolve(args.outputDir);
|
|
582
|
+
await fs.mkdir(outputDir, { recursive: true });
|
|
583
|
+
const bcid = String(args.type || 'qrcode');
|
|
584
|
+
const scale = Math.min(10, Math.max(1, Number(args.scale) || 4));
|
|
585
|
+
const outputs = [];
|
|
586
|
+
for (let i = 0; i < values.length; i++) {
|
|
587
|
+
const text = values[i];
|
|
588
|
+
const png = await bwipjs.toBuffer({
|
|
589
|
+
bcid,
|
|
590
|
+
text,
|
|
591
|
+
scale,
|
|
592
|
+
includetext: args.includeText !== false && bcid !== 'qrcode',
|
|
593
|
+
});
|
|
594
|
+
const safe = text.replace(/[^\w.-]+/g, '_').slice(0, 40) || `code-${i + 1}`;
|
|
595
|
+
const out = path.join(outputDir, `${safe}.png`);
|
|
596
|
+
await fs.writeFile(out, png);
|
|
597
|
+
outputs.push(out);
|
|
598
|
+
}
|
|
599
|
+
return {
|
|
600
|
+
ok: true,
|
|
601
|
+
processed: outputs.length,
|
|
602
|
+
outputs,
|
|
603
|
+
remaining: auth.remaining,
|
|
604
|
+
limit: auth.limit,
|
|
605
|
+
plan: auth.plan,
|
|
606
|
+
};
|
|
607
|
+
}
|