Skip to content

fix: size the cropped image at its natural resolution#32

Open
paodb wants to merge 1 commit into
masterfrom
issue-26
Open

fix: size the cropped image at its natural resolution#32
paodb wants to merge 1 commit into
masterfrom
issue-26

Conversation

@paodb

@paodb paodb commented Jun 5, 2026

Copy link
Copy Markdown
Member

Note: While investigating #26 it turned out that #21 has the same root
cause — the cropped output was sized wrong — and both are resolved by the same
change, so they are addressed together in this single PR.

Problem

The cropped output was sized in the image's displayed (rendered) pixels and
multiplied by window.devicePixelRatio, instead of using the image's natural
resolution. This caused two related bugs:

Fix

Size the output canvas and the drawImage destination in the crop region's
natural pixels (ccrop.width * scaleX / ccrop.height * scaleY) and drop
the devicePixelRatio multiplier. The cropped image now keeps the configured
dimensions regardless of how the image is displayed, and the original is used
verbatim. The same correction applies to circular crops, which previously used
the displayed size because the branch reset the canvas context mid-draw.

Note

Cropped images are now returned at their natural resolution, so their pixel
dimensions may differ from the previous (incorrect) output. No public API
changes — method signatures and events are unchanged.

Closes #26
Closes #21

Summary by CodeRabbit

  • Bug Fixes
    • Image crop export now uses the source image’s natural pixel dimensions to prevent unwanted scaling and preserve output resolution.
    • Circular crops render and export at the correct size and aspect, avoiding distorted or blurred results.

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2b809e7c-fb9f-48cf-8875-213e69b7c583

📥 Commits

Reviewing files that changed from the base of the PR and between decf9dd and 9ecadc4.

📒 Files selected for processing (1)
  • src/main/resources/META-INF/resources/frontend/src/image-crop.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/resources/META-INF/resources/frontend/src/image-crop.tsx

Walkthrough

The PR changes _updateCroppedImage to export cropped images at the source image’s natural pixel dimensions by computing outWidth/outHeight from the pixel crop and natural/display sizes, resizing the canvas to those values, adjusting circular-clip geometry, and drawing with the natural-size destination.

Changes

Image natural-pixel export pipeline

Layer / File(s) Summary
Canvas sizing and export logic
src/main/resources/META-INF/resources/frontend/src/image-crop.tsx
JSDoc updated. _updateCroppedImage computes outWidth/outHeight from the crop rectangle and image natural/display dimensions, sets the canvas to those natural-pixel sizes, removes devicePixelRatio transform logic, adjusts circular-crop arc geometry to the natural dimensions, and uses outWidth/outHeight as the drawImage destination size.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • javier-godoy
  • mlopezFC
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: size the cropped image at its natural resolution' clearly and concisely describes the main change: correcting how the cropped image is sized by using natural pixel resolution instead of display scaling.
Linked Issues check ✅ Passed The code changes directly address both linked issues: the method now uses natural pixel dimensions (convertToPixelCrop with scale factors) instead of scaled display pixels, ensuring crops match configured sizes (#26) and preventing upsampling on high-density displays (#21).
Out of Scope Changes check ✅ Passed All changes to _updateCroppedImage are directly focused on fixing the sizing logic by using natural resolution dimensions and removing devicePixelRatio multipliers; no unrelated modifications or scope creep detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-26

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/resources/META-INF/resources/frontend/src/image-crop.tsx`:
- Around line 164-180: Preconfigured pixel crops (crop.unit === 'px') are being
converted to natural pixels in onImageLoad() and then again scaled by
scaleX/scaleY in _updateCroppedImage(), causing double-scaling; fix by
normalizing incoming pixel-based crop to the image's rendered/display
coordinates before passing it into ReactCrop (or by keeping crop state
consistently in natural pixels end-to-end). Concretely, update onImageLoad() to
detect crop.unit === 'px' and convert the provided crop from natural pixels to
displayed pixels (divide width/height/x/y by scaleX/scaleY) before calling
setCrop/react-state for ReactCrop, or alternatively change state handling so
onImageLoad() stores the crop in natural pixels and remove the extra
scaleX/scaleY multiplication from _updateCroppedImage(); ensure both onImageLoad
and _updateCroppedImage use the same pixel-space convention so programmatic
crops export at the intended dimensions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4310bc0f-9153-492b-91ff-61ef45977caa

📥 Commits

Reviewing files that changed from the base of the PR and between fc6a0af and decf9dd.

📒 Files selected for processing (1)
  • src/main/resources/META-INF/resources/frontend/src/image-crop.tsx

Comment thread src/main/resources/META-INF/resources/frontend/src/image-crop.tsx Outdated
@paodb

paodb commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

@CodeRabbit full review

@coderabbitai

coderabbitai Bot commented Jun 5, 2026

Copy link
Copy Markdown
✅ Action performed

Full review finished.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/main/resources/META-INF/resources/frontend/src/image-crop.tsx`:
- Around line 164-180: The existing block incorrectly claims that a crop
measured in "px" always yields the same configured output size; update the
comment to state that react-image-crop reports the crop rectangle in rendered
(display) pixels, so when we map that rectangle to the source using
scaleX/scaleY and draw to an offscreen output canvas the resulting pixel
dimensions can differ if the image was scaled for display. Remove the assertion
that a configured 500x500 always yields 500x500, clarify that we intentionally
do NOT multiply output dimensions by window.devicePixelRatio (so exported pixels
reflect the source resolution, not DPR upsampling), and mention that preserving
configured-size behavior is a separate concern for future %-based crop state
work.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f017d9b1-5248-4cee-9194-0168a9318aa7

📥 Commits

Reviewing files that changed from the base of the PR and between fc6a0af and decf9dd.

📒 Files selected for processing (1)
  • src/main/resources/META-INF/resources/frontend/src/image-crop.tsx

Comment thread src/main/resources/META-INF/resources/frontend/src/image-crop.tsx Outdated
The cropped output was derived from the displayed image and scaled by
devicePixelRatio, so crops were too small when the image was scaled down
to fit and too large on high-density displays.

Close #26
Close #21
@sonarqubecloud

sonarqubecloud Bot commented Jun 8, 2026

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@paodb

paodb commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@paodb paodb marked this pull request as ready for review June 8, 2026 12:25
@paodb paodb requested review from javier-godoy and scardanzan June 8, 2026 12:25
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.

Panorama images are not cropped correctly (cropped image too small) Images are silently resampled to bigger Size

1 participant