From 96c0e6de0eb4c1b38dd09399eb8be920641b5e87 Mon Sep 17 00:00:00 2001 From: amay077 Date: Mon, 4 Nov 2024 19:37:08 +0900 Subject: [PATCH] =?UTF-8?q?bsky=20=E3=81=B8=E3=81=AE=E5=A4=A7=E3=81=8D?= =?UTF-8?q?=E3=81=AA=E7=94=BB=E5=83=8F=E3=82=92=E6=8A=95=E7=A8=BF=E3=81=99?= =?UTF-8?q?=E3=82=8B=E6=99=82=E3=81=AF1MB=E3=81=AB=E3=81=AA=E3=82=8B?= =?UTF-8?q?=E7=A8=8B=E5=BA=A6=E3=81=AB=E8=A7=A3=E5=83=8F=E5=BA=A6=E3=82=92?= =?UTF-8?q?=E4=B8=8B=E3=81=92=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97?= =?UTF-8?q?=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/MainContent.ts | 52 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/lib/MainContent.ts b/src/lib/MainContent.ts index 33f2802..19c86cb 100644 --- a/src/lib/MainContent.ts +++ b/src/lib/MainContent.ts @@ -177,13 +177,58 @@ const postToBluesky = async (text: string, imageDataURLs: string[]): Promise => { + const canvas = document.createElement('canvas'); + const ctx = canvas.getContext('2d'); + if (ctx == null) { + return null; + } + + const img = new Image(); + img.src = URL.createObjectURL(file); + await new Promise(r => img.onload = r); + canvas.width = img.width; + canvas.height = img.height; + ctx.drawImage(img, 0, 0); + const scale = Math.min(max / img.width, max / img.height); + canvas.width = img.width * scale; + canvas.height = img.height * scale; + ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height); + const dataUrl = canvas.toDataURL(file.type); + const arr2 = await fetch(dataUrl).then(r => r.blob()).then(r => r.arrayBuffer()); + return arr2; + } + + const MAX_SIZE = 1000000; const embedImages = await (async () => { const images = []; for (const image of imageDataURLs) { const file = await url2File(image, 'image'); - const arr = await file.arrayBuffer(); - - const dataArray: Uint8Array = new Uint8Array(arr); + + const { width, height } = await getWidHei(image); + let zoomRate = MAX_SIZE / file.size; + zoomRate = Math.sqrt(zoomRate) + + let resizedArr = await resize(file, width * zoomRate); + if (resizedArr == null) { + continue; + } + + if (resizedArr.byteLength > MAX_SIZE) { + resizedArr = await resize(file, width * zoomRate * 0.9); + if (resizedArr == null) { + continue; + } + } + + if (resizedArr.byteLength > MAX_SIZE) { + resizedArr = await resize(file, width * zoomRate * 0.7); + if (resizedArr == null) { + continue; + } + } + + const dataArray: Uint8Array = new Uint8Array(resizedArr); const { data: result } = await agent.uploadBlob( dataArray, { @@ -191,7 +236,6 @@ const postToBluesky = async (text: string, imageDataURLs: string[]): Promise