Crop and Zoom React Images Easily with react-easy-crop
React Easy Crop is a powerful and user-friendly React component that allows developers to implement image and video cropping functionality with ease. This library provides a seamless way to add drag, zoom, and rotate interactions to your React applications, making it an excellent choice for projects that require image manipulation features.
Features
React Easy Crop comes packed with a variety of features that make it stand out:
- Support for drag, zoom, and rotate interactions
- Provides crop dimensions in both pixels and percentages
- Compatible with various image formats (JPEG, PNG, GIF) as URL or base64 string
- Supports HTML5 video formats
- Mobile-friendly design
Installation
To get started with React Easy Crop, you can install it using npm or yarn:
npm install react-easy-crop
or
yarn add react-easy-crop
Basic Usage
Here’s a simple example of how to use React Easy Crop in your React application:
import { useState } from 'react'
import Cropper from 'react-easy-crop'
const ImageCropper = () => {
const [crop, setCrop] = useState({ x: 0, y: 0 })
const [zoom, setZoom] = useState(1)
const onCropComplete = (croppedArea, croppedAreaPixels) => {
console.log(croppedArea, croppedAreaPixels)
}
return (
<div style={{ position: 'relative', width: '100%', height: '300px' }}>
<Cropper
image="https://example.com/your-image.jpg"
crop={crop}
zoom={zoom}
aspect={4 / 3}
onCropChange={setCrop}
onZoomChange={setZoom}
onCropComplete={onCropComplete}
/>
</div>
)
}
In this example, we create a basic image cropper component. The Cropper is wrapped in a container with position: relative
to ensure it fills the container rather than the whole page.
Advanced Usage
React Easy Crop offers several advanced features and customization options:
Customizing Aspect Ratio
You can easily change the aspect ratio of the crop area:
<Cropper
image="https://example.com/your-image.jpg"
aspect={16 / 9} // Set to 16:9 aspect ratio
// ... other props
/>
Rotation
Enable rotation functionality by adding the rotation
prop:
const [rotation, setRotation] = useState(0)
<Cropper
image="https://example.com/your-image.jpg"
rotation={rotation}
onRotationChange={setRotation}
// ... other props
/>
Custom Crop Shape
You can create a circular crop area by setting the cropShape
prop:
<Cropper
image="https://example.com/your-image.jpg"
cropShape="round"
// ... other props
/>
Restricting Crop Area
Limit the crop area to a specific part of the image:
<Cropper
image="https://example.com/your-image.jpg"
restrictPosition={false}
// ... other props
/>
Handling the Cropped Image
To actually crop the image based on the user’s selection, you’ll need to implement a separate function. Here’s a basic example:
const createImage = (url) =>
new Promise((resolve, reject) => {
const image = new Image()
image.addEventListener('load', () => resolve(image))
image.addEventListener('error', (error) => reject(error))
image.src = url
})
const getCroppedImg = async (imageSrc, pixelCrop) => {
const image = await createImage(imageSrc)
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = pixelCrop.width
canvas.height = pixelCrop.height
ctx.drawImage(
image,
pixelCrop.x,
pixelCrop.y,
pixelCrop.width,
pixelCrop.height,
0,
0,
pixelCrop.width,
pixelCrop.height
)
return new Promise((resolve) => {
canvas.toBlob((blob) => {
resolve(URL.createObjectURL(blob))
}, 'image/jpeg')
})
}
You can then use this function in your component:
const onCropComplete = async (croppedArea, croppedAreaPixels) => {
const croppedImage = await getCroppedImg(imageUrl, croppedAreaPixels)
console.log('Cropped image:', croppedImage)
}
Conclusion
React Easy Crop provides a robust solution for implementing image cropping functionality in React applications. With its intuitive API and extensive customization options, it’s an excellent choice for developers looking to add image manipulation features to their projects. By following this guide, you should now have a solid understanding of how to integrate and use React Easy Crop in your applications, enabling you to create powerful and user-friendly image editing interfaces.