convertImageToBase64
A function that converts an image to Base64 format in the desired image format (png, jpeg, webp) using canvas.
💡 The image types accepted by canvas.toDataURL() are image/png, image/jpeg, and image/webp. For jpg, it is internally changed to jpeg.
Browsers are required to support
image/png; many will support additional formats includingimage/jpegandimage/webp.
Code
Interface
typescript
type CanvasImageType = 'png' | 'jpeg' | 'jpg' | 'webp';
const convertImageToBase64: (
url: string,
imageType?: CanvasImageType // default: 'png'
) => Promise<string>;
Usage
typescript
import { convertImageToBase64 } from '@modern-kit/utils';
const imageBase64 = await convertImageToBase64("image src", 'png');