[PR #6060] [MERGED] fix(chat): keep the aspect ratio of EXIF-rotated image attachments #7045

Closed
opened 2026-10-23 04:16:01 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/6060
Author: @Romuromylus
Created: 9/11/2026
Status: ✅ Merged
Merged: 9/11/2026
Merged by: @Romuromylus

Base: staging ← Head: fix/attachment-exif-orientation


📝 Commits (1)

  • a5fb55f fix(chat): keep the aspect ratio of EXIF-rotated image attachments

📊 Changes

3 files changed (+173 additions, -3 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/client/src/lib/chat-attachment-images.ts (+60 -3)
➕ scripts/regressions/chat-attachment-exif-orientation.regression.ts (+112 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #6053

Why this change

  • Attaching a photo in Roleplay or Conversation produced a horizontally squashed image, both in the preview and in the payload sent to the model. The attachment compressor reads the frame size from the JPEG header to pick a decode-time resize, but createImageBitmap applies the EXIF orientation before it resizes. A phone photo stored as 3000x2000 with orientation 6 decodes as a 2000x3000 portrait and was then forced into a 1536x1024 bitmap, so the compressed JPEG itself carried the wrong aspect ratio. Every later object-fit was already correct; the pixels were wrong.
  • Reproduced on the production deploy: decoding such a file plainly yields 2000x3000, while the pipeline's resizeWidth: 1536, resizeHeight: 1024 call yields the portrait content squashed into a landscape bitmap.

What changed

  • The JPEG header walk in chat-attachment-images.ts now also reads the Exif Orientation tag (APP1, both byte orders, bounds-checked) and swaps the frame width and height for the transposing orientations 5 to 8, so the resize hint matches the bitmap the browser will produce.
  • Both createImageBitmap calls pin imageOrientation: "from-image", so the behaviour does not depend on a browser default. This is belt and braces; the dimension swap is the fix.
  • Known limitation: only JPEG carries the orientation tag that browsers honour here. PNG has no orientation metadata and browsers do not auto-rotate WebP from its EXIF chunk, so those formats are unchanged.
  • New regression scripts/regressions/chat-attachment-exif-orientation.regression.ts covers orientations 1, 3, 5 to 8 in both byte orders, a non-Exif APP1 ahead of the Exif segment, a truncated Exif segment, and the untouched PNG path.
  • CHANGELOG entry.

Game Mode is not affected: its composer stores the raw file without running the compressor.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • pnpm --filter @marinara-engine/client lint green (one pre-existing unrelated warning); node scripts/run-regressions.mjs --filter chat-attachment-exif-orientation passes.
  • pnpm check green locally.
  • Verified on the production deploy with a 3000x2000 JPEG carrying EXIF orientation 6 (displays as 2000x3000 portrait). Before: decoding the file plainly gives 2000x3000, while the pipeline's resizeWidth: 1536, resizeHeight: 1024 call produced the portrait content squashed into a landscape bitmap. After deploying this branch: attaching the same file produces a 1024x1536 JPEG payload, portrait preserved.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

Payload dimensions on the production deploy for the same EXIF orientation 6 photo:

prepared attachment
before 1536x1024 (squashed landscape)
after 1024x1536 (portrait, correct)

Summary by CodeRabbit

  • Bug Fixes

    • Fixed image attachments in Roleplay and Conversation being squashed when photos include EXIF rotation metadata.
    • Attachments now preserve the correct width and height during compression, including photos taken on most phones.
  • Tests

    • Added regression coverage for rotated JPEGs, EXIF formats, truncated metadata, and PNG dimensions.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/6060 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 9/11/2026 **Status:** ✅ Merged **Merged:** 9/11/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `staging` ← **Head:** `fix/attachment-exif-orientation` --- ### 📝 Commits (1) - [`a5fb55f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/a5fb55f38481ef8b7de8084dd2a1698dfac3d807) fix(chat): keep the aspect ratio of EXIF-rotated image attachments ### 📊 Changes **3 files changed** (+173 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/client/src/lib/chat-attachment-images.ts` (+60 -3) ➕ `scripts/regressions/chat-attachment-exif-orientation.regression.ts` (+112 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #6053 ## Why this change - Attaching a photo in Roleplay or Conversation produced a horizontally squashed image, both in the preview and in the payload sent to the model. The attachment compressor reads the frame size from the JPEG header to pick a decode-time resize, but `createImageBitmap` applies the EXIF orientation before it resizes. A phone photo stored as 3000x2000 with orientation 6 decodes as a 2000x3000 portrait and was then forced into a 1536x1024 bitmap, so the compressed JPEG itself carried the wrong aspect ratio. Every later `object-fit` was already correct; the pixels were wrong. - Reproduced on the production deploy: decoding such a file plainly yields 2000x3000, while the pipeline's `resizeWidth: 1536, resizeHeight: 1024` call yields the portrait content squashed into a landscape bitmap. ## What changed - The JPEG header walk in `chat-attachment-images.ts` now also reads the Exif `Orientation` tag (APP1, both byte orders, bounds-checked) and swaps the frame width and height for the transposing orientations 5 to 8, so the resize hint matches the bitmap the browser will produce. - Both `createImageBitmap` calls pin `imageOrientation: "from-image"`, so the behaviour does not depend on a browser default. This is belt and braces; the dimension swap is the fix. - Known limitation: only JPEG carries the orientation tag that browsers honour here. PNG has no orientation metadata and browsers do not auto-rotate WebP from its EXIF chunk, so those formats are unchanged. - New regression `scripts/regressions/chat-attachment-exif-orientation.regression.ts` covers orientations 1, 3, 5 to 8 in both byte orders, a non-Exif APP1 ahead of the Exif segment, a truncated Exif segment, and the untouched PNG path. - CHANGELOG entry. Game Mode is not affected: its composer stores the raw file without running the compressor. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - `pnpm --filter @marinara-engine/client lint` green (one pre-existing unrelated warning); `node scripts/run-regressions.mjs --filter chat-attachment-exif-orientation` passes. - `pnpm check` green locally. - Verified on the production deploy with a 3000x2000 JPEG carrying EXIF orientation 6 (displays as 2000x3000 portrait). Before: decoding the file plainly gives 2000x3000, while the pipeline's `resizeWidth: 1536, resizeHeight: 1024` call produced the portrait content squashed into a landscape bitmap. After deploying this branch: attaching the same file produces a 1024x1536 JPEG payload, portrait preserved. ## Docs and release impact - [ ] No docs changes needed - [x] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) Payload dimensions on the production deploy for the same EXIF orientation 6 photo: | | prepared attachment | |---|---| | before | 1536x1024 (squashed landscape) | | after | 1024x1536 (portrait, correct) | <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed image attachments in Roleplay and Conversation being squashed when photos include EXIF rotation metadata. * Attachments now preserve the correct width and height during compression, including photos taken on most phones. * **Tests** * Added regression coverage for rotated JPEGs, EXIF formats, truncated metadata, and PNG dimensions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-23 04:16:01 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#7045
No description provided.