@@ -5,6 +5,9 @@ import theme from 'styles/theme';
55import { IMAGE_URLS } from 'utils/constants/images' ;
66import { Modal } from 'components' ;
77
8+ import imageCompression from 'browser-image-compression' ;
9+ import heic2any from 'heic2any' ;
10+
811const { backgroundGreen, mainWhite } = theme . color ;
912const { 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+
4258const 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 ( ! / \. ( g i f | j p g | j p e g | p n g ) $ / i. test ( fileBlob . name ) ) {
71+ if ( ! / \. ( g i f | j p g | j p e g | p n g | h e i c ) $ / 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 ( / \. ( h e i c ) $ / 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