2026-03-25 09:27:12 +08:00
|
|
|
import { crop } from "@stirling-image/image-engine";
|
|
|
|
|
import type { FastifyInstance } from "fastify";
|
|
|
|
|
import sharp from "sharp";
|
2026-03-22 03:56:34 +08:00
|
|
|
import { z } from "zod";
|
|
|
|
|
import { createToolRoute } from "../tool-factory.js";
|
|
|
|
|
|
|
|
|
|
const settingsSchema = z.object({
|
|
|
|
|
left: z.number().int().min(0),
|
|
|
|
|
top: z.number().int().min(0),
|
|
|
|
|
width: z.number().int().positive(),
|
|
|
|
|
height: z.number().int().positive(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function registerCrop(app: FastifyInstance) {
|
|
|
|
|
createToolRoute(app, {
|
|
|
|
|
toolId: "crop",
|
|
|
|
|
settingsSchema,
|
|
|
|
|
process: async (inputBuffer, settings, filename) => {
|
|
|
|
|
const image = sharp(inputBuffer);
|
|
|
|
|
const result = await crop(image, settings);
|
|
|
|
|
const buffer = await result.toBuffer();
|
|
|
|
|
return { buffer, filename, contentType: "image/png" };
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|