ProfileImageUploader.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import ReactCrop from 'react-image-crop';
  5. import AppContainer from '../services/AppContainer';
  6. import { createSubscribedElement } from './UnstatedUtils';
  7. class ProfileImageUploader extends React.Component {
  8. state = {
  9. src: null,
  10. crop: {
  11. unit: '%',
  12. width: 30,
  13. aspect: 16 / 9,
  14. },
  15. };
  16. onSelectFile = (e) => {
  17. if (e.target.files && e.target.files.length > 0) {
  18. const reader = new FileReader();
  19. reader.addEventListener('load', () => this.setState({ src: reader.result }));
  20. reader.readAsDataURL(e.target.files[0]);
  21. }
  22. };
  23. // If you setState the crop in here you should return false.
  24. onImageLoaded = (image) => {
  25. this.imageRef = image;
  26. };
  27. onCropComplete = (crop) => {
  28. this.makeClientCrop(crop);
  29. };
  30. onCropChange = (crop, percentCrop) => {
  31. // You could also use percentCrop:
  32. // this.setState({ crop: percentCrop });
  33. this.setState({ crop });
  34. };
  35. async makeClientCrop(crop) {
  36. if (this.imageRef && crop.width && crop.height) {
  37. const croppedImageUrl = await this.getCroppedImg(this.imageRef, crop, 'newFile.jpeg');
  38. this.setState({ croppedImageUrl });
  39. }
  40. }
  41. getCroppedImg(image, crop, fileName) {
  42. const canvas = document.createElement('canvas');
  43. const scaleX = image.naturalWidth / image.width;
  44. const scaleY = image.naturalHeight / image.height;
  45. canvas.width = crop.width;
  46. canvas.height = crop.height;
  47. const ctx = canvas.getContext('2d');
  48. ctx.drawImage(image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width, crop.height);
  49. return new Promise((resolve, reject) => {
  50. canvas.toBlob((blob) => {
  51. if (!blob) {
  52. // reject(new Error('Canvas is empty'));
  53. console.error('Canvas is empty');
  54. return;
  55. }
  56. blob.name = fileName;
  57. window.URL.revokeObjectURL(this.fileUrl);
  58. this.fileUrl = window.URL.createObjectURL(blob);
  59. resolve(this.fileUrl);
  60. }, 'image/jpeg');
  61. });
  62. }
  63. render() {
  64. const { crop, croppedImageUrl, src } = this.state;
  65. return (
  66. <div className="App">
  67. <div>
  68. <input type="file" onChange={this.onSelectFile} name="profileImage" accept="image/*" />
  69. </div>
  70. {src && <ReactCrop src={src} crop={crop} onImageLoaded={this.onImageLoaded} onComplete={this.onCropComplete} onChange={this.onCropChange} />}
  71. {croppedImageUrl && <img alt="Crop" style={{ maxWidth: '100%' }} src={croppedImageUrl} />}
  72. </div>
  73. );
  74. }
  75. }
  76. /**
  77. * Wrapper component for using unstated
  78. */
  79. const ProfileImageFormWrapper = (props) => {
  80. return createSubscribedElement(ProfileImageUploader, props, [AppContainer]);
  81. };
  82. ProfileImageUploader.propTypes = {
  83. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  84. };
  85. export default withTranslation()(ProfileImageFormWrapper);