/
ImageCropper.js
61 lines (56 loc) · 1.63 KB
/
ImageCropper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React, { Component } from 'react'
import ReactCrop, { makeAspectCrop } from 'react-image-crop';
import { Button } from 'antd'
import 'react-image-crop/dist/ReactCrop.css';
export default class ImageCropper extends Component {
state = {
crop: {
x: 20,
y: 10,
width: 100,
aspect: 1
},
showCroppedImage: false,
croppedImage: null
}
onImageLoaded = (image) => {
const crop = makeAspectCrop({
x: 0,
y: 0,
aspect: 16 / 9,
width: 50,
}, image.width / image.height);
const pixelCrop = {
x: Math.round(image.naturalWidth * (crop.x / 100)),
y: Math.round(image.naturalHeight * (crop.y / 100)),
width: Math.round(image.naturalWidth * (crop.width / 100)),
height: Math.round(image.naturalHeight * (crop.height / 100)),
};
this.renderCropPreview(image, pixelCrop);
this.setState({ crop });
}
renderCropPreview = (image, pixelCrop) => {
console.log(image);
console.log(pixelCrop);
}
handleCrop = () => {
console.log('handle crop called')
}
handleChange = (crop) => {
this.setState({ crop })
}
render() {
return (
<div>
<ReactCrop
src={this.props.src || 'https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png'}
onChange={this.handleChange}
onImageLoaded={this.onImageLoaded}
crop={this.state.crop}
/>
{this.props.src && <Button type="primary" onClick={this.handleCrop}>Crop Here</Button>}
{this.state.showCroppedImage && <img src={this.state.croppedImage} style={{ height: 400, width: 400 }} />}
</div>
)
}
}