File size: 536 Bytes
75aad1e
 
 
 
 
 
 
 
 
 
 
 
 
 
6a3d22f
75aad1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import sharp from "sharp"

export async function resizeBase64Image(imgBase64: string, targetWidth: number, targetHeight: number): Promise<string> {
  // Convert base64 to buffer
  const buffer = Buffer.from(imgBase64, 'base64');

  // Resize the buffer to the target size
  const resizedBuffer = await sharp(buffer)
      .resize(targetWidth, targetHeight)
      .toBuffer();

  // Convert the buffer back to base64
  const resizedImageBase64 = resizedBuffer.toString('base64');

  return `data:image/png;base64,${resizedImageBase64}`
}