Skip to content

Commit 94a4d5f

Browse files
authored
merge: 이미지 등록 시 이미지 파일 압축 및 heic 파일 변환
Refactor: 이미지 등록 시 이미지 파일 압축 및 heic 파일 변환
2 parents cd9b3a9 + 658f43c commit 94a4d5f

4 files changed

Lines changed: 456 additions & 177 deletions

File tree

package-lock.json

Lines changed: 113 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
"@emotion/babel-preset-css-prop": "^11.2.0",
77
"@emotion/react": "^11.9.0",
88
"@emotion/styled": "^11.8.1",
9+
"@reduxjs/toolkit": "^1.8.2",
910
"@testing-library/jest-dom": "^5.16.4",
1011
"@testing-library/react": "^13.3.0",
1112
"@testing-library/user-event": "^13.5.0",
1213
"axios": "^0.27.2",
14+
"browser-image-compression": "^2.0.0",
15+
"heic2any": "^0.0.3",
1316
"react": "^18.1.0",
1417
"react-dom": "^18.1.0",
1518
"react-router-dom": "^6.3.0",

src/components/UploadImage/index.jsx

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import theme from 'styles/theme';
55
import { IMAGE_URLS } from 'utils/constants/images';
66
import { Modal } from 'components';
77

8+
import imageCompression from 'browser-image-compression';
9+
import heic2any from 'heic2any';
10+
811
const { backgroundGreen, mainWhite } = theme.color;
912
const { POST_ADD_IMG } = IMAGE_URLS;
1013

@@ -39,36 +42,59 @@ const ImageInner = styled.div`
3942
background-repeat: no-repeat;
4043
`;
4144

45+
const CompressImage = async (fileSrc) => {
46+
const options = {
47+
maxSizeMB: 1,
48+
maxWidthOrHeight: 1920,
49+
useWebWorker: true,
50+
};
51+
try {
52+
return await imageCompression(fileSrc, options);
53+
} catch (error) {
54+
alert(error);
55+
}
56+
};
57+
4258
const UploadImage = ({ onChange, defaultImage, ...props }) => {
4359
const [imageSrc, setImageSrc] = useState(defaultImage);
4460
const fileInputRef = useRef(null);
4561
const [modalMsg, setModalMsg] = useState({ isModal: false, title: '', description: '' });
4662

47-
const handleFileChange = (e) => {
48-
const fileBlob = e.target.files[0];
63+
const handleFileChange = async (e) => {
64+
let fileBlob = e.target.files[0];
65+
const MAX_MB = 10;
4966

5067
if (!fileBlob) {
5168
return;
5269
}
5370

54-
if (!/\.(gif|jpg|jpeg|png)$/i.test(fileBlob.name)) {
71+
if (!/\.(gif|jpg|jpeg|png|heic)$/i.test(fileBlob.name)) {
5572
setModalMsg({
5673
isModal: true,
5774
title: '등록할 수 없는 파일입니다.',
58-
description: '등록 가능한 확장자: jpg, jpeg, gif, png',
75+
description: '등록 가능한 확장자: jpg, jpeg, gif, png, heic',
5976
});
6077
return;
6178
}
6279

63-
if (fileBlob.size > 1024 * 1024) {
80+
if (fileBlob.size > 1024 * 1024 * MAX_MB) {
6481
setModalMsg({
6582
isModal: true,
66-
title: '1MB 이하 파일만 등록해 주세요!',
83+
title: `${MAX_MB}MB 이하 파일만 등록해 주세요!`,
6784
description: `현재 파일 용량 : ${Math.round((fileBlob.size / 1024 / 1024) * 100) / 100}MB`,
6885
});
6986
return;
7087
}
7188

89+
if (/\.(heic)$/i.test(fileBlob.name)) {
90+
fileBlob = await heic2any({ blob: fileBlob, toType: 'image/jpeg' });
91+
}
92+
93+
// 1MB 보다 크다면 변환
94+
if (fileBlob.size > 1024 * 1024) {
95+
fileBlob = await CompressImage(fileBlob);
96+
}
97+
7298
const reader = new FileReader();
7399
reader.readAsDataURL(fileBlob);
74100
reader.onload = () => {

0 commit comments

Comments
 (0)