功能需求

有这样一个需求,用户点击文字或者按钮,会弹出图片的预览界面,要求图片可旋转,可缩放以及可下载。由于antd组件的Image的预览窗口不包含图片下载的功能,所以这里通过modal对img添加预览模块。

实现过程

代码如下:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import React from 'react';
import './style.css';
import { Button, Modal } from 'antd';
import {
ZoomInOutlined,
ZoomOutOutlined,
UndoOutlined,
RedoOutlined,
DownloadOutlined,
} from '@ant-design/icons';

export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
imgSrc:
'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
imageVisible: false,
imgScale: '100%',
imgTransform: '',
imgCurrent: 0,
};
}

closeImagePreviewModel = () => {
this.setState({
imageVisible: false,
imgScale: '100%',
imgTransform: '',
imgCurrent: 0,
});
};
//放大
imgToBigger = () => {
let a = parseInt(this.state.imgScale) + 5 + '%';
this.setState({ imgScale: a });
};
//缩小
imgToSmaller = () => {
let a = parseInt(this.state.imgScale) + -5 + '%';
this.setState({ imgScale: a });
};
//左旋转
imgToLeftRoll = () => {
let a = (this.state.imgCurrent - 90) % 360;
this.setState({ imgTransform: 'rotate(' + a + 'deg)', imgCurrent: a });
};
//右旋转
imgToRightRoll = () => {
let a = (this.state.imgCurrent + 90) % 360;
this.setState({ imgTransform: 'rotate(' + a + 'deg)', imgCurrent: a });
};
// 下载
downloadImage = () => {
const imgUrl = this.state.imgSrc;
const xhr = new XMLHttpRequest();
let url = imgUrl;
xhr.responseType = 'blob';
xhr.onload = function () {
if (this.status == '200') {
let blob = this.response;
let a = document.createElement('a');
a.style = 'display:none';
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = function (e) {
a.download = 'test.jpg';
a.href = e.target.result;
document.body.append(a);
a.click();
a.remove();
};
}
};
xhr.open('get', url, true);
xhr.send();
};

render = () => {
const { imgScale, imgSrc, imgTransform, imageVisible } = this.state;
return (
<div>
<Button
onClick={() => {
this.setState({ imageVisible: true });
}}
>
预览图片
</Button>
<Modal
title="图片预览"
visible={imageVisible}
onCancel={this.closeImagePreviewModel}
style={{
top: 0,
maxWidth: '100vw',
paddingBottom: 0,
}}
bodyStyle={{
textAlign: 'center',
height: 'calc(100vh - 118px)',
overflowY: 'auto',
}}
width="100vw"
footer={[
<div style={{ margin: '0 auto', textAlign: 'center' }}>
<Button
style={{ border: 'none', padding: '5px 8px' }}
title="放大"
onClick={() => this.imgToBigger()}
>
<ZoomInOutlined />
</Button>
<Button
style={{ border: 'none', padding: '5px 8px' }}
title="缩小"
onClick={() => this.imgToSmaller()}
>
<ZoomOutOutlined />
</Button>
<Button
style={{ border: 'none', padding: '5px 8px' }}
title="逆时针旋转"
onClick={() => this.imgToLeftRoll()}
>
<UndoOutlined />
</Button>
<Button
style={{ border: 'none', padding: '5px 8px' }}
title="顺时针旋转"
onClick={() => this.imgToRightRoll()}
>
<RedoOutlined />
</Button>
<Button
style={{ border: 'none', padding: '5px 8px' }}
title="下载"
onClick={() => this.downloadImage()}
>
<DownloadOutlined />
</Button>
</div>,
]}
>
<img
src={imgSrc}
style={{
width: 'auto',
height: 'auto',
maxWidth: '100vw',
maxHeight: 'calc(100vh - 158px)',
position: 'relative',
margin: '0 auto',
scale: imgScale,
transform: imgTransform,
}}
/>
</Modal>
</div>
);
};
}

实现效果

React antd 自定义图片预览实现