# Image crop

> Frame a photo to any aspect and straighten it with a dial: the photo zooms itself just enough that no corner ever shows empty.

The crop step for covers, posts, banners and product shots. A fixed window sits over the photo and the photo moves under it: drag to place it, wheel or pinch to zoom, pick an aspect and the window reshapes while the picture keeps covering it. Straighten with the dial below and the photo turns under the window, zooming itself just enough at every angle that no empty corner ever shows, the way a phone’s photo editor does. Quarter turns and Reset sit beside the aspects. Every settled change reports the crop in the image’s own pixels, rotation included, and cropImage turns it into a Blob for your upload.

## Install

```bash
npx shadcn@latest add @hairline/image-crop
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/image-crop
- Registry item (source): https://hairlineui.com/r/image-crop.json

## Usage

```tsx
import { ImageCrop, cropImage, type CropArea } from "@/components/image-crop";

const [area, setArea] = React.useState<CropArea | null>(null);

<ImageCrop src={photo.url} alt={photo.alt} onChange={setArea} />
<button onClick={async () => upload(await cropImage(photo.url, area!))}>Save</button>
```

## Cropping a cover before upload

Keep the latest crop, then turn it into a WebP no wider than 2400px and upload it with your own form.

```tsx
"use client";
import * as React from "react";
import { ImageCrop, cropImage, type CropArea } from "@/components/image-crop";

export function CoverEditor({ file, onSaved }: { file: File; onSaved: (url: string) => void }) {
  const src = React.useMemo(() => URL.createObjectURL(file), [file]);
  const [area, setArea] = React.useState<CropArea | null>(null);
  return (
    <div className="grid w-[28rem] gap-3">
      <ImageCrop src={src} alt="Your cover photo" aspects={[{ label: "Cover", value: 3 }, { label: "Square", value: 1 }]} onChange={setArea} />
      <button
        disabled={!area}
        onClick={async () => {
          const blob = await cropImage(src, area!, { type: "image/webp", maxSize: 2400 });
          const body = new FormData();
          body.append("cover", blob, "cover.webp");
          const res = await fetch("/api/cover", { method: "POST", body });
          onSaved((await res.json()).url);
        }}
      >
        Save cover
      </button>
    </div>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `src` | `string` | – | The photo to crop. A new src starts fresh. |
| `alt` | `string` | – | The photo’s text alternative; it is part of the frame’s label. |
| `aspects` | `{ label: string; value: number \| null }[]` | `Original, 1:1, 4:5, 16:9` | The aspects to offer, width over height; null is the photo’s own shape. |
| `defaultAspect` | `number` | `0` | Index into aspects to start with. |
| `onChange` | `(area: CropArea) => void` | – | The crop each time a change settles: x, y, width, height, rotation, centerX, centerY, in the image’s own pixels. |
| `cropImage(src, area, options?)` | `Promise<Blob>` | – | Draws the crop into a Blob. options: type (default image/jpeg), quality (0.92), maxSize (cap on the longer side). |
| `maxZoom` | `number` | `5` | Most it zooms, as a multiple of just covering the window. |
| `crossOrigin` | `"anonymous" \| "use-credentials"` | – | Set it for photos from another origin, so cropImage can read them (the host must send CORS headers). |
| `labels` | `{ straighten?, rotateLeft?, rotateRight?, reset?, frame? }` | – | Words for the controls and the frame’s label. |

## States

- **framing**: The window fills the stage at the chosen aspect with 24px around it; outside it the photo is dimmed to 38%. Rule-of-thirds lines fade in while you drag, zoom or straighten.
- **panning and zooming**: Drag moves the photo with momentum and gives past its edges; wheel and pinch zoom around the pointer, from just covering up to 5×. Arrow keys move it and + and − zoom when the frame has focus.
- **straightening**: Six pixels of dial per degree, −45° to +45°, snapping to level within 0.6°. The photo turns under the window and zooms to the smallest scale that still covers it.
- **changing aspect**: The window glides to its new size in 360ms and the photo springs to cover it.
- **reduced motion**: Nothing moves on its own; the photo follows your hand and settles in place.

## What it handles

Most croppers let the image slide off and show empty corners, rotate in coarse steps, and hand back numbers relative to the screen rather than the image.

- **Never an empty corner**: At every zoom, pan and angle the photo covers the window; the bounds are worked out in the photo’s own turned frame.
- **Straighten with a dial**: A ruler of degrees from −45 to +45 with a soft detent at level, by drag or arrow keys, the photo zooming to stay covered as it turns.
- **Aspects that reshape**: Original, 1:1, 4:5 and 16:9 by default (pass your own); the window glides to the new shape and the photo follows.
- **Exact pixels back**: onChange gets x, y, width and height in the image’s own pixels plus rotation and centre, so the server can crop the original.
- **A Blob when you want one**: cropImage(src, area) draws the crop at full resolution (or capped by maxSize) into a JPEG, PNG or WebP Blob.
- **Gestures that hand over**: Pan with momentum, rubber-band past the edges, pinch about the fingers; springs carry their speed, so grabbing mid-glide never jumps.

## Replaces

react-easy-crop, react-image-crop, cropperjs, react-cropper, image cropper modal

## Where it fits

- **Cover and banner uploads**: Frame a profile cover, a channel banner or an event header at its exact aspect.
- **Social posts**: Crop to 1:1, 4:5 or 16:9 before posting.
- **AI image results**: Crop and level the take you kept before exporting it.
- **Product photos**: Square and straighten a shot for a catalogue.

## Notes

- The frame is a focusable group whose label explains the keys; the dial is a slider (−45 to 45) and the aspects a radiogroup.
- With rotation, the crop is defined by its centre and size in the original photo, turned by rotation degrees clockwise; cropImage draws exactly that, and x and y are its top-left for servers that only crop straight.
- Only transforms change while you drag, zoom or turn; the window’s size animates once per aspect change.
- Under reduced motion nothing moves on its own; the photo settles in place.

License: MIT (https://hairlineui.com/license#components)
