From d2dabd48de4c9cd176aa4757874721dfdf1c8092 Mon Sep 17 00:00:00 2001 From: ParkGeunCheol <72205402+GC-Park@users.noreply.github.com> Date: Wed, 20 Sep 2023 21:01:17 +0900 Subject: [PATCH] =?UTF-8?q?[FE]=20Feature/#435=20=EC=9D=B4=EB=AF=B8?= =?UTF-8?q?=EC=A7=80=20=EC=95=95=EC=B6=95=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20(#436)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 이미지 압축하는 기능 custom hook으로 구현 * feat: 지도, 핀 등에 이미지 추가할 시 이미지 압축 기능 추가 * refactor: NewTopic, NewPin 텍스트 수정 * refactor: 이미지 리사이징 최대 크기 변경 * refactor: 지도 추가 페이지에서 이미지와 파일 추가 버튼 사이에 space 추가 --- frontend/src/hooks/useCompressImage.ts | 30 ++++++++++++++++++++++++++ frontend/src/pages/NewPin.tsx | 29 +++++++++++++------------ frontend/src/pages/NewTopic.tsx | 17 ++++++++++++--- frontend/src/pages/PinDetail.tsx | 6 +++++- 4 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 frontend/src/hooks/useCompressImage.ts diff --git a/frontend/src/hooks/useCompressImage.ts b/frontend/src/hooks/useCompressImage.ts new file mode 100644 index 000000000..52579a0cc --- /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 1e1233f4c..799b194c7 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 = () => { - - - 지도 선택 - - - - * - - - + + 지도 선택 + + + +