fix(utils): preserve JPEG format when compressing uploaded images#13585
Conversation
|
@octo-patch is attempting to deploy a commit to the LobeHub OSS Team on Vercel. A member of the Team first needs to authorize it. |
|
👋 Assigning reviewers for this PR. @arvinxx @nekomeowww — this fix touches Summary of changes:
|
|
@octo-patch please add the new tests |
Images with dimensions > 1920px were always re-encoded as PNG regardless of original format, inflating small JPEGs (100–200 KB) to 1 MB+ because PNG is lossless while JPEG is lossy. Fix: pass file.type to compressImage(), encode JPEG inputs as JPEG at 0.85 quality (not PNG), and derive File MIME type from the data URL instead of hardcoding 'image/png'. PNG and WebP inputs still compress to PNG as before. Fixes lobehub#13485
1be925f to
99e8ae5
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## canary #13585 +/- ##
===========================================
- Coverage 85.99% 68.11% -17.88%
===========================================
Files 621 2271 +1650
Lines 51799 194752 +142953
Branches 7944 19599 +11655
===========================================
+ Hits 44545 132665 +88120
- Misses 7123 61956 +54833
Partials 131 131
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Per @tjx666's request on lobehub#13585. Adds explicit coverage for the JPEG format-preservation behaviour: - compressImage with type='image/jpeg' calls toDataURL with quality 0.85 - compressImage with type='image/png' calls toDataURL without a quality arg - compressImage with no type defaults to PNG - compressImageFile preserves JPEG inputs as image/jpeg (regression fence for the previously hardcoded 'image/png' MIME type in dataUrlToFile) - compressImageFile keeps WebP inputs as PNG (documents the fallback) The existing PNG tests are preserved to guard against regression in the lossless path.
|
@tjx666 added a test commit (671ca6e). Coverage now explicitly fences the JPEG format-preservation behaviour:
Existing PNG tests preserved. |
|
❤️ Great PR @octo-patch ❤️ The growth of project is inseparable from user feedback and contribution, thanks for your contribution! If you are interesting with the lobehub developer community, please join our discord and then dm @arvinxx or @canisminor1990. They will invite you to our private developer channel. We are talking about the lobe-chat development or sharing ai newsletter around the world. |
…behub#13585) * 🐛 fix(utils): preserve JPEG format when compressing uploaded images Images with dimensions > 1920px were always re-encoded as PNG regardless of original format, inflating small JPEGs (100–200 KB) to 1 MB+ because PNG is lossless while JPEG is lossy. Fix: pass file.type to compressImage(), encode JPEG inputs as JPEG at 0.85 quality (not PNG), and derive File MIME type from the data URL instead of hardcoding 'image/png'. PNG and WebP inputs still compress to PNG as before. Fixes lobehub#13485 * ✅ test(utils): add tests for JPEG format preservation in compressImage Per @tjx666's request on lobehub#13585. Adds explicit coverage for the JPEG format-preservation behaviour: - compressImage with type='image/jpeg' calls toDataURL with quality 0.85 - compressImage with type='image/png' calls toDataURL without a quality arg - compressImage with no type defaults to PNG - compressImageFile preserves JPEG inputs as image/jpeg (regression fence for the previously hardcoded 'image/png' MIME type in dataUrlToFile) - compressImageFile keeps WebP inputs as PNG (documents the fallback) The existing PNG tests are preserved to guard against regression in the lossless path. --------- Co-authored-by: octo-patch <octo-patch@github.com> Co-authored-by: YuTengjing <ytj2713151713@gmail.com>
…behub#13585) * 🐛 fix(utils): preserve JPEG format when compressing uploaded images Images with dimensions > 1920px were always re-encoded as PNG regardless of original format, inflating small JPEGs (100–200 KB) to 1 MB+ because PNG is lossless while JPEG is lossy. Fix: pass file.type to compressImage(), encode JPEG inputs as JPEG at 0.85 quality (not PNG), and derive File MIME type from the data URL instead of hardcoding 'image/png'. PNG and WebP inputs still compress to PNG as before. Fixes lobehub#13485 * ✅ test(utils): add tests for JPEG format preservation in compressImage Per @tjx666's request on lobehub#13585. Adds explicit coverage for the JPEG format-preservation behaviour: - compressImage with type='image/jpeg' calls toDataURL with quality 0.85 - compressImage with type='image/png' calls toDataURL without a quality arg - compressImage with no type defaults to PNG - compressImageFile preserves JPEG inputs as image/jpeg (regression fence for the previously hardcoded 'image/png' MIME type in dataUrlToFile) - compressImageFile keeps WebP inputs as PNG (documents the fallback) The existing PNG tests are preserved to guard against regression in the lossless path. --------- Co-authored-by: octo-patch <octo-patch@github.com> Co-authored-by: YuTengjing <ytj2713151713@gmail.com>
Fixes #13485
Problem
After v2.1.47, images with dimensions exceeding 1920 px (e.g. a 2048×1536 px JPEG shot on a phone at 150 KB) were re-encoded as PNG regardless of the original format. PNG is lossless, so a compressed JPEG blows up to 1 MB+ after this round-trip:
Solution
file.typetocompressImage()— so the function knows the original format.canvas.toDataURL('image/jpeg', 0.85)produces files that are smaller than PNG for photographic content.FileMIME type from the data URL — instead of hardcoding'image/png', parse the actual type out ofdata:<type>;base64,...so content and declared type always match.PNG and WebP inputs still compress to PNG (lossless, consistent behavior).
Testing
packages/utils/src/compressImage.test.tscontinue to pass.