From 193a0c60f8e40f20d7c5c0035a4dcc81be8bb897 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 5 Jul 2023 13:14:30 +0200 Subject: [PATCH] Photo: Scale down an image for a local message thumbnail (#3423) --- src/api/gramjs/apiBuilders/messages.ts | 2 +- .../middle/composer/helpers/buildAttachment.ts | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/api/gramjs/apiBuilders/messages.ts b/src/api/gramjs/apiBuilders/messages.ts index 4fbff39b1..cb1480b85 100644 --- a/src/api/gramjs/apiBuilders/messages.ts +++ b/src/api/gramjs/apiBuilders/messages.ts @@ -1439,7 +1439,7 @@ function buildUploadingMedia( photo: { id: LOCAL_MEDIA_UPLOADING_TEMP_ID, sizes: [], - thumbnail: { width, height, dataUri: blobUrl }, + thumbnail: { width, height, dataUri: previewBlobUrl || blobUrl }, blobUrl, isSpoiler: shouldSendAsSpoiler, }, diff --git a/src/components/middle/composer/helpers/buildAttachment.ts b/src/components/middle/composer/helpers/buildAttachment.ts index 7b2feb10c..950d80ec1 100644 --- a/src/components/middle/composer/helpers/buildAttachment.ts +++ b/src/components/middle/composer/helpers/buildAttachment.ts @@ -14,6 +14,7 @@ import { import { scaleImage } from '../../../../util/imageResize'; const MAX_QUICK_IMG_SIZE = 1280; // px +const MAX_THUMB_IMG_SIZE = 40; // px const MAX_ASPECT_RATIO = 20; const FILE_EXT_REGEX = /\.[^/.]+$/; @@ -53,7 +54,14 @@ export default async function buildAttachment( quick = { width, height }; } - previewBlobUrl = blobUrl; + const shouldShrinkPreview = Math.max(width, height) > MAX_THUMB_IMG_SIZE; + if (shouldShrinkPreview) { + previewBlobUrl = await scaleImage( + blobUrl, MAX_THUMB_IMG_SIZE / Math.max(width, height), 'image/jpeg', + ); + } else { + previewBlobUrl = blobUrl; + } } else if (SUPPORTED_VIDEO_CONTENT_TYPES.has(mimeType)) { const { videoWidth: width, videoHeight: height, duration } = await preloadVideo(blobUrl); shouldSendAsFile = !validateAspectRatio(width, height);