import type { ResizeOptions, Sharp } from "../types.js"; export async function resize(image: Sharp, options: ResizeOptions): Promise { 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"); } 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; } return image.resize({ width, height, fit: fit ?? "cover", withoutEnlargement: withoutEnlargement ?? false, }); }