2026-06-04 10:26:08 -04:00
|
|
|
export type CompressionResult = {
|
|
|
|
|
blob: Blob;
|
2026-07-02 20:56:27 -04:00
|
|
|
/** MIME type of the produced blob (currently always image/jpeg). */
|
|
|
|
|
type: string;
|
2026-06-04 10:26:08 -04:00
|
|
|
originalSize: number;
|
|
|
|
|
compressedSize: number;
|
|
|
|
|
width: number;
|
|
|
|
|
height: number;
|
|
|
|
|
};
|
|
|
|
|
|
2026-06-05 20:22:38 -04:00
|
|
|
// Any raster image the browser can render to canvas can be compressed to JPEG.
|
|
|
|
|
// SVG is vector and must stay as-is; GIF animation is lost on canvas but static
|
|
|
|
|
// frames compress fine. Empty type string (undetected MIME) is excluded.
|
|
|
|
|
const isCompressibleType = (type: string): boolean =>
|
|
|
|
|
type.startsWith('image/') && type !== 'image/svg+xml' && type !== '';
|
2026-06-04 10:26:08 -04:00
|
|
|
|
2026-06-05 20:22:38 -04:00
|
|
|
/** Returns true if this file can be compressed via canvas (any raster image type). */
|
|
|
|
|
export function isCompressible(file: File | Blob): boolean {
|
|
|
|
|
return isCompressibleType(file.type);
|
2026-06-04 10:26:08 -04:00
|
|
|
}
|
|
|
|
|
|
2026-07-02 20:56:27 -04:00
|
|
|
const JPEG_OUTPUT_TYPE = 'image/jpeg';
|
|
|
|
|
|
2026-06-04 10:26:08 -04:00
|
|
|
/**
|
2026-06-05 20:22:38 -04:00
|
|
|
* Compress an image file via canvas.toBlob → JPEG at the given quality.
|
2026-07-02 20:56:27 -04:00
|
|
|
* Returns null if the browser cannot render the image (e.g. unsupported codec)
|
|
|
|
|
* or if the source is left untouched to avoid data loss (see below).
|
|
|
|
|
*
|
|
|
|
|
* PNG is skipped entirely: it may carry an alpha channel, and re-encoding to
|
|
|
|
|
* JPEG composites transparency onto an opaque (black) background, corrupting the
|
|
|
|
|
* image. Returning null makes callers fall back to uploading the lossless
|
|
|
|
|
* original. The image is decoded with `imageOrientation: 'from-image'` so any
|
|
|
|
|
* EXIF orientation is baked into the pixels instead of being silently dropped.
|
2026-06-04 10:26:08 -04:00
|
|
|
*/
|
2026-06-05 20:22:38 -04:00
|
|
|
export async function compressImage(
|
|
|
|
|
file: File | Blob,
|
|
|
|
|
quality = 0.82,
|
|
|
|
|
): Promise<CompressionResult | null> {
|
|
|
|
|
if (!isCompressibleType(file.type)) return null;
|
2026-07-02 20:56:27 -04:00
|
|
|
// Skip PNG (potential alpha) — re-encoding to JPEG would flatten transparency.
|
|
|
|
|
if (file.type === 'image/png') return null;
|
2026-06-04 10:26:08 -04:00
|
|
|
|
2026-07-02 20:56:27 -04:00
|
|
|
let bitmap: ImageBitmap;
|
|
|
|
|
try {
|
|
|
|
|
bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' });
|
|
|
|
|
} catch {
|
|
|
|
|
// Corrupt/unsupported source: fall back to uploading the lossless original
|
|
|
|
|
// (the caller uses the original file on a null result) rather than rejecting,
|
|
|
|
|
// which would drop the file entirely from the Promise.allSettled upload.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
const { width, height } = bitmap;
|
2026-06-04 10:26:08 -04:00
|
|
|
const canvas = document.createElement('canvas');
|
2026-07-02 20:56:27 -04:00
|
|
|
canvas.width = width;
|
|
|
|
|
canvas.height = height;
|
|
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
|
|
if (!ctx) {
|
|
|
|
|
bitmap.close();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
ctx.drawImage(bitmap, 0, 0);
|
|
|
|
|
bitmap.close();
|
2026-06-04 10:26:08 -04:00
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
canvas.toBlob(
|
|
|
|
|
(blob) => {
|
|
|
|
|
if (!blob) {
|
|
|
|
|
resolve(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
resolve({
|
|
|
|
|
blob,
|
2026-07-02 20:56:27 -04:00
|
|
|
type: JPEG_OUTPUT_TYPE,
|
2026-06-04 10:26:08 -04:00
|
|
|
originalSize: file.size,
|
|
|
|
|
compressedSize: blob.size,
|
2026-07-02 20:56:27 -04:00
|
|
|
width,
|
|
|
|
|
height,
|
2026-06-04 10:26:08 -04:00
|
|
|
});
|
|
|
|
|
},
|
2026-07-02 20:56:27 -04:00
|
|
|
JPEG_OUTPUT_TYPE,
|
2026-06-04 10:26:08 -04:00
|
|
|
quality,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function formatFileSize(bytes: number): string {
|
|
|
|
|
if (bytes < 1024) return `${bytes} B`;
|
|
|
|
|
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
|
|
|
|
|
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
|
|
|
}
|