[fix] encoding images

This commit is contained in:
kwaroran
2023-11-18 04:05:49 +09:00
parent d69c8eb6b6
commit 37455894dc
4 changed files with 33 additions and 28 deletions

View File

@@ -86,4 +86,20 @@ export async function getInlayImage(id: string){
export function supportsInlayImage(){
const db = get(DataBase)
return db.aiModel.startsWith('gptv') || (db.aiModel === 'reverse_proxy' && db.proxyRequestModel?.startsWith('gptv'))
}
export async function reencodeImage(img:Uint8Array){
const canvas = document.createElement('canvas')
const imgObj = new Image()
imgObj.src = URL.createObjectURL(new Blob([img], {type: `image/png`}))
await imgObj.decode()
let drawHeight = imgObj.height
let drawWidth = imgObj.width
canvas.width = drawWidth
canvas.height = drawHeight
const ctx = canvas.getContext('2d')
ctx.drawImage(imgObj, 0, 0, drawWidth, drawHeight)
const b64 = canvas.toDataURL('image/png').split(',')[1]
const b = Buffer.from(b64, 'base64')
return b
}