import React, {Component, Fragment} from 'react'; import lrz from 'lrz' import {Upload, message, Button, Icon, Modal} from 'antd';
  import Cropper from 'react-cropper'; import 'cropperjs/dist/cropper.css'; import "./index.less"
  class cropper extends Component {     state = {         srcCropper: '',          selectImgName: '',          selectImgSize: '',          selectImgSuffix: '',          editImageModalVisible: false,          imgData: [],         loading: false,         download: ""     };     downLoadImg = () => {                  this.setState({             download: this.refs.cropper.getCroppedCanvas({                 maxWidth: 4096,                 maxHeight: 4096             }).toDataURL()         });
 
      };
      saveImg = async () => {         const {loading} = this.state;         if (loading) return;         this.setState({             loading: true         });                           const {base64, base64Len} = await lrz(this.refs.cropper.getCroppedCanvas().toDataURL(), {quality: .6});         const {imgData} = this.state;         imgData.push(base64);         this.setState({             imgData,             loading: false         }, () => {             this.handleCancel()         });              };
      beforeUpload = (file) => {         const isLt10M = file.size / 1024 / 1024 >= 10;                  if (isLt10M) {              message.error({content: '文件大小不能超过10M'});             return;         }         let reader = new FileReader();         reader.readAsDataURL(file);                   reader.onload = (e) => {             this.setState({                 srcCropper: e.target.result,                 selectImgName: file.name,                 selectImgSize: (file.size / 1024 / 1024),                 selectImgSuffix: file.type.split("/")[1],                 editImageModalVisible: true,             });         };         reader.onerror = (e) => {             new Error('图片加载失败')         };         return false;     };     handleCancel = () => {         this.setState({             editImageModalVisible: false,         });     };
 
      render() {         const {ratio, title} = this.props;         const {srcCropper, editImageModalVisible, imgData, loading, download} = this.state;         const props = {             name: 'file',             accept: "image/*",             showUploadList: false,              beforeUpload: this.beforeUpload,              onCancel: this.handleCancel         };
          return (             <Fragment>                 <Upload {...props}>                     <Button htmlType={'button'} type="dashed">                         <Icon type="upload"/>{title}                     </Button>                 </Upload>
                  <Modal                     title="裁剪"                     visible={editImageModalVisible}                     loading={true}                     closable={false}                     footer={[                         <Button key={'取消'}                                 icon={'redo'}                                 htmlType={'button'}                                 onClick={this.handleCancel}>                             取消                         </Button>,                         <Button key={'保存'}                                 icon={'save'}                                 htmlType={'button'}                                 type="primary"                                 onClick={this.saveImg}                                 loading={loading}>                             保存                         </Button>,                         <Button key={'下载'}                                 htmlType={'button'}                                 icon="download"                                 type="primary"                                 onClick={this.downLoadImg}>
                              <a href={download}                                style={{color: '#fff', marginLeft: 5}}                                download="cropped.jpg">下载</a>                         </Button>                     ]}                 >
                      <Cropper                         key={'cropper'}                         ref='cropper'                         src={srcCropper}                         style={{height: 300, width: '100%'}}                         viewMode={1}                          zoomable={true}                          aspectRatio={ratio}                          guides={true}                          background={false}                          rotatable={false}                      />
                  </Modal>                 <div className={'imgBox'}>                     {                         imgData.map((item, index) => {                             return (<img                                     src={item}                                     alt={item}                                     key={index}/>                             )                         })                     }
                  </div>
              </Fragment>
          )     } }
  export default cropper; cropper.defaultProps = {     ratio: 1,     src: 'https://fengyuanchen.github.io/cropperjs/images/picture.jpg',     quality: 0.6,     title: '上传图片' };
  |