24 lines
950 B
TypeScript
24 lines
950 B
TypeScript
import sharp from "sharp";
|
|
|
|
// This is the provider's per-reference limit, separate from our 20 MiB upload limit.
|
|
export const VIP98K_REFERENCE_MAX_BYTES = 10 * 1024 * 1024;
|
|
|
|
export function isVip98kReferenceSizeAllowed(bytes: number) {
|
|
return Number.isFinite(bytes) && bytes > 0 && bytes <= VIP98K_REFERENCE_MAX_BYTES;
|
|
}
|
|
|
|
export async function normalizeVip98kReferenceImage(buffer: Buffer) {
|
|
for (const quality of [92, 82, 70]) {
|
|
const normalized = await sharp(buffer, { failOn: "none" })
|
|
.rotate()
|
|
.resize(1920, 1920, { fit: "inside", withoutEnlargement: true })
|
|
.flatten({ background: "#ffffff" })
|
|
.jpeg({ quality, chromaSubsampling: "4:4:4", mozjpeg: true })
|
|
.toBuffer();
|
|
if (isVip98kReferenceSizeAllowed(normalized.byteLength)) {
|
|
return { buffer: normalized, extension: "jpg" };
|
|
}
|
|
}
|
|
throw new Error("参考图压缩后仍超过服务商单张 10 MiB 限制,请缩小图片后重试");
|
|
}
|