perf: optimize image processing

This commit is contained in:
molvqingtai 2024-11-06 18:24:43 +08:00
parent 7681682445
commit 9438a3169d

View file

@ -250,7 +250,13 @@ const Footer: FC = () => {
const handleInjectImage = async (file: File) => { const handleInjectImage = async (file: File) => {
try { try {
setInputLoading(true) setInputLoading(true)
const blob = await compressImage({ input: file, targetSize: 30 * 1024, outputType: 'image/webp' })
const blob = await compressImage({
input: file,
targetSize: 30 * 1024,
outputType: file.size > 30 * 1024 ? 'image/webp' : undefined
})
const base64 = await blobToBase64(blob) const base64 = await blobToBase64(blob)
const hash = nanoid() const hash = nanoid()
const newMessage = `${message.slice(0, selectionEnd)}![Image](hash:${hash})${message.slice(selectionEnd)}` const newMessage = `${message.slice(0, selectionEnd)}![Image](hash:${hash})${message.slice(selectionEnd)}`