diff --git a/frontend/src/hooks/useCompressImage.ts b/frontend/src/hooks/useCompressImage.ts new file mode 100644 index 00000000..52579a0c --- /dev/null +++ b/frontend/src/hooks/useCompressImage.ts @@ -0,0 +1,30 @@ +import imageCompression from 'browser-image-compression'; + +const useCompressImage = () => { + const compressImage = async (file: File) => { + const resizingBlob = await imageCompression(file, { + maxSizeMB: 1, + maxWidthOrHeight: 750, + useWebWorker: true, + }); + const resizingFile = new File([resizingBlob], file.name, { + type: file.type, + }); + return resizingFile; + }; + + const compressImageList = async (files: FileList) => { + const compressedImageList: File[] = []; + + for (const file of files) { + const compressedImage = await compressImage(file); + compressedImageList.push(compressedImage); + } + + return compressedImageList; + }; + + return { compressImage, compressImageList }; +}; + +export default useCompressImage; diff --git a/frontend/src/pages/NewPin.tsx b/frontend/src/pages/NewPin.tsx index 1e1233f4..799b194c 100644 --- a/frontend/src/pages/NewPin.tsx +++ b/frontend/src/pages/NewPin.tsx @@ -24,6 +24,7 @@ import Modal from '../components/Modal'; import { styled } from 'styled-components'; import ModalMyTopicList from '../components/ModalMyTopicList'; import { getMapApi } from '../apis/getMapApi'; +import useCompressImage from '../hooks/useCompressImage'; type NewPinFormValueType = Pick< NewPinFormProps, @@ -49,6 +50,7 @@ const NewPin = () => { const { showToast } = useToast(); const { width } = useSetLayoutWidth(SIDEBAR); const { openModal, closeModal } = useContext(ModalContext); + const { compressImageList } = useCompressImage(); const [formImages, setFormImages] = useState([]); @@ -177,7 +179,9 @@ const NewPin = () => { }); }; - const onPinImageChange = (event: React.ChangeEvent) => { + const onPinImageChange = async ( + event: React.ChangeEvent, + ) => { const imageLists = event.target.files; let imageUrlLists = [...showedImages]; @@ -189,8 +193,10 @@ const NewPin = () => { return; } + const compressedImageList = await compressImageList(imageLists); + for (let i = 0; i < imageLists.length; i++) { - const currentImageUrl = URL.createObjectURL(imageLists[i]); + const currentImageUrl = URL.createObjectURL(compressedImageList[i]); imageUrlLists.push(currentImageUrl); } @@ -203,7 +209,7 @@ const NewPin = () => { return; } - setFormImages([...formImages, ...imageLists]); + setFormImages([...formImages, ...compressedImageList]); setShowedImages(imageUrlLists); }; @@ -239,16 +245,12 @@ const NewPin = () => { - - - 지도 선택 - - - - * - - - + + 지도 선택 + + + +