-
Notifications
You must be signed in to change notification settings - Fork 12
/
ImageUploader.jsx
165 lines (150 loc) · 4.46 KB
/
ImageUploader.jsx
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
164
165
import React, { useState, useCallback, useRef, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, TextField } from '@mui/material';
import { get } from 'lodash';
import ReactCrop from 'react-image-crop';
import "react-image-crop/dist/ReactCrop.css";
// Increase pixel density for crop preview quality on retina screens.
const pixelRatio = window.devicePixelRatio || 1;
// We resize the canvas down when saving on retina devices otherwise the image
// will be double or triple the preview size.
/* const getResizedCanvas = (canvas, newWidth, newHeight) => {
* const tmpCanvas = document.createElement("canvas");
* tmpCanvas.width = newWidth;
* tmpCanvas.height = newHeight;
*
* const ctx = tmpCanvas.getContext("2d");
* ctx.drawImage(
* canvas,
* 0,
* 0,
* canvas.width,
* canvas.height,
* 0,
* 0,
* newWidth,
* newHeight
* );
*
* return tmpCanvas;
* }
* */
/* const generateDownload = (previewCanvas, crop) => {
* if (!crop || !previewCanvas) {
* return;
* }
*
* const canvas = getResizedCanvas(previewCanvas, crop.width, crop.height);
*
* canvas.toBlob(
* (blob) => {
* const previewUrl = window.URL.createObjectURL(blob);
*
* const anchor = document.createElement("a");
* anchor.download = "cropPreview.png";
* anchor.href = URL.createObjectURL(blob);
* anchor.click();
*
* window.URL.revokeObjectURL(previewUrl);
* },
* "image/png",
* 1
* );
* }
* */
const ImageUploader = props => {
const { t } = useTranslation()
const [fileName, setFileName] = useState(props.defaultName);
const [upImg, setUpImg] = useState(props.defaultImg);
const imgRef = useRef(null);
const previewCanvasRef = useRef(null);
const [crop, setCrop] = useState({ unit: "%", width: 30, height: props.isCircle ? undefined : 30, aspect: props.isCircle ? 1 : undefined });
const [completedCrop, setCompletedCrop] = useState(null);
const [base64, setBase64] = useState(null);
const onSelectFile = (e) => {
if (e.target.files && e.target.files.length > 0) {
const reader = new FileReader();
reader.addEventListener("load", () => setUpImg(reader.result));
const file = e.target.files[0];
setFileName(file.name)
reader.readAsDataURL(e.target.files[0]);
}
};
const onLoad = useCallback((img) => {
imgRef.current = img;
}, []);
const uploadImage = () => {
props.onUpload(base64, fileName)
}
useEffect(() => {
if (!completedCrop || !previewCanvasRef.current || !imgRef.current) {
return;
}
const image = imgRef.current;
const canvas = previewCanvasRef.current;
const crop = completedCrop;
const scaleX = image.naturalWidth / image.width;
const scaleY = image.naturalHeight / image.height;
const ctx = canvas.getContext("2d");
canvas.width = crop.width * pixelRatio;
canvas.height = crop.height * pixelRatio;
ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
ctx.imageSmoothingQuality = "high";
ctx.drawImage(
image,
crop.x * scaleX,
crop.y * scaleY,
crop.width * scaleX,
crop.height * scaleY,
0,
0,
crop.width,
crop.height
);
setBase64(canvas.toDataURL());
}, [completedCrop]);
return (
<div className="App">
<div className='flex-vertical-center'>
<TextField
variant="outlined"
inputProps={{
type: "file",
accept: "image/*"
}}
onChange={onSelectFile}
/>
<Button
style={{marginLeft: '10px'}}
variant='outlined'
color='primary'
disabled={!get(completedCrop, 'width') || !get(completedCrop, 'height')}
onClick={uploadImage}
>
{t('common.upload')}
</Button>
</div>
<div style={{marginTop: '10px'}}>
<ReactCrop
src={upImg}
onImageLoaded={onLoad}
crop={crop}
onChange={(c) => setCrop(c)}
onComplete={(c) => setCompletedCrop(c)}
crossorigin='Anonymous'
circularCrop={props.isCircle}
/>
</div>
<div style={{display: 'none'}}>
<canvas
ref={previewCanvasRef}
style={{
width: Math.round(get(completedCrop, 'width', 0)),
height: Math.round(get(completedCrop, 'height', 0))
}}
/>
</div>
</div>
);
}
export default ImageUploader;