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' ;
77import { joinPath } from '../../../../base/common/resources.js' ;
88import { URI } from '../../../../base/common/uri.js' ;
99import { 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):
8185export 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