All files / src/utils imageResize.ts

100% Statements 36/36
71.42% Branches 5/7
75% Functions 3/4
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 411x   4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x   2x 2x 2x 2x 2x 2x 2x 2x 2x  
const SMALL_SIZE = 200;
 
function resizeToJpeg(file: File, maxDimension: number): Promise<Blob> {
  return new Promise((resolve, reject) => {
    const img = new Image();
    const url = URL.createObjectURL(file);
 
    img.onload = () => {
      URL.revokeObjectURL(url);
      const scale = Math.min(1, maxDimension / Math.max(img.width, img.height));
      const canvas = document.createElement('canvas');
      canvas.width = Math.round(img.width * scale);
      canvas.height = Math.round(img.height * scale);
      const ctx = canvas.getContext('2d');
      if (!ctx) return reject(new Error('Canvas not supported'));
      ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
      canvas.toBlob(
        (blob) => {
          if (!blob) return reject(new Error('Failed to convert image'));
          resolve(blob);
        },
        'image/jpeg',
        0.9,
      );
    };
 
    img.onerror = () => reject(new Error('Failed to load image'));
    img.src = url;
  });
}
 
export async function prepareAvatarBlobs(
  file: File,
): Promise<{ original: Blob; small: Blob }> {
  const [original, small] = await Promise.all([
    resizeToJpeg(file, 1200),
    resizeToJpeg(file, SMALL_SIZE),
  ]);
  return { original, small };
}