Skip to content

Commit 39187c4

Browse files
committed
fix: image too large
1 parent 72de829 commit 39187c4

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

components/ChatInput.vue

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import {handleImgZoom} from "~/utils/tools";
2+
import {compressionFile, handleImgZoom} from "~/utils/tools";
33
44
const input = ref('')
55
const addHistory = ref(true)
@@ -52,10 +52,6 @@ function checkFile(file: File) {
5252
alert(imageType.join(', ') + ' only')
5353
return false
5454
}
55-
if (file.size > 1024 * 1024 * 15) {
56-
alert('The image size should be less than 15MB')
57-
return false
58-
}
5955
return true
6056
}
6157
@@ -64,14 +60,18 @@ function handleAddFiles() {
6460
input.type = 'file'
6561
input.accept = imageType.join(',')
6662
input.multiple = true
67-
input.onchange = () => {
68-
const files = Array.from(input.files || [])
69-
files.forEach(file => {
70-
if (!checkFile(file)) return
63+
input.onchange = async () => {
64+
document.body.style.cursor = 'wait'
7165
66+
const files = Array.from(input.files || [])
67+
for (const f of files) {
68+
if (!checkFile(f)) continue;
69+
const file = await compressionFile(f, f.type)
7270
const url = URL.createObjectURL(file)
7371
fileList.value.push({file, url})
74-
})
72+
}
73+
74+
document.body.style.cursor = 'auto'
7575
}
7676
input.click()
7777
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cloudflare-ai-web",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"private": true,
55
"type": "module",
66
"scripts": {

utils/tools.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,44 @@ export function handleImgZoom(img: HTMLImageElement) {
103103

104104
imgZoom.height
105105
container.style.opacity = '1'
106-
}
106+
}
107+
108+
const fileToDataURL = (file: Blob): Promise<any> => {
109+
return new Promise((resolve) => {
110+
const reader = new FileReader()
111+
reader.onloadend = (e) => resolve((e.target as FileReader).result)
112+
reader.readAsDataURL(file)
113+
})
114+
}
115+
116+
const dataURLToImage = (dataURL: string): Promise<HTMLImageElement> => {
117+
return new Promise((resolve) => {
118+
const img = new Image()
119+
img.onload = () => resolve(img)
120+
img.src = dataURL
121+
})
122+
}
123+
124+
const canvastoFile = (canvas: HTMLCanvasElement, type: string, quality: number): Promise<Blob | null> => {
125+
return new Promise((resolve) => canvas.toBlob((blob) => resolve(blob), type, quality))
126+
}
127+
128+
export const compressionFile = async (file: File, type: string, quality = 0.2) => {
129+
const fileName = file.name
130+
const canvas = document.createElement('canvas')
131+
const context = canvas.getContext('2d') as CanvasRenderingContext2D
132+
const base64 = await fileToDataURL(file)
133+
const img = await dataURLToImage(base64)
134+
canvas.width = img.width
135+
canvas.height = img.height
136+
context.clearRect(0, 0, img.width, img.height)
137+
138+
context.fillStyle = '#fff'
139+
context.fillRect(0, 0, img.width, img.height)
140+
141+
context.drawImage(img, 0, 0, img.width, img.height)
142+
const blob = (await canvastoFile(canvas, type, quality)) as Blob
143+
return new File([blob], fileName, {
144+
type: type
145+
})
146+
}

0 commit comments

Comments
 (0)