mirror of
https://github.com/block/buzz.git
synced 2026-11-25 10:42:26 +00:00
Keep transparency in mobile avatar photo crops (#9319)
## Summary Choosing a transparent PNG as a profile photo on mobile produced an avatar with a solid background on every client. The crop step always re-encoded to JPEG ([`profile_avatar_crop_page.dart#L507`](44b3c4823f/mobile/lib/shared/widgets/profile_avatar_crop_page.dart (L507))), and `ProfileImageAvatarDraft` always uploaded with `mimeType: 'image/jpeg'` ([`profile_avatar_draft.dart#L51`](44b3c4823f/mobile/lib/features/profile/profile_avatar_draft.dart (L51))). - The crop encodes PNG when the 512px result has an alpha channel and any pixel is transparent, and JPEG otherwise. Opaque photos, including 16-bit and low-bit-depth PNGs whose decoder reports zero alpha, keep the smaller JPEG output. - The draft labels its upload from the bytes' signature, using the existing detector in `media_upload.dart`, now public as `detectImageMimeType`. ### Related issue None found. ### Testing - `uploads a transparent photo crop as PNG` picks a transparent PNG, crops and saves it, and checks the upload is `image/png` with a transparent corner pixel. It fails on `main` with `Expected: 'image/png' Actual: 'image/jpeg'`. - `uploads an opaque 16-bit photo crop as JPEG` fails without the alpha-channel guard, and the existing opaque-photo test now checks the upload stays `image/jpeg`. - On an iOS simulator against a local relay, I picked a transparent PNG with an opaque red disc through the native photo picker, cropped it and saved it. On `main` the stored avatar is a 512x512 JPEG with white corners. With this change it is a 512x512 RGBA PNG with transparent corners and the same red center. | Before (`main`) | After | | --- | --- | |  |  | buzz-review-completed 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: John Tennant <jtennant@squareup.com> Signed-off-by: Tom Brow <tomb@squareup.com> Co-authored-by: Maeve <be734facf808af9bc7720a1cb69aeb2a3d136149938eedf4d0667a70ef433961@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Tom Brow <tomb@squareup.com> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
This commit is contained in:
committed by
GitHub
parent
6f1eabe4e8
commit
21802f019e
@@ -713,7 +713,7 @@ class MediaUploadService {
|
||||
_PreparedUploadImage _buildPreparedUploadImage(Uint8List bytes) {
|
||||
return _PreparedUploadImage(
|
||||
bytes: bytes,
|
||||
mimeType: _detectImageMimeType(bytes),
|
||||
mimeType: detectImageMimeType(bytes),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -804,13 +804,14 @@ String _sha256Hex(Uint8List bytes) => crypto.sha256.convert(bytes).toString();
|
||||
|
||||
String? _tryDetectImageMimeType(Uint8List bytes) {
|
||||
try {
|
||||
return _detectImageMimeType(bytes);
|
||||
return detectImageMimeType(bytes);
|
||||
} on Exception {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
String _detectImageMimeType(Uint8List bytes) {
|
||||
/// Returns the image MIME type of [bytes] from their signature.
|
||||
String detectImageMimeType(Uint8List bytes) {
|
||||
if (_startsWith(bytes, const [0xff, 0xd8, 0xff])) {
|
||||
return 'image/jpeg';
|
||||
}
|
||||
|
||||
@@ -545,5 +545,8 @@ Uint8List _cropAvatar(_CropRequest request) {
|
||||
width: _cropOutputSize,
|
||||
height: _cropOutputSize,
|
||||
);
|
||||
return image.encodeJpg(resized, quality: 90);
|
||||
return resized.hasAlpha &&
|
||||
resized.any((pixel) => pixel.a < pixel.maxChannelValue)
|
||||
? image.encodePng(resized, singleFrame: true)
|
||||
: image.encodeJpg(resized, quality: 90);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user