-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Resize image before conversation and save history (#1116)
Co-authored-by: Ian Seabock (Centific Technologies Inc) <v-ianseabock@microsoft.com>
- Loading branch information
Showing
6 changed files
with
63 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,47 @@ | ||
export const resizeImage = (file: Blob, maxWidth: number, maxHeight: number) => { | ||
const img = new Image() | ||
const reader = new FileReader() | ||
|
||
reader.readAsDataURL(file) | ||
reader.onloadend = () => { | ||
img.src = reader.result as string | ||
img.onload = () => { | ||
const canvas = document.createElement('canvas') | ||
const ctx = canvas.getContext('2d') | ||
|
||
let { width, height } = img | ||
|
||
if (width > maxWidth || height > maxHeight) { | ||
if (width > height) { | ||
height *= maxWidth / width | ||
width = maxWidth | ||
} else { | ||
width *= maxHeight / height | ||
height = maxHeight | ||
export const resizeImage = (file: Blob, maxWidth: number, maxHeight: number): Promise<string> => { | ||
return new Promise((resolve, reject) => { | ||
const img = new Image() | ||
const reader = new FileReader() | ||
|
||
reader.readAsDataURL(file) | ||
reader.onloadend = () => { | ||
img.src = reader.result as string | ||
img.onload = () => { | ||
const canvas = document.createElement('canvas') | ||
const ctx = canvas.getContext('2d') | ||
|
||
let { width, height } = img | ||
|
||
// Calculate the new dimensions | ||
if (width > maxWidth || height > maxHeight) { | ||
if (width > height) { | ||
height = (maxWidth / width) * height | ||
width = maxWidth | ||
} else { | ||
width = (maxHeight / height) * width | ||
height = maxHeight | ||
} | ||
} | ||
|
||
canvas.width = width | ||
canvas.height = height | ||
|
||
if (ctx) { | ||
ctx.drawImage(img, 0, 0, width, height) | ||
} | ||
|
||
// Convert the canvas to a base64 string | ||
const resizedBase64 = canvas.toDataURL('image/jpeg', 0.8) | ||
resolve(resizedBase64) | ||
} | ||
|
||
canvas.width = width | ||
canvas.height = height | ||
if (ctx) { | ||
ctx.drawImage(img, 0, 0, width, height) | ||
img.onerror = error => { | ||
reject('Error loading image: ' + error) | ||
} | ||
} | ||
|
||
const resizedBase64 = canvas.toDataURL('image/jpeg', 0.8) | ||
return resizedBase64 | ||
reader.onerror = error => { | ||
reject('Error reading file: ' + error) | ||
} | ||
} | ||
reader.onerror = error => { | ||
console.error('Error: ', error) | ||
} | ||
}) | ||
} |
26 changes: 13 additions & 13 deletions
26
static/assets/index-59f36d49.js → static/assets/index-c5246876.js
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
static/assets/index-59f36d49.js.map → static/assets/index-c5246876.js.map
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters