A light library without any dependencies cropping tool for React. Both working for web and mobile.
npm i react-avatar-image-cropper --save
Include the main js module, e.g:
import AvatarImageCropper from 'react-avatar-image-cropper';
also you can implement the library as external js, add the script in build directory like below:
<script src="/dist/index.js"></script>
you will get the AvatarImageCropper in window object.
Set the container for cropper and pass apply function for handling cropped file, e.g:
apply = (file) => {
// handle the blob file you want
// such as get the image src
var src = window.URL.createObjectURL(file);
}
render() {
return (
<div style={{ width: '250px', height: '250px', margin: 'auto', border: '1px solid black' }}>
<AvatarImageCr apply={this.apply} />
</div>
);
}
Most of the props is used for customizing the display:
The apply function will get the cropped blob file, you can handle it whatever you want.
The cancel function fired when you cancel cropping.
When the file is not image of png/jpg/jpeg or the file size exceed maxsize(default is 2M), the error message will displayed inside container as default. You can handle the error with errorHandler, then the default behavoir will not be fired again. With errorHandler, when file is not image return "not_image", when exceeded size return "maxsize".
errorHandler = (type) => {
console.log(type);
}
<AvatarImageCr apply={this.apply} errorHandler={this.errorHandler} />
when there is a background image in container, pass true to change the avatar background and text color
replace the initial text.
replace the cancel/apply buttons with customized button. The first one must be as cancel, and the second one must be as apply
const actions = [
<button key={0}>test_cancel</button>,
<button key={1}>test_apply</button>,
]
<AvatarImageCr apply={this.apply} actions={actions} />
replace the initial display of icon.
const icon = (
<svg viewBox="0 0 24 24" style={this.iconStyle}>
<circle cx="12" cy="12" r="3.2"></circle>
<path
d="M9 2L7.17 4H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2h-3.17L15 2H9zm3 15c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5z"></path>
</svg>
)
<AvatarImageCr apply={this.apply} icon={icon}/>
replace the initial display of loading component.
set the maxsize of uploaded image, default value is 2M
const maxsize= 1024 * 1024 *5
<AvatarImageCr apply={this.apply} maxsize={maxsize}/>
set the class for the root element
set the initial icon style
set the initial text style
set the error text style
set the root element style
set the container of slider style
set the container div of slider style
set the cancel button style(if the there is actions prop, then the style is no working)
set the apply button style(if the there is actions prop, then the style is no working)
To view the demo or develop,open the demo directory and under it,
npm install
npm start
you can open localhost:8080 in a browser to try.
MIT