forked from lobehub/lobe-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡️ perf: Optimize the image upload size for
gpt-4-vision
(lobehub#669)
* ♻️ refactor: `FileItem` changed to `ImageFileItem` * ⚡️ perf: Optimize the image upload size for `gpt-4-vision` * 🔧 test: add Test Configuration * 🧪 test: add case * ✏️ chore: typos --------- Co-authored-by: wuxh <[email protected]>
- Loading branch information
Showing
9 changed files
with
135 additions
and
8 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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import compressImage from './compressImage'; | ||
|
||
const getContextSpy = vi.spyOn(global.HTMLCanvasElement.prototype, 'getContext'); | ||
const drawImageSpy = vi.spyOn(CanvasRenderingContext2D.prototype, 'drawImage'); | ||
|
||
beforeEach(() => { | ||
getContextSpy.mockClear(); | ||
drawImageSpy.mockClear(); | ||
}); | ||
|
||
describe('compressImage', () => { | ||
it('should compress image with maxWidth', () => { | ||
const img = document.createElement('img'); | ||
img.width = 3000; | ||
img.height = 2000; | ||
|
||
const r = compressImage({ img }); | ||
|
||
expect(r).toMatch(/^data:image\/webp;base64,/); | ||
|
||
expect(getContextSpy).toBeCalledTimes(1); | ||
expect(getContextSpy).toBeCalledWith('2d'); | ||
|
||
expect(drawImageSpy).toBeCalledTimes(1); | ||
expect(drawImageSpy).toBeCalledWith(img, 0, 0, 3000, 2000, 0, 0, 2160, 1440); | ||
}); | ||
|
||
it('should compress image with maxHeight', () => { | ||
const img = document.createElement('img'); | ||
img.width = 2000; | ||
img.height = 3000; | ||
|
||
const r = compressImage({ img }); | ||
|
||
expect(r).toMatch(/^data:image\/webp;base64,/); | ||
|
||
expect(getContextSpy).toBeCalledTimes(1); | ||
expect(getContextSpy).toBeCalledWith('2d'); | ||
|
||
expect(drawImageSpy).toBeCalledTimes(1); | ||
expect(drawImageSpy).toBeCalledWith(img, 0, 0, 2000, 3000, 0, 0, 1440, 2160); | ||
}); | ||
|
||
it('should not compress image', () => { | ||
const img = document.createElement('img'); | ||
img.width = 2000; | ||
img.height = 2000; | ||
|
||
const r = compressImage({ img }); | ||
|
||
expect(r).toMatch(/^data:image\/webp;base64,/); | ||
|
||
expect(getContextSpy).toBeCalledTimes(1); | ||
expect(getContextSpy).toBeCalledWith('2d'); | ||
|
||
expect(drawImageSpy).toBeCalledTimes(1); | ||
expect(drawImageSpy).toBeCalledWith(img, 0, 0, 2000, 2000, 0, 0, 2000, 2000); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const compressImage = ({ img, type = 'image/webp' }: { img: HTMLImageElement; type?: string }) => { | ||
// 设置最大宽高 | ||
const maxWidth = 2160; | ||
const maxHeight = 2160; | ||
let width = img.width; | ||
let height = img.height; | ||
|
||
if (width > height && width > maxWidth) { | ||
// 如果图片宽度大于高度且大于最大宽度限制 | ||
width = maxWidth; | ||
height = Math.round((maxWidth / img.width) * img.height); | ||
} else if (height > width && height > maxHeight) { | ||
// 如果图片高度大于宽度且大于最大高度限制 | ||
height = maxHeight; | ||
width = Math.round((maxHeight / img.height) * img.width); | ||
} | ||
|
||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D; | ||
|
||
canvas.width = width; | ||
canvas.height = height; | ||
|
||
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height); | ||
|
||
return canvas.toDataURL(type); | ||
}; | ||
|
||
export default compressImage; |
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