2026-04-14 20:55:42 +08:00
|
|
|
import { resize } from "@ashim/image-engine";
|
2026-03-25 09:27:12 +08:00
|
|
|
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({
|
|
|
|
|
width: z.number().positive().optional(),
|
|
|
|
|
height: z.number().positive().optional(),
|
2026-03-25 09:27:12 +08:00
|
|
|
fit: z.enum(["contain", "cover", "fill", "inside", "outside"]).default("contain"),
|
2026-03-22 03:56:34 +08:00
|
|
|
withoutEnlargement: z.boolean().default(false),
|
|
|
|
|
percentage: z.number().positive().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function registerResize(app: FastifyInstance) {
|
|
|
|
|
createToolRoute(app, {
|
|
|
|
|
toolId: "resize",
|
|
|
|
|
settingsSchema,
|
|
|
|
|
process: async (inputBuffer, settings, filename) => {
|
|
|
|
|
const image = sharp(inputBuffer);
|
|
|
|
|
const result = await resize(image, settings);
|
|
|
|
|
const buffer = await result.toBuffer();
|
|
|
|
|
return { buffer, filename, contentType: "image/png" };
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|