ImageCrop 图片裁剪

裁剪图片

何时使用

裁剪图片

代码演示

基本

最简单的用法。

ImageCrop图片裁剪 - 图1

  1. import ReactDOM from 'react-dom';
  2. import { ImageCrop, Button } from 'choerodon-ui';
  3. import React, { useState, useCallback } from 'react';
  4. const Demo = () => {
  5. const [visable, setVisiable] = useState(false);
  6. const hanleClick = useCallback(() => {
  7. setVisiable(true);
  8. }, []);
  9. const hanleOk = useCallback(({ url, blob, area }) => {
  10. console.log(url, blob, area);
  11. setVisiable(false);
  12. }, []);
  13. const hanleCancel = useCallback(() => {
  14. setVisiable(false);
  15. console.log('close');
  16. }, []);
  17. return (
  18. <>
  19. <Button onClick={hanleClick} funcType="raised">
  20. 查看
  21. </Button>
  22. <ImageCrop
  23. modalVisible={visable}

图片裁剪下载

图片裁剪下载。

ImageCrop图片裁剪 - 图2

  1. import ReactDOM from 'react-dom';
  2. import { ImageCrop, Button } from 'choerodon-ui';
  3. import React, { useState, useCallback } from 'react';
  4. const Demo = () => {
  5. const [visable, setVisiable] = useState(false);
  6. const hanleClick = useCallback(() => {
  7. setVisiable(true);
  8. }, []);
  9. const hanleOk = useCallback(({ url, blob, area }) => {
  10. console.log(url, blob, area);
  11. setVisiable(false);
  12. // 这里是获取到的图片base64编码,这里只是个例子哈,要自行编码图片替换这里才能测试看到效果
  13. const imgUrl = url;
  14. // 如果浏览器支持msSaveOrOpenBlob方法(也就是使用IE浏览器的时候),那么调用该方法去下载图片
  15. if (window.navigator.msSaveOrOpenBlob) {
  16. const bstr = atob(imgUrl.split(',')[1]);
  17. let n = bstr.length;
  18. const u8arr = new Uint8Array(n);
  19. // eslint-disable-next-line no-plusplus
  20. while (n--) {
  21. u8arr[n] = bstr.charCodeAt(n);
  22. }
  23. // eslint-disable-next-line no-shadow
  24. const blob = new Blob([u8arr]);
  25. window.navigator.msSaveOrOpenBlob(blob, 'chart-download.png');

头像展示

头像编辑。

ImageCrop图片裁剪 - 图3

  1. import ReactDOM from 'react-dom';
  2. import { ImageCrop, Avatar } from 'choerodon-ui';
  3. import React, { useState, useCallback } from 'react';
  4. const { AvatarUploader } = ImageCrop;
  5. const Demo = () => {
  6. const [visable, setVisiable] = useState(false);
  7. const hanleClick = useCallback(() => {
  8. setVisiable(true);
  9. }, []);
  10. const hanleOk = useCallback(() => {
  11. setVisiable(false);
  12. }, []);
  13. const hanleCancel = useCallback(() => {
  14. setVisiable(false);
  15. console.log('close');
  16. }, []);
  17. return (
  18. <>
  19. <Avatar onClick={hanleClick} style={{ backgroundColor: '#87d068' }}>
  20. 我绿了
  21. </Avatar>
  22. <AvatarUploader

图片裁剪并上传

图片裁剪并上传,不可以使用upload上传类型和上传前的钩子其他upload的配置可以正常使用。

ImageCrop图片裁剪 - 图4

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Icon, Modal, ImageCrop, Upload } from 'choerodon-ui';
  4. class PicturesWall extends React.Component {
  5. state = {
  6. previewVisible: false,
  7. previewImage: '',
  8. fileList: [
  9. {
  10. uid: -1,
  11. name: 'xxx.png',
  12. status: 'done',
  13. url:
  14. 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
  15. },
  16. ],
  17. };
  18. handleCancel = () => this.setState({ previewVisible: false });
  19. handlePreview = (file) => {
  20. this.setState({
  21. previewImage: file.url || file.thumbUrl,
  22. previewVisible: true,
  23. });
  24. };

图片服务端裁剪

图片服务端裁剪,把完整的裁剪信息和原图传输到服务器。

ImageCrop图片裁剪 - 图5

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import { Icon, Modal, ImageCrop, Upload } from 'choerodon-ui';
  4. class PicturesWall extends React.Component {
  5. state = {
  6. previewVisible: false,
  7. previewImage: '',
  8. fileList: [
  9. {
  10. uid: -1,
  11. name: 'xxx.png',
  12. status: 'done',
  13. url:
  14. 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
  15. },
  16. ],
  17. };
  18. handleCancel = () => this.setState({ previewVisible: false });
  19. handlePreview = (file) => {
  20. this.setState({
  21. previewImage: file.url || file.thumbUrl,
  22. previewVisible: true,
  23. });
  24. };

裁剪区域自定义渲染

支持在裁剪区域设置想要的渲染样式。

ImageCrop图片裁剪 - 图6

  1. import ReactDOM from 'react-dom';
  2. import { ImageCrop, Button, Row, Col, Avatar } from 'choerodon-ui';
  3. import React, { useState, useCallback } from 'react';
  4. const avatarRender = (src) => {
  5. const prefix = `c7n-image-crop`;
  6. const avatarList = [
  7. {
  8. src,
  9. size: 80,
  10. icon: 'person',
  11. text: '80×80',
  12. },
  13. {
  14. src,
  15. size: 40,
  16. icon: 'person',
  17. text: '40×40',
  18. },
  19. {
  20. src,
  21. size: 20,
  22. icon: 'person',
  23. text: '20×20',
  24. },
  25. ];
  26. return src
  27. ? avatarList.map(
  28. // eslint-disable-next-line react/jsx-key
  29. (itemProps) => (
  30. <Row
  31. key={itemProps.text}
  32. className={`${prefix}-avatar-row`}

API

参数说明类型默认值
aspect裁切区域宽高比,width / heightnumber1 / 1
shape裁切区域形状,‘rect’‘round’string‘rect’
grid显示裁切区域网格(九宫格)booleanfalse
zoom启用图片缩放booleantrue
rotate启用图片旋转booleanfalse
beforeCrop弹窗打开前调用,若返回 false,弹框将不会打开function-
modalTitle弹窗标题string‘编辑图片’
modalWidth弹窗宽度,像素值或百分比number | string520
modalOk弹窗确定按钮文字string‘确定’
modalCancel弹窗取消按钮文字string‘取消’
onCancel取消模态框触发onCancel?: () => void-
onOk点击模态框确定触发{ url: string, blob: Blob,area:Area }=> void-
modalVisible控制模态框的展示booleanture
serverCrop服务端裁剪booleanfalse
onCropComplete裁剪完毕触发方法({ url: string, blob: Blob, area: Area }) => void-
cropContent修改裁剪编辑位置的渲染(crop: ReactElement<EasyCropProps>) => React.ReactElement<any>-
modalPropsmodal 的属性配置不可以更改onOk,onCancelModalProps-

ImgCrop.AvatarUploader

这个组件可以完成对于头像上传的裁剪的场景需求,主要是增加预览效果集成

参数说明类型默认值
visible上传图片模态框的显示状态booleanfalse
onClose模态框关闭时的回调(visible: boolean) => void;
onUploadOk成功上传时的回调(res: any) => void;
uploadUrl上传链接string
uploadFaild上传失败() => void;
uploadError上传服务器错误(error: any) => void;
handleUpload点击上传(area: AvatarArea) => void;
cropComplete裁剪完成(imageState: any) => void;
title上传头像标题string | React.ReactElement;
subTitle上传头像小标题string | React.ReactElement;
previewTitle头像预览标题string | React.ReactElement;
reloadTitle重新上传标题string | React.ReactElement;
uploadProps上传配置UploadProps
modalProps模态框的配置ModalProps
limit限制内容boolean{type: ‘jpeg,png,jpg’,size: 1024,}
previewList定义预览的大小number[][80, 30, 18]
editorWidth裁剪容器宽度number540
editorHeight裁剪容器高度number300
minRectSize最小裁剪大小number80
defaultRectSize默认裁剪大小number200
axiosConfigaxios 上传配置AxiosRequestConfig