Files
simple-react-app-kit/packages/cropper/docs/zh-Hans/examples/avatar-crop.mdx
T

38 lines
822 B
Plaintext
Raw Normal View History

2026-09-29 17:09:39 +08:00
---
title: 头像裁剪
description: 创建圆形头像预览并导出固定尺寸的 PNG。
order: 30
toc:
- id: example
title: 示例
---
## 示例 {#example}
```tsx
2026-09-29 17:46:16 +08:00
import { Cropper, type CropperHandle } from "@workspace/cropper"
2026-09-29 17:09:39 +08:00
function AvatarCropper({ src }: { src: string }) {
2026-09-29 17:46:16 +08:00
const cropperRef = React.useRef<CropperHandle>(null)
2026-09-29 17:09:39 +08:00
async function saveAvatar() {
2026-09-29 17:46:16 +08:00
const blob = await cropperRef.current?.getCropImage({
2026-09-29 17:09:39 +08:00
height: 512,
shape: "circle",
type: "image/png",
width: 512,
})
2026-09-29 17:46:16 +08:00
if (blob instanceof Blob) await uploadAvatar(blob)
2026-09-29 17:09:39 +08:00
}
return (
<div className="space-y-4">
2026-09-29 17:46:16 +08:00
<Cropper ref={cropperRef} src={src} shape="circle" className="size-72" />
<button type="button" onClick={saveAvatar}>
2026-09-29 17:09:39 +08:00
保存头像
</button>
</div>
)
}
```