-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.js
122 lines (112 loc) · 3.6 KB
/
upload.js
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
import React, { PropTypes } from 'react'
export default class Upload extends React.Component {
// static propTyps = {
// uploadSrc: PropTypes.string.isRequired
// }
//
// static defaultPropTypes = {
// uploadSrc: ''
// }
constructor(props) {
super(props)
this.drawCanvasImage = this.drawCanvasImage.bind(this)
this.handleImgChange = this.handleImgChange.bind(this)
this.readImage = this.readImage.bind(this)
this.state = {
uploadSrc: ''
}
}
drawCanvasImage(image) {
// 绘制图片
let width = image.width
let height = image.height
// 如果图片大于四百万像素,计算压缩比并将大小压至400万以下
let ratio = width * height / 1000000
if (ratio > 1) {
ratio = Math.sqrt(ratio)
width /= ratio
height /= ratio
} else {
ratio = 1
}
function onloadCanvas() {
// 用于压缩图片的canvas
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
// 瓦片canvas
const tCanvas = document.createElement('canvas')
const tctx = tCanvas.getContext('2d')
// canvas 清屏
ctx.fillStyle = '#fff'
ctx.clearRect(0, 0, canvas.width, canvas.height)
// 重置canvas宽高
canvas.width = width
canvas.height = height
// 绘制
// 如果图片像素大于100万则使用瓦片绘制
let count = width * height / 1000000
if (count > 1) {
// 计算要分成多少块瓦片
count = ~~(Math.sqrt(count) + 1)
// 计算每块瓦片的宽和高
const nw = ~~(width / count)
const nh = ~~(height / count)
tCanvas.width = nw
tCanvas.height = nh
for (let i = 0; i < count; i++) {
for (let j = 0; j < count; j++) {
tctx.drawImage(image, i * nw * ratio, j * nh * ratio,
nw * ratio, nh * ratio, 0, 0, nw, nh)
ctx.drawImage(tCanvas, i * nw, j * nh, nw, nh)
}
}
} else {
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
}
const imgSrcData = canvas.toDataURL('image/jpg', 0.1)
// tCanvas.width = tCanvas.height = canvas.width = canvas.height = 0
this.setState({
uploadSrc: imgSrcData
})
}
onloadCanvas()
}
loadImage(src, onload) {
const img = new Image()
img.src = src
img.onload = () => {
onload(img)
}
img.onerror = () => {
onload(false)
}
}
readImage(src) {
const that = this
// 创建 FileReader 对象 并调用 render 函数来完成渲染
// src.type
const reader = new FileReader()
reader.onload = function(e) {
// that.drawImage(e.target.result)
that.loadImage(e.target.result, that.drawCanvasImage)
}
// 读取文件内容
reader.readAsDataURL(src)
}
handleImgChange(event) {
this.readImage(event.target.files[0])
}
render() {
return (
<div className="upload-wrap">
<input
type="file"
accept="image/png,image/jpeg,image/gif"
name="file"
onChange={this.handleImgChange}
className="upload-file-input"
/>
</div>
)
}
}