Skip to content

fix: keep the color profile of uploaded images - #815

Draft
eas4ai wants to merge 1 commit into
dev-null-sh:productionfrom
eas4ai:fix/keep-image-color-profile
Draft

eas4ai wants to merge 1 commit into
dev-null-sh:productionfrom
eas4ai:fix/keep-image-color-profile

Conversation

@eas4ai

@eas4ai eas4ai commented Oct 20, 2026 •

Copy link
Copy Markdown

Summary

Fixes #806.

Uploaded images are re-encoded as WebP with GD, which drops the ICC color profile. A Display P3 screenshot keeps its P3 pixel values but loses the tag that says so, so browsers read the pixels as sRGB and the colors look washed out.

This draft carries the profile over instead of converting pixels: CreateCommitImageFiles extracts the profile from the upload (JPEG APP2, including profiles split over several segments; PNG iCCP; WebP ICCP), validates it, re-encodes with GD exactly as today, then muxes the validated profile into the stored WebP as a VP8X ICCP chunk. Every upload path goes through this action (commits, scheduled commits, message images), so one change covers all of them.

@anttikissa you mentioned you might be looking into a fix as well — happy to close this in favor of yours if you have one in flight. Drafting this so the approach can be weighed first.

Display P3 patches rendered in Chrome on macOS: the original upload and the WebP stored by this PR match; the same WebP with its profile dropped, as GD stores it today, renders washed out

Screenshot taken in Chrome on macOS: the same Display P3 patches as the original upload, as stored by this PR, and as stored today with the profile dropped. Rows 1 and 2 match; row 3 is the washed-out rendering from #806.

Why transplanting instead of converting to sRGB

Converting P3 pixels to sRGB needs a color-managed transform. GD has none, and this file deliberately uses GD because Imagick writes source metadata back out. Transplanting needs no pixel math and no new extension: the validated profile is written into the WebP container around GD's output (a plain image gains the VP8X header; an image with alpha already has it, so only the ICC flag and chunk are added). Color-managed browsers then render the colors as authored.

Safety

Everything else is still stripped. A profile is carried only if it is a plausible display profile: 128 bytes to 1 MB, declared size matches its length, mntr device class, RGB color space, acsp signature. Anything else is dropped, including a PNG profile that does not inflate. The existing "drops anything smuggled alongside the pixels" test still passes.

Tests

New coverage in CreateCommitImagesTest, in the same segment-injection style as the EXIF orientation test:

  • keeps JPEG, PNG, transparent PNG, and WebP profiles, verifying the stored WebP carries the exact bytes and still decodes at the right size
  • keeps a profile split across JPEG segments and one of an odd length
  • drops bad-signature, size-mismatched, print, non-RGB, truncated, and oversized profiles, and PNG profiles that do not inflate
  • leaves untagged uploads untagged

Verified locally:

  • pest tests/Unit/Actions/CreateCommitImagesTest.php — 35 passed; CreateCommitImageFiles coverage 100.0%
  • Full suite — 3,533 passed, 1 failed: DatabaseSeederTest, which also fails on unmodified production in my environment, so it looks pre-existing and unrelated
  • pint --parallel --test — 940 files pass; rector --dry-run — clean; phpstan (level max) — no errors; type coverage — 100.0%
  • bun run test:unit — 101 pass; bun run test:lint — 0 errors; bun run test:types — clean
  • A file produced by the action was checked with libwebp's own tools: webpinfo reports ICCP: 1 with no errors and dwebp decodes it
  • End to end on macOS 26 (Apple Silicon, ColorSync via sips): a screenshot-style PNG tagged Display P3 on the Mac was uploaded through the action, and the stored WebP reports profile: Display P3 in sips. Converting the original and the stored WebP to sRGB on the Mac gives the same colors (patch samples within 1 per channel, e.g. rgb(206,89,54) vs rgb(205,89,55)), while the same WebP with the profile stripped — today's GD output — renders as the raw untagged values (rgb(191,96,65))

Docs

KNOWLEDGE_BASE.md now says the color profile is kept while other metadata is stripped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uploaded images lose their color profile and look washed out on a Mac

1 participant