mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
29 lines
923 B
TypeScript
29 lines
923 B
TypeScript
import { z } from "zod";
|
|||
|
|
import { createToolRoute } from "../tool-factory.js";
|
||
|
|
import { resize } from "@stirling-image/image-engine";
|
||
|
|
import sharp from "sharp";
|
||
|
|
import type { FastifyInstance } from "fastify";
|
||
|
|
|
||
|
|
const settingsSchema = z.object({
|
||
|
|
width: z.number().positive().optional(),
|
||
|
|
height: z.number().positive().optional(),
|
||
|
|
fit: z
|
||
|
|
.enum(["contain", "cover", "fill", "inside", "outside"])
|
||
|
|
.default("contain"),
|
||
|
|
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" };
|
||
|
|
},
|
||
|
|
});
|
||
|
|
}
|