Repository navigation
fix(send): set Width/Height on image messages so clients keep the aspect ratio - #212
mediam4kers wants to merge 2 commits into
Conversation
…ect ratio ImageMessage was sent without Width/Height. WhatsApp clients size the chat bubble from these fields before the media is downloaded; without them the iOS app falls back to a square bubble and center-crops every portrait or landscape image (a 1080x1350 image loses ~10% at the top and bottom). Read the pixel size with image.DecodeConfig (no full decode) and set it on every ImageMessage built from an uploaded file: /send/media (file and URL, regular and newsletter), carousel image headers and image status. When the size cannot be read the fields stay nil and the message is sent as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reviewer's GuideThe PR fixes client-side image aspect-ratio rendering by reading image dimensions from file headers and attaching them to every uploaded ImageMessage path, while retaining backward-compatible behavior when dimensions cannot be determined. Sequence diagram for attaching image dimensions to media messagessequenceDiagram
participant Client
participant SendService
participant ImageHeader
participant MediaUpload
participant WhatsApp
Client->>SendService: sendMediaFileWithRetry() or sendMediaUrlWithRetry()
SendService->>ImageHeader: imageDimensions(fileData)
ImageHeader-->>SendService: width, height or nil, nil
SendService->>MediaUpload: upload image data
SendService->>WhatsApp: Send ImageMessage with Width and Height
WhatsApp-->>Client: Image bubble sized to image ratio
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pkg/sendMessage/service/send_service.go" line_range="1046" />
<code_context>
// particular). On failure jpegThumb is nil and the message is sent
// without a preview rather than failing the request.
jpegThumb := makeJPEGThumbnail(fileData, 72)
+ imgWidth, imgHeight := imageDimensions(fileData)
if isNewsletter {
// Newsletter: SEM MediaKey e FileEncSHA256
</code_context>
<issue_to_address>
**Button images use square sizing**
When a button message sends a non-square image URL, `SendButton` builds the uploaded button `ImageMessage` without calling `imageDimensions` or setting `Width` and `Height`, so clients fall back to square sizing and crop the image to the wrong aspect ratio.
Call `imageDimensions` on the downloaded image and set the returned `Width` and `Height` on the button image message.
Also at `pkg/sendMessage/service/send_service.go:1057-1058`, `pkg/sendMessage/service/send_service.go:1351`, `pkg/sendMessage/service/send_service.go:2147-2148`.
</issue_to_address>
### Comment 2
<location path="pkg/sendMessage/service/send_service.go" line_range="2148" />
<code_context>
+// center-crops portrait and landscape images. It returns nil pointers when the
+// size cannot be read, so the message is still sent (just without dimensions).
+func imageDimensions(fileData []byte) (width, height *uint32) {
+ cfg, _, err := image.DecodeConfig(bytes.NewReader(fileData))
+ if err != nil || cfg.Width < 1 || cfg.Height < 1 {
+ return nil, nil
</code_context>
<issue_to_address>
**Rotated JPEGs get wrong dimensions**
When an uploaded JPEG has EXIF orientation 5, 6, 7, or 8, `image.DecodeConfig` returns the encoded dimensions, and `imageDimensions` copies them unchanged, so the outgoing `Width` and `Height` have the wrong aspect ratio and clients size the bubble incorrectly.
Read the JPEG orientation and swap the dimensions for orientations 5 through 8 before returning them.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and the change adds image dimensions to outbound message metadata, so an incorrect or unsupported dimension could be retained in already-sent messages and cause clients to render the image with the wrong aspect ratio. Reverting stops future messages from using the values, but does not change messages that were already sent; the impact is bounded to presentation and can generally be corrected by resending the image.
Blocking findings: pkg/sendMessage/service/send_service.go:1046, pkg/sendMessage/service/send_service.go:2148
…set dimensions on buttons header image Review follow-up. Phone cameras often store a JPEG sideways with an EXIF orientation of 5-8 and clients display it rotated, so Width/Height must be the displayed size: imageDimensions now swaps them for those orientations. The JPEG preview built by makeJPEGThumbnail is also rotated/mirrored the same way (orientations 2-8), otherwise it previews sideways. The orientation is read from the APP1 EXIF segment with a small parser (no new dependency). The image header of reply-button messages was the one remaining ImageMessage without Width/Height; it now gets them too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem
ImageMessageis sent withoutWidth/Height. WhatsApp clients use these fields to size the chat bubble before the media is downloaded. When they are missing, the iOS app shows every image in a square bubble and center-crops it.We sent 1:1, 4:5, 3:4, 2:3 and 9:16 test images through
/send/media(v0.7.2) to a group. On iPhone all five rendered in the same square, and the 4:5 image lost about 10% at the top and 10% at the bottom. The same image sent from the official app renders at its real ratio.Fix
imageDimensions(fileData)reads the pixel size withimage.DecodeConfig, so only the header is parsed, not the whole image.Width/Heightare set on everyImageMessagebuilt from uploaded bytes:/send/mediaby file and by URL, regular and newsletter;makeJPEGThumbnailis rotated or mirrored the same way (orientations 2-8), so it no longer previews sideways.niland the message is sent exactly as before.Testing
orientRGBA.gofmt -lis clean andgo test ./pkg/sendMessage/service/passes. Both were run in a Docker build from tag 0.7.2 with this branch applied./send/mediaresponses now includeimageMessage.width/height, for example 1080x1350 and 1080x1920.🤖 Generated with Claude Code