import React, { useState, useRef } from 'react'; import ReactCrop, { type Crop } from 'react-image-crop'; import 'react-image-crop/dist/ReactCrop.css'; interface ImageCropperProps { imageUrl: string; aspect: number; onCropComplete: (croppedImageUrl: string) => void; onCancel: () => void; isCircular?: boolean; // Add this prop } export default function ImageCropper({ imageUrl, aspect, onCropComplete, onCancel, isCircular = false // Default to false }: ImageCropperProps) { const [crop, setCrop] = useState({ unit: 'px', width: 300, height: 300, x: 0, y: 0, }); const [completedCrop, setCompletedCrop] = useState(null); const imgRef = useRef(null); const [dimensions, setDimensions] = useState({ width: 0, height: 0 }); const containerRef = useRef(null); const getCroppedImg = async (image: HTMLImageElement, crop: Crop): Promise => { const canvas = document.createElement('canvas'); const scaleX = image.naturalWidth / image.width; const scaleY = image.naturalHeight / image.height; canvas.width = crop.width || 0; canvas.height = crop.height || 0; const ctx = canvas.getContext('2d'); if (!ctx) { throw new Error('No 2d context'); } ctx.drawImage( image, (crop.x || 0) * scaleX, (crop.y || 0) * scaleY, (crop.width || 0) * scaleX, (crop.height || 0) * scaleY, 0, 0, crop.width || 0, crop.height || 0 ); return canvas.toDataURL('image/jpeg'); }; const handleCropComplete = async (crop: Crop) => { if (!crop || !crop.width || !crop.height) return; try { const croppedImageUrl = await getCroppedImg(imgRef.current!, crop); onCropComplete(croppedImageUrl); } catch (e) { console.error('Error creating crop:', e); } }; const calculateOptimalDimensions = (imgWidth: number, imgHeight: number) => { if (!containerRef.current) return { width: imgWidth, height: imgHeight }; const containerWidth = containerRef.current.clientWidth - 48; // account for padding const containerHeight = window.innerHeight * 0.7; // 70vh const minDesiredWidth = 600; // minimum width we want for small images let scale = 1; // If image is smaller than minimum desired width, scale it up if (imgWidth < minDesiredWidth) { scale = minDesiredWidth / imgWidth; } // Calculate scaled dimensions let newWidth = imgWidth * scale; let newHeight = imgHeight * scale; // If scaled dimensions are too large, scale them down to fit container const widthRatio = containerWidth / newWidth; const heightRatio = containerHeight / newHeight; const finalScale = Math.min(widthRatio, heightRatio, 1); return { width: Math.floor(newWidth * finalScale), height: Math.floor(newHeight * finalScale) }; }; const handleImageLoad = (e: React.SyntheticEvent) => { const img = e.currentTarget; const { width, height } = calculateOptimalDimensions(img.naturalWidth, img.naturalHeight); setDimensions({ width, height }); // Set initial crop const minSize = Math.min(width, height); setCrop({ unit: 'px', width: aspect ? minSize : width * 0.8, height: aspect ? minSize / aspect : height * 0.8, x: (width - (aspect ? minSize : width * 0.8)) / 2, y: (height - (aspect ? minSize / aspect : height * 0.8)) / 2 }); }; const cropStyle = isCircular ? { className: 'rounded-full', style: { aspectRatio: '1', backgroundColor: 'rgba(0, 0, 0, 0.5)', boxShadow: '0 0 0 9999px rgba(0, 0, 0, 0.5)', } } : {}; return (

Adjust Image

setCrop(c)} aspect={aspect} className="max-w-full max-h-full select-none" circularCrop={isCircular} ruleOfThirds {...cropStyle} > Crop preview
); }