mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
30 lines
799 B
TypeScript
30 lines
799 B
TypeScript
import type { ConvertOptions, OutputFormat, Sharp } from "../types.js";
|
|
|
|
const FORMAT_MAP: Record<OutputFormat, string> = {
|
|
jpg: "jpeg",
|
|
png: "png",
|
|
webp: "webp",
|
|
avif: "avif",
|
|
tiff: "tiff",
|
|
gif: "gif",
|
|
};
|
|
|
|
export async function convert(image: Sharp, options: ConvertOptions): Promise<Sharp> {
|
|
const { format, quality } = options;
|
|
|
|
const sharpFormat = FORMAT_MAP[format];
|
|
if (!sharpFormat) {
|
|
throw new Error(`Unsupported output format: ${format}`);
|
|
}
|
|
|
|
const formatOptions: Record<string, unknown> = {};
|
|
if (quality !== undefined) {
|
|
if (quality < 1 || quality > 100) {
|
|
throw new Error("Quality must be between 1 and 100");
|
|
}
|
|
formatOptions.quality = quality;
|
|
}
|
|
|
|
return image.toFormat(sharpFormat as keyof import("sharp").FormatEnum, formatOptions);
|
|
}
|