You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
669 B
31 lines
669 B
2 years ago
|
import fs from 'fs';
|
||
|
import path from 'path';
|
||
|
|
||
|
export const extTypeMap = {
|
||
|
'.png': 'image/png',
|
||
|
'.gif': 'image/gif',
|
||
|
'.jpg': 'image/jpeg',
|
||
|
'.jpeg': 'image/jpeg',
|
||
|
'.bm': 'image/bmp',
|
||
|
'.bmp': 'image/bmp',
|
||
|
'.webp': 'image/webp',
|
||
|
'.ico': 'image/x-icon',
|
||
|
'.svg': 'image/svg+xml'
|
||
|
}
|
||
|
|
||
|
export type ExtType = '.png'
|
||
|
| '.gif'
|
||
|
| '.jpg'
|
||
|
| '.jpeg'
|
||
|
| '.bm'
|
||
|
| '.bmp'
|
||
|
| '.webp'
|
||
|
| '.ico'
|
||
|
| '.svg';
|
||
|
|
||
|
export default function image2uri(file: string) {
|
||
|
const image = fs.readFileSync(file)
|
||
|
const ext = path.extname(file) as ExtType;
|
||
|
const contentType = extTypeMap[ext] || 'image/jpeg'
|
||
|
return `data:${contentType};base64,${image.toString('base64')}`
|
||
|
}
|