Overview
TheFotoPerfil component provides a complete photo upload and cropping solution for user profile images. It integrates the react-cropper library with cropperjs to enable precise image cropping with live preview, file size validation, and localStorage persistence.
Location: src/components/FotoPerfil.jsx
Dependencies
Required Packages
react-cropper- React wrapper for Cropper.jscropperjs- Core cropping librarynext/image- Next.js image optimizationreact-icons/go- Trash icon for delete functionality
CSS Import
Component State
src/components/FotoPerfil.jsx:12-26
LocalStorage Integration
Loading Saved Photo
On component mount, load photo and circular state from localStorage:src/components/FotoPerfil.jsx:28-42
Saving Photo
Cropped image is saved as base64 data URL:src/components/FotoPerfil.jsx:78-81
Storage Keys
fotoPerfil- Base64 encoded cropped imagefotoRedonda- Boolean string (“true”/“false”) for circular display
File Upload
File Input Ref
src/components/FotoPerfil.jsx:23,97-100
File Change Handler
src/components/FotoPerfil.jsx:51-71
Cropper Configuration
Cropper Component
src/components/FotoPerfil.jsx:192-211
Key Settings
| Property | Value | Description |
|---|---|---|
initialAspectRatio | 1 | Square crop box |
viewMode | 1 | Restrict crop box to canvas |
dragMode | "move" | Move image, not crop box |
cropBoxMovable | false | Fixed crop box position |
cropBoxResizable | false | Fixed crop box size |
autoCropArea | 1 | Crop box fills entire canvas |
background | false | No background grid |
responsive | true | Rebuild on window resize |
guides | true | Show crop box guides |
Preview Integration
The preview is linked via className:src/components/FotoPerfil.jsx:200,240
Cropping & Saving
Get Cropped Data
src/components/FotoPerfil.jsx:73-95
Circular Image Toggle
Toggle Handler
src/components/FotoPerfil.jsx:45-49
Toggle UI
src/components/FotoPerfil.jsx:157-180
Image Display
Profile Image Rendering
- If no photo saved (
cropData === "#"): Show default user icon - If photo saved: Show cropped image
- Apply
rounded-[50%]class if circular toggle enabled
src/components/FotoPerfil.jsx:118-136
Delete Functionality
Delete Handler
src/components/FotoPerfil.jsx:107-112
Delete Confirmation Modal
src/components/FotoPerfil.jsx:274-297
Modal Structure
Cropper Modal Layout
The cropper modal is a full-screen overlay on mobile, card-based on desktop:src/components/FotoPerfil.jsx:185-271
Error Handling
File Size Validation
Only files ≤ 1MB enable the accept button:Storage Error
If localStorage quota exceeded during save:Error Message Display
src/components/FotoPerfil.jsx:246-252
Usage Example
In Sidebar Component
src/components/Sidebar.jsx:5,27
Integration Checklist
-
Install dependencies:
-
Import CSS in component:
-
Provide default user image in
/public/user.svg - Ensure localStorage is available (client-side only)
- Handle responsive layouts (mobile vs desktop modals)
Best Practices
- Validate file size before processing (1MB limit)
- Use base64 data URLs for localStorage (no external file storage)
- Provide live preview for user feedback
- Implement error handling for storage quota
- Use refs for hidden file inputs
- Lock crop box aspect ratio for consistent profile images
- Provide delete functionality with confirmation
- Support circular image display toggle
- Use fixed crop box to simplify UX
- Show filename after selection for confirmation
