@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,534 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format & conversion MCP tools — local sharp (+ optional heic / pdf-lib).
|
|
3
|
+
* Strict Automation authorize via runFolderBatch.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs/promises';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import sharp from 'sharp';
|
|
9
|
+
import { runFolderBatch, outPath, parseHexColor } from '../lib/batch.js';
|
|
10
|
+
|
|
11
|
+
const FORMATS = new Set(['jpeg', 'jpg', 'png', 'webp', 'tiff', 'avif']);
|
|
12
|
+
|
|
13
|
+
function normalizeFormat(fmt) {
|
|
14
|
+
const f = String(fmt || 'jpeg').toLowerCase();
|
|
15
|
+
if (f === 'jpg') return 'jpeg';
|
|
16
|
+
return FORMATS.has(f) ? f : 'jpeg';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function extFor(fmt) {
|
|
20
|
+
const f = normalizeFormat(fmt);
|
|
21
|
+
return f === 'jpeg' ? '.jpg' : `.${f}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function encodeSharp(pipeline, format, quality) {
|
|
25
|
+
const f = normalizeFormat(format);
|
|
26
|
+
const q = Math.min(100, Math.max(40, Number(quality) || 85));
|
|
27
|
+
if (f === 'png') return pipeline.png();
|
|
28
|
+
if (f === 'webp') return pipeline.webp({ quality: q });
|
|
29
|
+
if (f === 'tiff') return pipeline.tiff();
|
|
30
|
+
if (f === 'avif') return pipeline.avif({ quality: q });
|
|
31
|
+
return pipeline.jpeg({ quality: q, mozjpeg: true });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Bulk Image Resizer + Compressor (combined ops) */
|
|
35
|
+
export async function batchResize(env, args) {
|
|
36
|
+
const longestSide = args.longestSide != null
|
|
37
|
+
? Math.min(8000, Math.max(16, Number(args.longestSide)))
|
|
38
|
+
: null;
|
|
39
|
+
const width = args.width != null ? Math.min(8000, Math.max(1, Number(args.width))) : null;
|
|
40
|
+
const height = args.height != null ? Math.min(8000, Math.max(1, Number(args.height))) : null;
|
|
41
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
42
|
+
const quality = args.quality;
|
|
43
|
+
|
|
44
|
+
return runFolderBatch(env, {
|
|
45
|
+
inputDir: args.inputDir,
|
|
46
|
+
outputDir: args.outputDir,
|
|
47
|
+
processOne: async (file, outputDir) => {
|
|
48
|
+
let pipeline = sharp(file).rotate();
|
|
49
|
+
if (width || height) {
|
|
50
|
+
pipeline = pipeline.resize({
|
|
51
|
+
width: width || undefined,
|
|
52
|
+
height: height || undefined,
|
|
53
|
+
fit: args.fit || 'inside',
|
|
54
|
+
withoutEnlargement: args.withoutEnlargement !== false,
|
|
55
|
+
});
|
|
56
|
+
} else if (longestSide) {
|
|
57
|
+
pipeline = pipeline.resize({
|
|
58
|
+
width: longestSide,
|
|
59
|
+
height: longestSide,
|
|
60
|
+
fit: 'inside',
|
|
61
|
+
withoutEnlargement: args.withoutEnlargement !== false,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
pipeline = await encodeSharp(pipeline, format, quality);
|
|
65
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
66
|
+
await pipeline.toFile(out);
|
|
67
|
+
return { out };
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Bulk Image Compressor — recompress; preserves PNG/WebP when format omitted */
|
|
73
|
+
export async function batchCompress(env, args) {
|
|
74
|
+
return runFolderBatch(env, {
|
|
75
|
+
inputDir: args.inputDir,
|
|
76
|
+
outputDir: args.outputDir,
|
|
77
|
+
processOne: async (file, outputDir) => {
|
|
78
|
+
const meta = await sharp(file).rotate().metadata();
|
|
79
|
+
let format = args.format ? normalizeFormat(args.format) : null;
|
|
80
|
+
if (!format) {
|
|
81
|
+
const fmt = String(meta.format || '').toLowerCase();
|
|
82
|
+
if (fmt === 'png' || meta.hasAlpha) format = 'png';
|
|
83
|
+
else if (fmt === 'webp') format = 'webp';
|
|
84
|
+
else format = 'jpeg';
|
|
85
|
+
}
|
|
86
|
+
const quality = args.quality ?? (format === 'png' ? 90 : 72);
|
|
87
|
+
let pipeline = sharp(file).rotate();
|
|
88
|
+
pipeline = await encodeSharp(pipeline, format, quality);
|
|
89
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
90
|
+
await pipeline.toFile(out);
|
|
91
|
+
return { out };
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Bulk Image Cropper — EXIF-orient first, then crop in oriented pixels */
|
|
97
|
+
export async function batchCrop(env, args) {
|
|
98
|
+
const left = Math.max(0, Number(args.left) || 0);
|
|
99
|
+
const top = Math.max(0, Number(args.top) || 0);
|
|
100
|
+
const width = Math.max(1, Number(args.width) || 1);
|
|
101
|
+
const height = Math.max(1, Number(args.height) || 1);
|
|
102
|
+
const format = normalizeFormat(args.format || 'png');
|
|
103
|
+
|
|
104
|
+
return runFolderBatch(env, {
|
|
105
|
+
inputDir: args.inputDir,
|
|
106
|
+
outputDir: args.outputDir,
|
|
107
|
+
processOne: async (file, outputDir) => {
|
|
108
|
+
const orientedBuf = await sharp(file).rotate().toBuffer();
|
|
109
|
+
const meta = await sharp(orientedBuf).metadata();
|
|
110
|
+
const maxL = Math.max(0, (meta.width || 1) - 1);
|
|
111
|
+
const maxT = Math.max(0, (meta.height || 1) - 1);
|
|
112
|
+
const L = Math.min(left, maxL);
|
|
113
|
+
const T = Math.min(top, maxT);
|
|
114
|
+
const W = Math.max(1, Math.min(width, (meta.width || 1) - L));
|
|
115
|
+
const H = Math.max(1, Math.min(height, (meta.height || 1) - T));
|
|
116
|
+
let pipeline = sharp(orientedBuf).extract({ left: L, top: T, width: W, height: H });
|
|
117
|
+
pipeline = await encodeSharp(pipeline, format, args.quality);
|
|
118
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
119
|
+
await pipeline.toFile(out);
|
|
120
|
+
return { out };
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Bulk Image Converter / PNG→WebP / etc. */
|
|
126
|
+
export async function batchConvert(env, args) {
|
|
127
|
+
const format = normalizeFormat(args.format || 'webp');
|
|
128
|
+
return runFolderBatch(env, {
|
|
129
|
+
inputDir: args.inputDir,
|
|
130
|
+
outputDir: args.outputDir,
|
|
131
|
+
processOne: async (file, outputDir) => {
|
|
132
|
+
let pipeline = sharp(file).rotate();
|
|
133
|
+
pipeline = await encodeSharp(pipeline, format, args.quality ?? 85);
|
|
134
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
135
|
+
await pipeline.toFile(out);
|
|
136
|
+
return { out };
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Bulk Image Rotator */
|
|
142
|
+
export async function batchRotate(env, args) {
|
|
143
|
+
const angle = Number(args.angle);
|
|
144
|
+
if (![90, 180, 270, -90, -180, -270].includes(angle)) {
|
|
145
|
+
return { ok: false, error: 'angle must be 90, 180, or 270.', processed: 0 };
|
|
146
|
+
}
|
|
147
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
148
|
+
return runFolderBatch(env, {
|
|
149
|
+
inputDir: args.inputDir,
|
|
150
|
+
outputDir: args.outputDir,
|
|
151
|
+
processOne: async (file, outputDir) => {
|
|
152
|
+
let pipeline = sharp(file).rotate(angle);
|
|
153
|
+
pipeline = await encodeSharp(pipeline, format, args.quality);
|
|
154
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
155
|
+
await pipeline.toFile(out);
|
|
156
|
+
return { out };
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Bulk Black and White */
|
|
162
|
+
export async function batchGrayscale(env, args) {
|
|
163
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
164
|
+
return runFolderBatch(env, {
|
|
165
|
+
inputDir: args.inputDir,
|
|
166
|
+
outputDir: args.outputDir,
|
|
167
|
+
processOne: async (file, outputDir) => {
|
|
168
|
+
let pipeline = sharp(file).rotate().grayscale();
|
|
169
|
+
pipeline = await encodeSharp(pipeline, format, args.quality);
|
|
170
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
171
|
+
await pipeline.toFile(out);
|
|
172
|
+
return { out };
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Bulk Image Upscale (interpolation toward 4K — same class as website Core upscaler, not ML) */
|
|
178
|
+
export async function batchUpscale(env, args) {
|
|
179
|
+
const scale = Math.min(4, Math.max(2, Number(args.scale) || 2));
|
|
180
|
+
const format = normalizeFormat(args.format || 'jpeg');
|
|
181
|
+
return runFolderBatch(env, {
|
|
182
|
+
inputDir: args.inputDir,
|
|
183
|
+
outputDir: args.outputDir,
|
|
184
|
+
processOne: async (file, outputDir) => {
|
|
185
|
+
const orientedBuf = await sharp(file).rotate().toBuffer();
|
|
186
|
+
const meta = await sharp(orientedBuf).metadata();
|
|
187
|
+
const tw = Math.min(8000, Math.round((meta.width || 1) * scale));
|
|
188
|
+
const th = Math.min(8000, Math.round((meta.height || 1) * scale));
|
|
189
|
+
let pipeline = sharp(orientedBuf).resize({
|
|
190
|
+
width: tw,
|
|
191
|
+
height: th,
|
|
192
|
+
fit: 'fill',
|
|
193
|
+
kernel: sharp.kernel.lanczos3,
|
|
194
|
+
});
|
|
195
|
+
if (args.sharpen !== false) pipeline = pipeline.sharpen();
|
|
196
|
+
pipeline = await encodeSharp(pipeline, format, args.quality ?? 90);
|
|
197
|
+
const out = outPath(file, outputDir, extFor(format));
|
|
198
|
+
await pipeline.toFile(out);
|
|
199
|
+
return { out };
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function decodeHeicToBuffer(file) {
|
|
205
|
+
// Prefer sharp (libvips heif). Fall back to heic-convert WASM.
|
|
206
|
+
try {
|
|
207
|
+
return await sharp(file).rotate().png().toBuffer();
|
|
208
|
+
} catch {
|
|
209
|
+
const heicConvert = (await import('heic-convert')).default;
|
|
210
|
+
const input = await fs.readFile(file);
|
|
211
|
+
const out = await heicConvert({ buffer: input, format: 'PNG' });
|
|
212
|
+
return Buffer.from(out);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** HEIC → PNG / JPG / PDF */
|
|
217
|
+
export async function batchHeicConvert(env, args) {
|
|
218
|
+
const format = String(args.format || 'jpeg').toLowerCase();
|
|
219
|
+
if (!['png', 'jpeg', 'jpg', 'pdf'].includes(format)) {
|
|
220
|
+
return { ok: false, error: 'format must be png, jpeg, or pdf.', processed: 0 };
|
|
221
|
+
}
|
|
222
|
+
const heicExts = new Set(['.heic', '.heif']);
|
|
223
|
+
|
|
224
|
+
if (format === 'pdf') {
|
|
225
|
+
const { PDFDocument } = await import('pdf-lib');
|
|
226
|
+
return runFolderBatch(env, {
|
|
227
|
+
inputDir: args.inputDir,
|
|
228
|
+
outputDir: args.outputDir,
|
|
229
|
+
exts: heicExts,
|
|
230
|
+
processOne: async (file, outputDir) => {
|
|
231
|
+
const pngBuf = await decodeHeicToBuffer(file);
|
|
232
|
+
const pdf = await PDFDocument.create();
|
|
233
|
+
const img = await pdf.embedPng(pngBuf);
|
|
234
|
+
const page = pdf.addPage([img.width, img.height]);
|
|
235
|
+
page.drawImage(img, { x: 0, y: 0, width: img.width, height: img.height });
|
|
236
|
+
const bytes = await pdf.save();
|
|
237
|
+
const out = outPath(file, outputDir, '.pdf');
|
|
238
|
+
await fs.writeFile(out, bytes);
|
|
239
|
+
return { out };
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const outFmt = format === 'png' ? 'png' : 'jpeg';
|
|
245
|
+
return runFolderBatch(env, {
|
|
246
|
+
inputDir: args.inputDir,
|
|
247
|
+
outputDir: args.outputDir,
|
|
248
|
+
exts: heicExts,
|
|
249
|
+
processOne: async (file, outputDir) => {
|
|
250
|
+
const pngBuf = await decodeHeicToBuffer(file);
|
|
251
|
+
let pipeline = sharp(pngBuf);
|
|
252
|
+
pipeline = await encodeSharp(pipeline, outFmt, args.quality ?? 90);
|
|
253
|
+
const out = outPath(file, outputDir, extFor(outFmt));
|
|
254
|
+
await pipeline.toFile(out);
|
|
255
|
+
return { out };
|
|
256
|
+
},
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Bulk Image → PDF (one PDF per image, or single combined PDF) */
|
|
261
|
+
export async function batchImagesToPdf(env, args) {
|
|
262
|
+
const { PDFDocument } = await import('pdf-lib');
|
|
263
|
+
const combine = args.combine === true;
|
|
264
|
+
|
|
265
|
+
if (combine) {
|
|
266
|
+
const inputDir = path.resolve(args.inputDir);
|
|
267
|
+
const outputDir = path.resolve(args.outputDir);
|
|
268
|
+
const { listFiles, MAX_BATCH } = await import('../lib/batch.js');
|
|
269
|
+
const { authorizeBatch } = await import('../authorize.js');
|
|
270
|
+
const files = await listFiles(inputDir);
|
|
271
|
+
if (!files.length) return { ok: false, error: 'No images found.', processed: 0 };
|
|
272
|
+
if (files.length > MAX_BATCH) {
|
|
273
|
+
return { ok: false, error: `Batch too large (${files.length}). Max ${MAX_BATCH}.`, processed: 0 };
|
|
274
|
+
}
|
|
275
|
+
const auth = await authorizeBatch({
|
|
276
|
+
licenseKey: env.ICI_LICENSE_KEY,
|
|
277
|
+
fileCount: files.length,
|
|
278
|
+
apiBase: env.ICI_API_BASE,
|
|
279
|
+
});
|
|
280
|
+
if (!auth.allowed) {
|
|
281
|
+
return { ok: false, error: auth.error, paywall: true, processed: 0, remaining: auth.remaining };
|
|
282
|
+
}
|
|
283
|
+
await fs.mkdir(outputDir, { recursive: true });
|
|
284
|
+
const pdf = await PDFDocument.create();
|
|
285
|
+
for (const file of files) {
|
|
286
|
+
const png = await sharp(file).rotate().png().toBuffer();
|
|
287
|
+
const img = await pdf.embedPng(png);
|
|
288
|
+
const page = pdf.addPage([img.width, img.height]);
|
|
289
|
+
page.drawImage(img, { x: 0, y: 0, width: img.width, height: img.height });
|
|
290
|
+
}
|
|
291
|
+
const out = path.join(outputDir, args.filename || 'images.pdf');
|
|
292
|
+
await fs.writeFile(out, await pdf.save());
|
|
293
|
+
return {
|
|
294
|
+
ok: true,
|
|
295
|
+
processed: files.length,
|
|
296
|
+
outputs: [out],
|
|
297
|
+
remaining: auth.remaining,
|
|
298
|
+
limit: auth.limit,
|
|
299
|
+
plan: auth.plan,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return runFolderBatch(env, {
|
|
304
|
+
inputDir: args.inputDir,
|
|
305
|
+
outputDir: args.outputDir,
|
|
306
|
+
processOne: async (file, outputDir) => {
|
|
307
|
+
const png = await sharp(file).rotate().png().toBuffer();
|
|
308
|
+
const pdf = await PDFDocument.create();
|
|
309
|
+
const img = await pdf.embedPng(png);
|
|
310
|
+
const page = pdf.addPage([img.width, img.height]);
|
|
311
|
+
page.drawImage(img, { x: 0, y: 0, width: img.width, height: img.height });
|
|
312
|
+
const out = outPath(file, outputDir, '.pdf');
|
|
313
|
+
await fs.writeFile(out, await pdf.save());
|
|
314
|
+
return { out };
|
|
315
|
+
},
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** PDF → images (pdfjs-dist + @napi-rs/canvas). Output png or jpeg. */
|
|
320
|
+
export async function batchPdfToImages(env, args) {
|
|
321
|
+
try {
|
|
322
|
+
const pdfjs = await import('pdfjs-dist/legacy/build/pdf.mjs');
|
|
323
|
+
const { createCanvas } = await import('@napi-rs/canvas');
|
|
324
|
+
const inputDir = path.resolve(args.inputDir);
|
|
325
|
+
const outputDir = path.resolve(args.outputDir);
|
|
326
|
+
const { listFiles, MAX_BATCH } = await import('../lib/batch.js');
|
|
327
|
+
const { authorizeBatch } = await import('../authorize.js');
|
|
328
|
+
const outFmt = String(args.format || 'png').toLowerCase() === 'jpeg' ? 'jpeg' : 'png';
|
|
329
|
+
const pdfs = await listFiles(inputDir, { exts: new Set(['.pdf']) });
|
|
330
|
+
if (!pdfs.length) return { ok: false, error: 'No PDF files in inputDir.', processed: 0 };
|
|
331
|
+
|
|
332
|
+
let totalPages = 0;
|
|
333
|
+
const docs = [];
|
|
334
|
+
for (const file of pdfs) {
|
|
335
|
+
const data = new Uint8Array(await fs.readFile(file));
|
|
336
|
+
const doc = await pdfjs.getDocument({ data, useSystemFonts: true, isEvalSupported: false }).promise;
|
|
337
|
+
totalPages += doc.numPages;
|
|
338
|
+
docs.push({ file, doc });
|
|
339
|
+
}
|
|
340
|
+
if (totalPages > MAX_BATCH) {
|
|
341
|
+
return {
|
|
342
|
+
ok: false,
|
|
343
|
+
error: `Too many pages (${totalPages}). Max ${MAX_BATCH} pages per call.`,
|
|
344
|
+
processed: 0,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
const auth = await authorizeBatch({
|
|
348
|
+
licenseKey: env.ICI_LICENSE_KEY,
|
|
349
|
+
fileCount: totalPages,
|
|
350
|
+
apiBase: env.ICI_API_BASE,
|
|
351
|
+
});
|
|
352
|
+
if (!auth.allowed) {
|
|
353
|
+
return { ok: false, error: auth.error, paywall: true, processed: 0, remaining: auth.remaining };
|
|
354
|
+
}
|
|
355
|
+
await fs.mkdir(outputDir, { recursive: true });
|
|
356
|
+
const scale = Math.min(3, Math.max(1, Number(args.scale) || 2));
|
|
357
|
+
const outputs = [];
|
|
358
|
+
for (const { file, doc } of docs) {
|
|
359
|
+
const base = path.parse(file).name;
|
|
360
|
+
for (let p = 1; p <= doc.numPages; p++) {
|
|
361
|
+
const page = await doc.getPage(p);
|
|
362
|
+
const viewport = page.getViewport({ scale });
|
|
363
|
+
const canvas = createCanvas(Math.ceil(viewport.width), Math.ceil(viewport.height));
|
|
364
|
+
const ctx = canvas.getContext('2d');
|
|
365
|
+
const task = page.render({
|
|
366
|
+
canvasContext: ctx,
|
|
367
|
+
viewport,
|
|
368
|
+
canvas,
|
|
369
|
+
});
|
|
370
|
+
await task.promise;
|
|
371
|
+
const pngBuf = canvas.toBuffer('image/png');
|
|
372
|
+
const out = path.join(outputDir, `${base}-p${p}.${outFmt === 'jpeg' ? 'jpg' : 'png'}`);
|
|
373
|
+
if (outFmt === 'jpeg') {
|
|
374
|
+
await sharp(pngBuf).jpeg({ quality: Number(args.quality) || 90, mozjpeg: true }).toFile(out);
|
|
375
|
+
} else {
|
|
376
|
+
await fs.writeFile(out, pngBuf);
|
|
377
|
+
}
|
|
378
|
+
outputs.push(out);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
ok: true,
|
|
383
|
+
processed: outputs.length,
|
|
384
|
+
outputs,
|
|
385
|
+
remaining: auth.remaining,
|
|
386
|
+
limit: auth.limit,
|
|
387
|
+
plan: auth.plan,
|
|
388
|
+
};
|
|
389
|
+
} catch (e) {
|
|
390
|
+
return {
|
|
391
|
+
ok: false,
|
|
392
|
+
error:
|
|
393
|
+
`PDF→image failed (${e?.message ?? e}). Needs pdfjs-dist + @napi-rs/canvas. Or use website Core PDF tool.`,
|
|
394
|
+
processed: 0,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* AI Vectorize (website-named) — local potrace mono SVG (not Cloud AI).
|
|
401
|
+
* Writes a temp PNG file for reliable potrace file-path API.
|
|
402
|
+
*/
|
|
403
|
+
export async function batchVectorize(env, args) {
|
|
404
|
+
let potrace;
|
|
405
|
+
try {
|
|
406
|
+
potrace = await import('potrace');
|
|
407
|
+
} catch {
|
|
408
|
+
return {
|
|
409
|
+
ok: false,
|
|
410
|
+
error: 'potrace not available. Reinstall @imageconvertit/mcp-server dependencies.',
|
|
411
|
+
processed: 0,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
const os = await import('node:os');
|
|
415
|
+
const threshold = Math.min(255, Math.max(0, Number(args.threshold) || 128));
|
|
416
|
+
const turdSize = Math.max(0, Number(args.turdSize) || 2);
|
|
417
|
+
return runFolderBatch(env, {
|
|
418
|
+
inputDir: args.inputDir,
|
|
419
|
+
outputDir: args.outputDir,
|
|
420
|
+
processOne: async (file, outputDir) => {
|
|
421
|
+
const tmpPng = path.join(
|
|
422
|
+
os.tmpdir(),
|
|
423
|
+
`ici-potrace-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}.png`,
|
|
424
|
+
);
|
|
425
|
+
try {
|
|
426
|
+
// High-contrast preprocess similar to website mono path
|
|
427
|
+
await sharp(file)
|
|
428
|
+
.rotate()
|
|
429
|
+
.grayscale()
|
|
430
|
+
.normalise()
|
|
431
|
+
.threshold(threshold)
|
|
432
|
+
.png()
|
|
433
|
+
.toFile(tmpPng);
|
|
434
|
+
const svg = await new Promise((resolve, reject) => {
|
|
435
|
+
potrace.trace(tmpPng, { threshold, turdSize, optCurve: true }, (err, svgStr) => {
|
|
436
|
+
if (err) reject(err);
|
|
437
|
+
else resolve(svgStr);
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
const out = outPath(file, outputDir, '.svg');
|
|
441
|
+
await fs.writeFile(out, svg, 'utf8');
|
|
442
|
+
return { out };
|
|
443
|
+
} finally {
|
|
444
|
+
await fs.unlink(tmpPng).catch(() => {});
|
|
445
|
+
}
|
|
446
|
+
},
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Book cover bleed checker — guide overlay + JSON report (trim vs bleed).
|
|
452
|
+
* Not a full KDP validator; production preview + measurements.
|
|
453
|
+
*/
|
|
454
|
+
export async function batchBleedChecker(env, args) {
|
|
455
|
+
const bleedIn = Math.max(0, Number(args.bleedInches) || 0.125);
|
|
456
|
+
const trimWIn = Number(args.trimWidthInches) || null;
|
|
457
|
+
const trimHIn = Number(args.trimHeightInches) || null;
|
|
458
|
+
const dpi = Math.max(72, Number(args.dpi) || 300);
|
|
459
|
+
const bleedPx = Math.round(bleedIn * dpi);
|
|
460
|
+
const guide = parseHexColor(args.guideColor || '#FF0000');
|
|
461
|
+
const reports = [];
|
|
462
|
+
|
|
463
|
+
const result = await runFolderBatch(env, {
|
|
464
|
+
inputDir: args.inputDir,
|
|
465
|
+
outputDir: args.outputDir,
|
|
466
|
+
processOne: async (file, outputDir) => {
|
|
467
|
+
const meta = await sharp(file).rotate().metadata();
|
|
468
|
+
const w = meta.width || 1;
|
|
469
|
+
const h = meta.height || 1;
|
|
470
|
+
const warnings = [];
|
|
471
|
+
if (w < bleedPx * 2 + 10 || h < bleedPx * 2 + 10) {
|
|
472
|
+
warnings.push('Image too small for configured bleed margin.');
|
|
473
|
+
}
|
|
474
|
+
if (trimWIn && trimHIn) {
|
|
475
|
+
const expectW = Math.round(trimWIn * dpi) + bleedPx * 2;
|
|
476
|
+
const expectH = Math.round(trimHIn * dpi) + bleedPx * 2;
|
|
477
|
+
if (Math.abs(w - expectW) > 2 || Math.abs(h - expectH) > 2) {
|
|
478
|
+
warnings.push(
|
|
479
|
+
`Pixel size ${w}×${h} ≠ expected full-bleed ${expectW}×${expectH} @ ${dpi}dpi (trim ${trimWIn}×${trimHIn}" + ${bleedIn}" bleed).`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
const svg = `
|
|
484
|
+
<svg width="${w}" height="${h}" xmlns="http://www.w3.org/2000/svg">
|
|
485
|
+
<rect x="0" y="0" width="${w}" height="${h}" fill="none" stroke="rgba(${guide.r},${guide.g},${guide.b},0.45)" stroke-width="2"/>
|
|
486
|
+
<rect x="${bleedPx}" y="${bleedPx}" width="${Math.max(1, w - bleedPx * 2)}" height="${Math.max(1, h - bleedPx * 2)}"
|
|
487
|
+
fill="none" stroke="rgba(${guide.r},${guide.g},${guide.b},0.95)" stroke-width="2" stroke-dasharray="8 6"/>
|
|
488
|
+
<text x="12" y="22" fill="rgba(${guide.r},${guide.g},${guide.b},0.9)" font-size="14" font-family="Arial,sans-serif">
|
|
489
|
+
Bleed ${bleedIn}" (${bleedPx}px) · Trim box dashed · ${w}×${h}px @ ${dpi}dpi
|
|
490
|
+
</text>
|
|
491
|
+
</svg>`;
|
|
492
|
+
const out = outPath(file, outputDir, '.png');
|
|
493
|
+
await sharp(file)
|
|
494
|
+
.rotate()
|
|
495
|
+
.composite([{ input: Buffer.from(svg), top: 0, left: 0 }])
|
|
496
|
+
.png()
|
|
497
|
+
.toFile(out);
|
|
498
|
+
reports.push({
|
|
499
|
+
file,
|
|
500
|
+
preview: out,
|
|
501
|
+
widthPx: w,
|
|
502
|
+
heightPx: h,
|
|
503
|
+
bleedInches: bleedIn,
|
|
504
|
+
bleedPx,
|
|
505
|
+
trimBox: {
|
|
506
|
+
x: bleedPx,
|
|
507
|
+
y: bleedPx,
|
|
508
|
+
width: Math.max(1, w - bleedPx * 2),
|
|
509
|
+
height: Math.max(1, h - bleedPx * 2),
|
|
510
|
+
},
|
|
511
|
+
warnings,
|
|
512
|
+
ok: warnings.length === 0,
|
|
513
|
+
});
|
|
514
|
+
return { out };
|
|
515
|
+
},
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
if (result.ok || result.processed) {
|
|
519
|
+
const reportPath = path.join(path.resolve(args.outputDir), 'bleed-report.json');
|
|
520
|
+
try {
|
|
521
|
+
await fs.mkdir(path.resolve(args.outputDir), { recursive: true });
|
|
522
|
+
await fs.writeFile(reportPath, JSON.stringify({ reports, dpi, bleedInches: bleedIn }, null, 2));
|
|
523
|
+
result.report = reportPath;
|
|
524
|
+
result.reports = reports;
|
|
525
|
+
} catch { /* non-fatal */ }
|
|
526
|
+
}
|
|
527
|
+
return result;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** Explicit PNG→WebP helper (same engine as batch_convert). */
|
|
531
|
+
export async function batchPngToWebp(env, args) {
|
|
532
|
+
return batchConvert(env, { ...args, format: 'webp', quality: args.quality ?? 85 });
|
|
533
|
+
}
|
|
534
|
+
|