Skip to content

Commit 3bf6aca

Browse files
iseabockIan Seabock (Centific Technologies Inc)
andauthored
Fix: Resize image before conversation and save history (#1116)
Co-authored-by: Ian Seabock (Centific Technologies Inc) <v-ianseabock@microsoft.com>
1 parent d98ef41 commit 3bf6aca

File tree

6 files changed

+63
-57
lines changed

6 files changed

+63
-57
lines changed

frontend/src/components/QuestionInput/QuestionInput.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Send from '../../assets/Send.svg'
77
import styles from './QuestionInput.module.css'
88
import { ChatMessage } from '../../api'
99
import { AppStateContext } from '../../state/AppProvider'
10+
import { resizeImage } from '../../utils/resizeImage'
1011

1112
interface Props {
1213
onSend: (question: ChatMessage['content'], id?: string) => void
@@ -32,17 +33,12 @@ export const QuestionInput = ({ onSend, disabled, placeholder, clearOnSend, conv
3233
};
3334

3435
const convertToBase64 = async (file: Blob) => {
35-
const reader = new FileReader();
36-
37-
reader.readAsDataURL(file);
38-
39-
reader.onloadend = () => {
40-
setBase64Image(reader.result as string);
41-
};
42-
43-
reader.onerror = (error) => {
44-
console.error('Error: ', error);
45-
};
36+
try {
37+
const resizedBase64 = await resizeImage(file, 800, 800);
38+
setBase64Image(resizedBase64);
39+
} catch (error) {
40+
console.error('Error:', error);
41+
}
4642
};
4743

4844
const sendQuestion = () => {

frontend/src/pages/chat/Chat.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ const Chat = () => {
652652
}
653653
const noContentError = appStateContext.state.currentChat.messages.find(m => m.role === ERROR)
654654

655-
if (typeof noContentError?.content === "string" && !noContentError?.content.includes(NO_CONTENT_ERROR)) {
655+
if (!noContentError) {
656656
saveToDB(appStateContext.state.currentChat.messages, appStateContext.state.currentChat.id)
657657
.then(res => {
658658
if (!res.ok) {

frontend/src/utils/resizeImage.ts

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
1-
export const resizeImage = (file: Blob, maxWidth: number, maxHeight: number) => {
2-
const img = new Image()
3-
const reader = new FileReader()
4-
5-
reader.readAsDataURL(file)
6-
reader.onloadend = () => {
7-
img.src = reader.result as string
8-
img.onload = () => {
9-
const canvas = document.createElement('canvas')
10-
const ctx = canvas.getContext('2d')
11-
12-
let { width, height } = img
13-
14-
if (width > maxWidth || height > maxHeight) {
15-
if (width > height) {
16-
height *= maxWidth / width
17-
width = maxWidth
18-
} else {
19-
width *= maxHeight / height
20-
height = maxHeight
1+
export const resizeImage = (file: Blob, maxWidth: number, maxHeight: number): Promise<string> => {
2+
return new Promise((resolve, reject) => {
3+
const img = new Image()
4+
const reader = new FileReader()
5+
6+
reader.readAsDataURL(file)
7+
reader.onloadend = () => {
8+
img.src = reader.result as string
9+
img.onload = () => {
10+
const canvas = document.createElement('canvas')
11+
const ctx = canvas.getContext('2d')
12+
13+
let { width, height } = img
14+
15+
// Calculate the new dimensions
16+
if (width > maxWidth || height > maxHeight) {
17+
if (width > height) {
18+
height = (maxWidth / width) * height
19+
width = maxWidth
20+
} else {
21+
width = (maxHeight / height) * width
22+
height = maxHeight
23+
}
24+
}
25+
26+
canvas.width = width
27+
canvas.height = height
28+
29+
if (ctx) {
30+
ctx.drawImage(img, 0, 0, width, height)
2131
}
32+
33+
// Convert the canvas to a base64 string
34+
const resizedBase64 = canvas.toDataURL('image/jpeg', 0.8)
35+
resolve(resizedBase64)
2236
}
2337

24-
canvas.width = width
25-
canvas.height = height
26-
if (ctx) {
27-
ctx.drawImage(img, 0, 0, width, height)
38+
img.onerror = error => {
39+
reject('Error loading image: ' + error)
2840
}
41+
}
2942

30-
const resizedBase64 = canvas.toDataURL('image/jpeg', 0.8)
31-
return resizedBase64
43+
reader.onerror = error => {
44+
reject('Error reading file: ' + error)
3245
}
33-
}
34-
reader.onerror = error => {
35-
console.error('Error: ', error)
36-
}
46+
})
3747
}

static/assets/index-59f36d49.js renamed to static/assets/index-c5246876.js

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/assets/index-59f36d49.js.map renamed to static/assets/index-c5246876.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<link rel="icon" type="image/x-icon" href="{{ favicon }}" />
66
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
77
<title>{{ title }}</title>
8-
<script type="module" crossorigin src="/assets/index-59f36d49.js"></script>
8+
<script type="module" crossorigin src="/assets/index-c5246876.js"></script>
99
<link rel="stylesheet" href="/assets/index-65ec62ea.css">
1010
</head>
1111
<body>

0 commit comments

Comments
 (0)