2026-04-14 20:55:42 +08:00
|
|
|
import { flip, rotate } 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({
|
|
|
|
|
angle: z.number().default(0),
|
|
|
|
|
horizontal: z.boolean().default(false),
|
|
|
|
|
vertical: z.boolean().default(false),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export function registerRotate(app: FastifyInstance) {
|
|
|
|
|
createToolRoute(app, {
|
|
|
|
|
toolId: "rotate",
|
|
|
|
|
settingsSchema,
|
|
|
|
|
process: async (inputBuffer, settings, filename) => {
|
|
|
|
|
let image = sharp(inputBuffer);
|
|
|
|
|
|
|
|
|
|
// Apply rotation first
|
|
|
|
|
if (settings.angle !== 0) {
|
|
|
|
|
image = await rotate(image, { angle: settings.angle });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Then apply flip/flop
|
|
|
|
|
if (settings.horizontal || settings.vertical) {
|
|
|
|
|
image = await flip(image, {
|
|
|
|
|
horizontal: settings.horizontal,
|
|
|
|
|
vertical: settings.vertical,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buffer = await image.toBuffer();
|
|
|
|
|
return { buffer, filename, contentType: "image/png" };
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|