"use client"; import { Dialog, DialogContent, DialogTitle } from "./dialog"; import { useState, useCallback } from "react"; import Cropper, { Area } from "react-easy-crop"; import { Button } from "./button"; import { cn } from "~/lib/utils"; import { Slider } from "./slider"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "./card"; import { Loader2 } from "lucide-react"; interface ImageCropModalProps { file: File; aspect?: number; onCrop: (blob: Blob) => void; onCancel: () => void; className?: string; cropBoxClassName?: string; overlayClassName?: string; } export function ImageCropModal({ file, aspect = 1, onCrop, onCancel, className, cropBoxClassName, overlayClassName, }: ImageCropModalProps) { const [crop, setCrop] = useState({ x: 0, y: 0 }); const [zoom, setZoom] = useState(1.5); const [croppedAreaPixels, setCroppedAreaPixels] = useState(null); const [imageUrl] = useState(() => URL.createObjectURL(file)); const [isCropping, setIsCropping] = useState(false); const onCropComplete = useCallback((croppedArea: Area, croppedAreaPixels: Area) => { setCroppedAreaPixels(croppedAreaPixels); }, []); const handleCrop = async () => { try { setIsCropping(true); if (!croppedAreaPixels) return; const canvas = document.createElement("canvas"); const image = new Image(); image.src = imageUrl; await new Promise((resolve) => { image.onload = resolve; }); const scaleX = image.naturalWidth / image.width; const scaleY = image.naturalHeight / image.height; // Set a fixed size for the output const outputSize = 400; canvas.width = outputSize; canvas.height = outputSize; const ctx = canvas.getContext("2d"); if (!ctx) return; ctx.imageSmoothingQuality = "high"; ctx.drawImage( image, croppedAreaPixels.x * scaleX, croppedAreaPixels.y * scaleY, croppedAreaPixels.width * scaleX, croppedAreaPixels.height * scaleY, 0, 0, outputSize, outputSize ); canvas.toBlob( (blob) => { if (blob) { onCrop(blob); } }, "image/jpeg", 0.95 ); } catch (error) { console.error("Error cropping image:", error); } finally { setIsCropping(false); } }; return ( onCancel()}>
Crop Profile Picture

Adjust the image and zoom to crop your profile picture

Zoom

setZoom(value)} className="py-2" aria-label="Zoom" />
); }