Image Helper Functions
These Image
Helper Functions are used for
Converting Images to Base64 and Compressing Images
Used Package
ToBase64
bae64.ts
const toBase64 = (file: File | Blob) => {
return new Promise<string>(
(resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL();
reader.onload = () => {
typeof reader.result === 'string' && resolve(reader.result.replace('data:image/webp;base64,', ''))
}
reader.onerror = reject
}
)
}
CompressImage
compressImage.ts
import Compressor from 'compressorjs'
const compressImage = (file: File, options?: object) => {
return new Promise()<string>((resolve, reject) => {
new Compressor(file, {
resize: 'cover',
mimeType: 'image/webp',
error: reject,
success: (result) => toBase64(result).then(resolve).catch(reject),
...options
})
})
}