Skip to content

Commit 6d27f12

Browse files
authored
make sure to resize image to the same mimetype (microsoft#258891)
* make sure to resize image to the same mimetype * default to either jpeg or png
1 parent 35b1b11 commit 6d27f12

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/vs/platform/imageResize/browser/imageResizeService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ export class ImageResizeService implements IImageResizeService {
6262
const ctx = canvas.getContext('2d');
6363
if (ctx) {
6464
ctx.drawImage(img, 0, 0, width, height);
65-
canvas.toBlob((blob) => {
65+
66+
const jpegTypes = ['image/jpeg', 'image/jpg'];
67+
const outputMimeType = mimeType && jpegTypes.includes(mimeType) ? 'image/jpeg' : 'image/png';
68+
69+
canvas.toBlob(blob => {
6670
if (blob) {
6771
const reader = new FileReader();
6872
reader.onload = () => {
@@ -73,7 +77,7 @@ export class ImageResizeService implements IImageResizeService {
7377
} else {
7478
reject(new Error('Failed to create blob from canvas'));
7579
}
76-
}, mimeType || 'image/png');
80+
}, outputMimeType);
7781
} else {
7882
reject(new Error('Failed to get canvas context'));
7983
}

src/vs/workbench/contrib/chat/browser/imageUtils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { VSBuffer } from '../../../../base/common/buffer.js';
6+
import { decodeBase64, VSBuffer } from '../../../../base/common/buffer.js';
77
import { joinPath } from '../../../../base/common/resources.js';
88
import { URI } from '../../../../base/common/uri.js';
99
import { IFileService } from '../../../../platform/files/common/files.js';
@@ -55,7 +55,11 @@ export async function resizeImage(data: Uint8Array | string, mimeType?: string):
5555
const ctx = canvas.getContext('2d');
5656
if (ctx) {
5757
ctx.drawImage(img, 0, 0, width, height);
58-
canvas.toBlob((blob) => {
58+
59+
const jpegTypes = ['image/jpeg', 'image/jpg'];
60+
const outputMimeType = mimeType && jpegTypes.includes(mimeType) ? 'image/jpeg' : 'image/png';
61+
62+
canvas.toBlob(blob => {
5963
if (blob) {
6064
const reader = new FileReader();
6165
reader.onload = () => {
@@ -66,7 +70,7 @@ export async function resizeImage(data: Uint8Array | string, mimeType?: string):
6670
} else {
6771
reject(new Error('Failed to create blob from canvas'));
6872
}
69-
}, 'image/png');
73+
}, outputMimeType);
7074
} else {
7175
reject(new Error('Failed to get canvas context'));
7276
}
@@ -81,7 +85,7 @@ export async function resizeImage(data: Uint8Array | string, mimeType?: string):
8185
export function convertStringToUInt8Array(data: string): Uint8Array {
8286
const base64Data = data.includes(',') ? data.split(',')[1] : data;
8387
if (isValidBase64(base64Data)) {
84-
return Uint8Array.from(atob(base64Data), char => char.charCodeAt(0));
88+
return decodeBase64(base64Data).buffer;
8589
}
8690
return new TextEncoder().encode(data);
8791
}

0 commit comments

Comments
 (0)