ImageCropModal.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react';
  2. import canvasToBlob from 'async-canvas-to-blob';
  3. import PropTypes from 'prop-types';
  4. import { withTranslation } from 'react-i18next';
  5. import ReactCrop from 'react-image-crop';
  6. import {
  7. Modal,
  8. ModalHeader,
  9. ModalBody,
  10. ModalFooter,
  11. } from 'reactstrap';
  12. import 'react-image-crop/dist/ReactCrop.css';
  13. import { toastError } from '~/client/util/apiNotification';
  14. import loggerFactory from '~/utils/logger';
  15. const logger = loggerFactory('growi:ImageCropModal');
  16. class ImageCropModal extends React.Component {
  17. // demo: https://codesandbox.io/s/72py4jlll6
  18. constructor(props) {
  19. super();
  20. this.state = {
  21. crop: null,
  22. imageRef: null,
  23. };
  24. this.onImageLoaded = this.onImageLoaded.bind(this);
  25. this.onCropChange = this.onCropChange.bind(this);
  26. this.getCroppedImg = this.getCroppedImg.bind(this);
  27. this.crop = this.crop.bind(this);
  28. this.reset = this.reset.bind(this);
  29. this.imageRef = null;
  30. }
  31. onImageLoaded(image) {
  32. this.setState({ imageRef: image }, () => this.reset());
  33. return false; // Return false when setting crop state in here.
  34. }
  35. onCropChange(crop) {
  36. this.setState({ crop });
  37. }
  38. async getCroppedImg(image, crop, fileName) {
  39. const canvas = document.createElement('canvas');
  40. const scaleX = image.naturalWidth / image.width;
  41. const scaleY = image.naturalHeight / image.height;
  42. canvas.width = crop.width;
  43. canvas.height = crop.height;
  44. const ctx = canvas.getContext('2d');
  45. ctx.drawImage(image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width, crop.height);
  46. try {
  47. const blob = await canvasToBlob(canvas);
  48. return blob;
  49. }
  50. catch (err) {
  51. logger.error(err);
  52. toastError(new Error('Failed to draw image'));
  53. }
  54. }
  55. async crop() {
  56. // crop immages
  57. if (this.state.imageRef && this.state.crop.width && this.state.crop.height) {
  58. const croppedImage = await this.getCroppedImg(this.state.imageRef, this.state.crop, '/images/icons/user');
  59. this.props.onCropCompleted(croppedImage);
  60. }
  61. }
  62. reset() {
  63. const size = Math.min(this.state.imageRef.width, this.state.imageRef.height);
  64. this.setState({
  65. crop: {
  66. aspect: 1,
  67. unit: 'px',
  68. x: this.state.imageRef.width / 2 - size / 2,
  69. y: this.state.imageRef.height / 2 - size / 2,
  70. width: size,
  71. height: size,
  72. },
  73. });
  74. }
  75. render() {
  76. return (
  77. <Modal isOpen={this.props.show} toggle={this.props.onModalClose}>
  78. <ModalHeader tag="h4" toggle={this.props.onModalClose} className="bg-info text-light">
  79. Image Crop
  80. </ModalHeader>
  81. <ModalBody className="my-4">
  82. <ReactCrop circularCrop src={this.props.src} crop={this.state.crop} onImageLoaded={this.onImageLoaded} onChange={this.onCropChange} />
  83. </ModalBody>
  84. <ModalFooter>
  85. <button type="button" className="btn btn-outline-danger rounded-pill mr-auto" onClick={this.reset}>
  86. Reset
  87. </button>
  88. <button type="button" className="btn btn-outline-secondary rounded-pill mr-2" onClick={this.props.onModalClose}>
  89. Cancel
  90. </button>
  91. <button type="button" className="btn btn-outline-primary rounded-pill" onClick={this.crop}>
  92. Crop
  93. </button>
  94. </ModalFooter>
  95. </Modal>
  96. );
  97. }
  98. }
  99. /**
  100. * Wrapper component for using unstated
  101. */
  102. ImageCropModal.propTypes = {
  103. show: PropTypes.bool.isRequired,
  104. src: PropTypes.string,
  105. onModalClose: PropTypes.func.isRequired,
  106. onCropCompleted: PropTypes.func.isRequired,
  107. };
  108. export default withTranslation()(ImageCropModal);