# Crop and Rotate Tool

## Objective

Let people crop, straighten, and rotate an image before it is saved.

An in-app editor that lets a user set a crop rectangle, rotate in ninety-degree steps, and straighten by a small angle before the image is stored.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Extend the app's existing upload flow rather than adding a separate editor screen. If avatar upload or focal-point cropping already exists, this is the same tool with more controls, not a second one.
2. Read the image's orientation metadata and apply it before showing the canvas, so photos taken on a phone are not presented sideways and then cropped sideways.
3. Store the crop and rotation as parameters against the untouched original, so the user can reopen the tool later, adjust, and get a different result without a fresh upload.
4. When a surface requires a fixed aspect ratio, lock the crop rectangle to it and show that constraint in the interface rather than accepting a free crop and silently trimming it afterwards.
5. Do not load a full-resolution photo into the canvas for editing. Work on a downscaled preview and apply the same transform to the original when saving, or large images will fail on modest devices.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Photos carry orientation metadata that many rendering paths ignore. Normalise the image up front, otherwise the preview, the crop rectangle, and the saved output will disagree about which way is up.
- Where a fixed aspect ratio is required, the crop handles must be constrained to it during the drag, not corrected on save. A user who framed a shot and then saw it trimmed will not trust the tool.
- Destroying the original means a crop can never be widened again. Keep the source file and store the transform separately, so reopening the tool starts from the full image.
- A crop of zero width or height, or one dragged entirely outside the image, must be rejected before it reaches the encoder. Enforce a minimum size and clamp the rectangle to the image bounds.
- Decoding a very large photo into a canvas will exhaust memory on a phone and fail without a useful message. Downscale to a working size for editing and apply the transform to the original server-side.
- Rotating by an arbitrary angle leaves empty corners. Either crop inward automatically to fill the frame or fill the corners with a stated colour, but never save transparent wedges by accident.
- The tool needs undo and a reset to the original framing, since cropping is a sequence of adjustments rather than one decision.
- Leaving the editor with an unapplied crop must warn rather than silently discarding the work or silently saving it.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Cropping happens inside the existing upload flow, not in a parallel editor.
- [ ] Orientation metadata is applied before the image is shown and preserved through save.
- [ ] Fixed aspect ratios constrain the crop rectangle during the drag.
- [ ] The original file is retained and the crop can be reopened and adjusted.
- [ ] Zero-size and out-of-bounds crops are rejected before encoding.
- [ ] Large images are edited at a reduced preview size without memory failures.
- [ ] Undo, reset, and an unsaved-change warning are all present.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
