File size: 897 Bytes
2cae2a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { convertImageToJpeg } from "./convertImageToJpeg.mts"
import { convertImageToPng } from "./convertImageToPng.mts"
import { convertImageToWebp } from "./convertImageToWebp.mts"
import { ImageFileExt } from "./imageFormats.mts"

/**
 * Convert an image to one of the supported file formats
 * 
 * @param imgBase64 
 * @param outputFormat 
 * @returns 
 */
export async function convertImageTo(imgBase64: string = "", outputFormat: ImageFileExt): Promise<string> {
  const format = outputFormat.trim().toLowerCase() as ImageFileExt
  if (!["jpeg", "jpg", "png", "webp"].includes(format)) {
    throw new Error(`unsupported file format "${format}"`)
  }

  const isJpeg = format === "jpg" || format === "jpeg" 
     

  if (isJpeg) {
    return convertImageToJpeg(imgBase64)
  }

  if (format === "webp") {
    return convertImageToWebp(imgBase64)
  }

  return convertImageToPng(imgBase64)
}