mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: add format tools (SVG-to-raster, vectorize, GIF) and optimization (rename, favicon, image-to-PDF)
Add 6 tools for format conversion and optimization extras: - svg-to-raster: SVG to PNG/JPG/WebP at custom resolution - vectorize: raster to SVG via potrace (B&W and color modes) - gif-tools: animated GIF resize, frame extraction, optimization - bulk-rename: pattern-based file renaming with ZIP output - favicon: generate all favicon/app icon sizes with manifest.json - image-to-pdf: combine images into PDF using pdfkit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { z } from "zod";
|
||||
import { createToolRoute } from "../tool-factory.js";
|
||||
import sharp from "sharp";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
const settingsSchema = z.object({
|
||||
width: z.number().min(1).max(4096).optional(),
|
||||
height: z.number().min(1).max(4096).optional(),
|
||||
extractFrame: z.number().min(0).optional(),
|
||||
optimize: z.boolean().default(false),
|
||||
});
|
||||
|
||||
export function registerGifTools(app: FastifyInstance) {
|
||||
createToolRoute(app, {
|
||||
toolId: "gif-tools",
|
||||
settingsSchema,
|
||||
process: async (inputBuffer, settings, filename) => {
|
||||
if (settings.extractFrame !== undefined) {
|
||||
// Extract a single frame from animated GIF
|
||||
const image = sharp(inputBuffer, { page: settings.extractFrame });
|
||||
|
||||
if (settings.width || settings.height) {
|
||||
image.resize(settings.width, settings.height, { fit: "inside" });
|
||||
}
|
||||
|
||||
const buffer = await image.png().toBuffer();
|
||||
const outName = filename.replace(/\.gif$/i, "") + `_frame${settings.extractFrame}.png`;
|
||||
return { buffer, filename: outName, contentType: "image/png" };
|
||||
}
|
||||
|
||||
// Process animated GIF (preserve animation)
|
||||
const image = sharp(inputBuffer, { animated: true });
|
||||
|
||||
if (settings.width || settings.height) {
|
||||
image.resize(settings.width, settings.height, { fit: "inside" });
|
||||
}
|
||||
|
||||
if (settings.optimize) {
|
||||
// Reduce colors for optimization
|
||||
image.gif({ effort: 10 });
|
||||
}
|
||||
|
||||
const buffer = await image.gif().toBuffer();
|
||||
return { buffer, filename, contentType: "image/gif" };
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user