Files
SnapOtter/apps/api/src/routes/tools/crop.ts
T
Siddharth Kumar Sah b8227c45b9 feat(crop): improve UI and fix batch crop for multi-file
- Replace Rule of Thirds button with checkbox for clearer toggle
- Show side-by-side comparison after crop instead of overlay slider
- Add custom aspect ratio option with W:H number inputs
- Fix batch crop failing on files with different dimensions by
  sending percentage-based coordinates instead of absolute pixels
- Add failed-file error state display in tool page
2026-04-11 14:47:46 +08:00

31 lines
1.0 KiB
TypeScript

import { crop } from "@stirling-image/image-engine";
import type { FastifyInstance } from "fastify";
import sharp from "sharp";
import { z } from "zod";
import { resolveOutputFormat } from "../../lib/output-format.js";
import { createToolRoute } from "../tool-factory.js";
const settingsSchema = z.object({
left: z.number().min(0),
top: z.number().min(0),
width: z.number().positive(),
height: z.number().positive(),
unit: z.enum(["px", "percent"]).optional(),
});
export function registerCrop(app: FastifyInstance) {
createToolRoute(app, {
toolId: "crop",
settingsSchema,
process: async (inputBuffer, settings, filename) => {
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
const image = sharp(inputBuffer);
const result = await crop(image, settings);
const buffer = await result
.toFormat(outputFormat.format, { quality: outputFormat.quality })
.toBuffer();
return { buffer, filename, contentType: outputFormat.contentType };
},
});
}