2026-03-25 09:27:12 +08:00
|
|
|
import type { ResizeOptions, Sharp } from "../types.js";
|
2026-03-22 03:42:40 +08:00
|
|
|
|
|
|
|
|
export async function resize(image: Sharp, options: ResizeOptions): Promise<Sharp> {
|
|
|
|
|
let { width, height, fit, withoutEnlargement, percentage } = options;
|
|
|
|
|
|
|
|
|
|
if (percentage !== undefined) {
|
|
|
|
|
if (percentage <= 0) {
|
|
|
|
|
throw new Error("Resize percentage must be greater than 0");
|
|
|
|
|
}
|
|
|
|
|
const metadata = await image.metadata();
|
|
|
|
|
const currentWidth = metadata.width ?? 0;
|
|
|
|
|
const currentHeight = metadata.height ?? 0;
|
|
|
|
|
width = Math.round(currentWidth * (percentage / 100));
|
|
|
|
|
height = Math.round(currentHeight * (percentage / 100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (width !== undefined && width <= 0) {
|
|
|
|
|
throw new Error("Resize width must be greater than 0");
|
|
|
|
|
}
|
|
|
|
|
if (height !== undefined && height <= 0) {
|
|
|
|
|
throw new Error("Resize height must be greater than 0");
|
|
|
|
|
}
|
|
|
|
|
if (width === undefined && height === undefined) {
|
|
|
|
|
throw new Error("Resize requires width, height, or percentage");
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-25 07:17:45 +08:00
|
|
|
if (withoutEnlargement) {
|
|
|
|
|
const meta = await image.metadata();
|
|
|
|
|
const curW = meta.width ?? 0;
|
|
|
|
|
const curH = meta.height ?? 0;
|
|
|
|
|
if (width !== undefined && width > curW) width = curW;
|
|
|
|
|
if (height !== undefined && height > curH) height = curH;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 03:42:40 +08:00
|
|
|
return image.resize({
|
|
|
|
|
width,
|
|
|
|
|
height,
|
|
|
|
|
fit: fit ?? "cover",
|
|
|
|
|
withoutEnlargement: withoutEnlargement ?? false,
|
|
|
|
|
});
|
|
|
|
|
}
|