2026-04-24 18:02:21 +08:00
|
|
|
import { flip, rotate } from "@snapotter/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";
|
2026-04-26 03:22:26 +08:00
|
|
|
import { resolveOutputFormat } from "../../lib/output-format.js";
|
2026-03-22 03:56:34 +08:00
|
|
|
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) => {
|
2026-04-26 03:22:26 +08:00
|
|
|
const outputFormat = await resolveOutputFormat(inputBuffer, filename);
|
2026-03-22 03:56:34 +08:00
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 03:22:26 +08:00
|
|
|
const buffer = await image
|
|
|
|
|
.toFormat(outputFormat.format, { quality: outputFormat.quality })
|
|
|
|
|
.toBuffer();
|
|
|
|
|
return { buffer, filename, contentType: outputFormat.contentType };
|
2026-03-22 03:56:34 +08:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|