From cdf4e4cde93504c0927f6a79126d33ac461a3cb6 Mon Sep 17 00:00:00 2001 From: Hong Minhee Date: Wed, 18 Dec 2024 14:19:30 +0900 Subject: [PATCH] Fix thumbnail's orientation https://github.com/dahlia/hollo/issues/76 --- src/media.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/media.ts b/src/media.ts index 30925322..5c73fdf2 100644 --- a/src/media.ts +++ b/src/media.ts @@ -23,12 +23,21 @@ export async function uploadThumbnail( thumbnailArea = DEFAULT_THUMBNAIL_AREA, ): Promise { const originalMetadata = await original.metadata(); - const thumbnailSize = calculateThumbnailSize( - originalMetadata.width!, - originalMetadata.height!, - thumbnailArea, - ); - const thumbnail = await original.resize(thumbnailSize).webp().toBuffer(); + let width = originalMetadata.width!; + let height = originalMetadata.height!; + if (originalMetadata.orientation !== 1) { + // biome-ignore lint/style/noParameterAssign: + original = original.clone(); + original.rotate(); + if (originalMetadata.orientation !== 3) { + [width, height] = [height, width]; + } + } + const thumbnailSize = calculateThumbnailSize(width, height, thumbnailArea); + const thumbnail = await original + .resize(thumbnailSize) + .webp({ nearLossless: true }) + .toBuffer(); const content = new Uint8Array(thumbnail); try { await disk.put(`media/${id}/thumbnail.webp`, content, {