An image cropping tool for React with no dependencies.
- Touch enabled
- Free-form or fixed aspect crops
- Keyboard support for nudging selection
- Min/max crop size
- No dependencies/small footprint (5KB gzip)
npm i react-image-crop --save
Include the main js module:
import ReactCrop from 'react-image-crop';
Include either dist/ReactCrop.css
or ReactCrop.scss
.
import 'react-image-crop/dist/ReactCrop.css';
// or scss:
import 'react-image-crop/lib/ReactCrop.scss';
If you prefer to include ReactCrop globally by marking react-image-crop
as external in your application, then include react-image-crop
from one of the following CDNs:
-
cdnjs (Coming soon)
<script src="https://unpkg.com/react-image-crop/dist/ReactCrop.min.js"></script>
Note when importing the script globally using a <script>
tag access the component with ReactCrop.Component
.
<ReactCrop src="path/to/image.jpg" />
You can of course pass a blob url (using URL.createObjectURL()
and URL.revokeObjectURL()
) or base64 data.
A callback which happens for every change of the crop (i.e. many times as you are dragging/resizing). Passes the current crop state object.
Note you must implement this callback and update your crop state, otherwise nothing will change!
onChange = (crop) => {
this.setState({ crop });
}
All crop values are in pixels. All crop params are optional.
* While you can initially omit the crop object, any subsequent change will need to be saved to state in the onChange
callback and passed here.
crop: {
x: 130,
y: 50,
width: 200,
height: 200
}
<ReactCrop src="path/to/image.jpg" crop={this.state.crop} />
If you want a fixed aspect you can either omit width
and height
:
crop: {
aspect: 16/9
}
Or specify one or both of the dimensions:
crop: {
aspect: 16/9,
width: 50,
}
If you specify just one of the dimensions, the other will be calculated for you.
A minimum crop width, in pixels.
A minimum crop height, in pixels.
A maximum crop width, in pixels.
A maximum crop height, in pixels.
If true is passed then selection can't be disabled if the user clicks outside the selection area.
If true then the user cannot resize or draw a new crop. A class of ReactCrop--disabled
is also added to the container for user styling.
If true then the user cannot create or resize a crop, but can still drag the existing crop around. A class of ReactCrop--locked
is also added to the container for user styling.
A string of classes to add to the main ReactCrop
element.
Inline styles object to be passed to the image wrapper element.
Inline styles object to be passed to the image element.
A callback which happens after a resize, drag, or nudge. Passes the current crop state object.
A callback which happens when the image is loaded. Passes the image DOM element.
This event is called if the image had an error loading.
A callback which happens when a user starts dragging or resizing. It is convenient to manipulate elements outside this component.
A callback which happens when a user releases the cursor or touch after dragging or resizing.
Allows setting the crossorigin attribute on the image.
Render a custom element in crop selection.
I wanted to keep this component focused so I didn't provide this. Normally a cropped image will be rendered and cached by a backend.
However here's a ready to use function that returns a file blob for the cropped part after providing some parameters you already have when using this package:
/**
* @param {File} image - Image File Object
* @param {Object} crop - crop Object
* @param {String} fileName - Name of the returned file in Promise
*/
function getCroppedImg(image, crop, fileName) {
const canvas = document.createElement('canvas');
const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height;
canvas.width = crop.width;
canvas.height = crop.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(
image,
crop.x * scaleX,
crop.y * scaleY,
crop.width * scaleX,
crop.height * scaleY,
0,
0,
crop.width,
crop.height,
);
// As Base64 string
// const base64Image = canvas.toDataURL('image/jpeg');
// As a blob
return new Promise((resolve, reject) => {
canvas.toBlob(blob => {
blob.name = fileName;
resolve(blob);
}, 'image/jpeg');
});
}
async test() {
const croppedImg = await getCroppedImg(image, crop, fileName);
}
Some things to note:
-
toDataURL is synchronous and will block the main thread, for large images this could be for as long as a couple of seconds. Always use
toDataURL('image/jpeg')
otherwise it will default toimage/png
and the conversion will be significantly slower. -
toBlob is both faster and asynchronous, but not supported on old browsers (this is quickly becoming irrelevant).
-
Another option to make the conversion faster is to scale the image down before converting it.
You might find that some images are rotated incorrectly. Unfortunately this is a browser wide issue not related to this library. You need to fix your image before passing it in.
You can use the following library to load images, which will correct the rotation for you: https://github.com/blueimp/JavaScript-Load-Image/
To develop run npm start
, this will recompile your JS and SCSS on changes.
You can test your changes by opening demo/index.html
in a browser (you don't need to be running a server).