Files
co-lite/solvway-admin-portal/app/lib/images/shrinkImage.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

60 lines
2.4 KiB
TypeScript

/** Upload budget: stays under the smallest ingress body limit we have proof of (existing <1 MB uploads). */
const TARGET_BYTES = 900 * 1024;
/** Tried in order until one fits TARGET_BYTES: quality first, then size. */
const STEPS: [maxEdge: number, quality: number][] = [
[2000, 0.85],
[2000, 0.7],
[1600, 0.7],
[1280, 0.7],
[1024, 0.6],
];
/**
* Re-encodes a photo as a JPEG of at most 2000px on its long side and, where possible, at most
* 900 KB, stepping quality and then size down until it fits (the smallest attempt wins otherwise).
* A PNG already within 900 KB is kept as-is so screenshot text stays sharp. Returns the file
* unchanged when the browser cannot decode it (or in jsdom), or when re-encoding would not make it
* smaller. The server's 5 MB limit stays the backstop.
*/
export async function shrinkImage(file: File): Promise<File> {
if (file.type === "image/png" && file.size <= TARGET_BYTES) return file;
if (typeof createImageBitmap !== "function") return file;
try {
const bitmap = await createImageBitmap(file);
let best: Blob | null = null;
try {
for (const [maxEdge, quality] of STEPS) {
const blob = await encode(bitmap, maxEdge, quality);
if (!blob) break;
if (!best || blob.size < best.size) best = blob;
if (blob.size <= TARGET_BYTES) break;
}
} finally {
bitmap.close();
}
if (!best || best.size >= file.size) return file;
return new File([best], `${file.name.replace(/\.[^.]+$/, "")}.jpg`, { type: "image/jpeg" });
} catch {
return file;
}
}
async function encode(bitmap: ImageBitmap, maxEdge: number, quality: number): Promise<Blob | null> {
const { width, height } = fitWithin(bitmap.width, bitmap.height, maxEdge);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
if (!ctx) return null;
ctx.fillStyle = "#fff"; // JPEG has no alpha: transparent PNG pixels would otherwise turn black
ctx.fillRect(0, 0, width, height);
ctx.drawImage(bitmap, 0, 0, width, height);
return new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, "image/jpeg", quality));
}
/** Fits w x h within maxEdge keeping the aspect ratio; never upscales. */
function fitWithin(w: number, h: number, maxEdge: number): { width: number; height: number } {
const scale = Math.min(1, maxEdge / Math.max(w, h));
return { width: Math.floor(w * scale), height: Math.floor(h * scale) };
}