Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#114 image compression (+webp) #208

Merged
merged 5 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"@types/react-gtm-module": "^2.0.1",
"axios": "^0.26.0",
"compressorjs": "^1.1.1",
"qs": "^6.11.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
Expand Down
19 changes: 13 additions & 6 deletions src/@components/JoinPage/UserProfilePage/ProfileImage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@ import { useState } from "react";

import { IcAddProfileBtn } from "../../../../asset/icon";
import { ImgDefaultBigProfile } from "../../../../asset/image";
import compressImage from "../../../../util/imageCompressor";
import { St } from "./style";

interface ProfileImageProps {
setImage: (file: File) => void;
setProfileImage: (file: File) => void;
}

const MAX_IMAGE_SIZE = 80 * 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

최대치가 *2인 이유가 있을까요?!?!

Copy link
Member Author

@joohaem joohaem Feb 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

80으로 직접 명시를 해도 좋은데, 레티나 디스플레이 대응 이슈로 인해 2배 정도로 지정하면 좋다고 알아요!!!~

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 구렇구나😆😆


export default function ProfileImage(props: ProfileImageProps) {
const { setImage } = props;
const [imgUrl, setImgUrl] = useState<string>("");
const { setProfileImage } = props;
const [previewImgUrl, setPreviewImgUrl] = useState("");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<string> 요거 있는거랑 없는거랑 상관업숴?-?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"" 로 string인게 이미 추론되어서, 직접 명시할 필요 없어!!


const handleImagePatch = async (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files === null) return;

const selectedImg = e.target.files[0];
setImgUrl(URL.createObjectURL(selectedImg));
const compressedSelectedImg = await compressImage(selectedImg, {
maxWidth: MAX_IMAGE_SIZE,
maxHeight: MAX_IMAGE_SIZE,
});
Comment on lines +22 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요로코롬 사용하눈 거구나👀👀 므싯다요😎😎😎😎


setImage(selectedImg);
setProfileImage(compressedSelectedImg);
setPreviewImgUrl(URL.createObjectURL(compressedSelectedImg));
};

return (
<St.ProfileImage>
<St.ImageContainer>
<St.ImageWrapper>
<St.AddImage src={imgUrl ? imgUrl : ImgDefaultBigProfile} alt="프로필" />
<St.AddImage src={previewImgUrl ? previewImgUrl : ImgDefaultBigProfile} alt="프로필" />
</St.ImageWrapper>
<St.AddBtnWrapper>
<IcAddProfileBtn />
Expand Down
7 changes: 4 additions & 3 deletions src/@components/JoinPage/UserProfilePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function UserProfilePage() {
const [birthData, setBirthData] = useState<string>(""); // 생년월일
// TODO :: 변수명 gender로 바꾸기
const [isSelected, setIsSelected] = useState<string>(""); // 성별
const [image, setImage] = useState(ImgDefaultBigProfile); // 이미지
const [profileImage, setProfileImage] = useState(ImgDefaultBigProfile); // 이미지

const [isChecked, setIsChecked] = useState(false); //닉넴 중복 확인
const [isInComplete, setisInComplete] = useState(false); // 다음으로 버튼
Expand All @@ -44,7 +44,7 @@ export default function UserProfilePage() {
currentFormData.append("birthday", birthData);

if (isSelected) currentFormData.append("gender", isSelected);
if (image) currentFormData.append("imgFile", image);
if (profileImage) currentFormData.append("imgFile", profileImage);

return currentFormData;
});
Expand All @@ -64,7 +64,8 @@ export default function UserProfilePage() {
<St.ProfileContainer>
<St.Title>프로필을 설정해주세요</St.Title>
<St.SubTitle>프로필 사진(선택)</St.SubTitle>
<ProfileImage setImage={setImage} />
<ProfileImage setProfileImage={setProfileImage} />

<St.SubTitle>닉네임(필수)</St.SubTitle>
<St.Requirement>※ 한글, 영문, 숫자 상관없이 8자 이내</St.Requirement>
<ProfileNickname
Expand Down
1 change: 1 addition & 0 deletions src/asset/asset.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare module "*.jpg";
declare module "*.png";
declare module "*.jpeg";
declare module "*.gif";
declare module "*.webp";

declare module "*.svg" {
import React = require("react");
Expand Down
Binary file added src/asset/image/banner_1.webp
Binary file not shown.
Binary file added src/asset/image/banner_2.webp
Binary file not shown.
Binary file added src/asset/image/banner_3.webp
Binary file not shown.
Binary file added src/asset/image/categoryBanner.webp
Binary file not shown.
Binary file added src/asset/image/defaultBigProfile.webp
Binary file not shown.
Binary file added src/asset/image/defaultProfile.webp
Binary file not shown.
14 changes: 7 additions & 7 deletions src/asset/image/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export { default as ImgBanner1 } from "./banner_1.png";
export { default as ImgBanner2 } from "./banner_2.png";
export { default as ImgBanner3 } from "./banner_3.png";
export { default as ImgCategoryBanner } from "./categoryBanner.png";
export { default as ImgDefaultBigProfile } from "./defaultBigProfile.png";
export { default as ImgDefaultProfile } from "./defaultProfile.png";
export { default as ImgBanner1 } from "./banner_1.webp";
export { default as ImgBanner2 } from "./banner_2.webp";
export { default as ImgBanner3 } from "./banner_3.webp";
export { default as ImgCategoryBanner } from "./categoryBanner.webp";
export { default as ImgDefaultBigProfile } from "./defaultBigProfile.webp";
export { default as ImgDefaultProfile } from "./defaultProfile.webp";
export { default as ImgPiickleLoading } from "./piickleLoading.gif";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.gif.webp로 변환하지 않은 이유가 있을까요?-? 마찬가지로 용량이 줄어든다고 알고 있어서요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 그것도 되는구나!! 변경해놓을게요

export { default as ImgVoteBanner } from "./voteBanner.png";
export { default as ImgVoteBanner } from "./voteBanner.webp";
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added src/asset/image/voteBanner.webp
Binary file not shown.
26 changes: 26 additions & 0 deletions src/util/imageCompressor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Compressor from "compressorjs";

interface CompressorImageOption {
maxWidth: number;
maxHeight: number;
quality?: number;
}

// TODO :: File | Blob 호환성
export default function compressImage(file: File, option?: CompressorImageOption): Promise<File> {
return new Promise((resolve, reject) => {
new Compressor(file, {
maxWidth: option?.maxWidth,
maxHeight: option?.maxHeight,
quality: option?.quality ?? 0.6,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? 요거 무엇인가요?_? 처음 봐써요!👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined, null 일 경우에만 ?? 다음의 값을 반환해줘!


success: (result) => {
// if(result instance File)
resolve(new File([result], file.name));
},
error: (result) => {
reject(result);
},
});
});
}
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4722,6 +4722,11 @@ bluebird@^3.5.5:
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
integrity sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg==

blueimp-canvas-to-blob@^3.29.0:
version "3.29.0"
resolved "https://registry.yarnpkg.com/blueimp-canvas-to-blob/-/blueimp-canvas-to-blob-3.29.0.tgz#d965f06cb1a67fdae207a2be56683f55ef531466"
integrity sha512-0pcSSGxC0QxT+yVkivxIqW0Y4VlO2XSDPofBAqoJ1qJxgH9eiUDLv50Rixij2cDuEfx4M6DpD9UGZpRhT5Q8qg==

bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.11.9:
version "4.12.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
Expand Down Expand Up @@ -5553,6 +5558,14 @@ compression@^1.7.4:
safe-buffer "5.1.2"
vary "~1.1.2"

compressorjs@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/compressorjs/-/compressorjs-1.1.1.tgz#54c147cf37fb38828b08c48646d0258d52faf050"
integrity sha512-SysRuUPfmUNoq+RviE0iMFVUmoX2q/x+7PkEPUmk6NGkd85hDrmvujx0Qtp8UCGA6KMe5kuodsylPQcNaLf60w==
dependencies:
blueimp-canvas-to-blob "^3.29.0"
is-blob "^2.1.0"

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -8614,6 +8627,11 @@ is-binary-path@~2.1.0:
dependencies:
binary-extensions "^2.0.0"

is-blob@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-blob/-/is-blob-2.1.0.tgz#e36cd82c90653f1e1b930f11baf9c64216a05385"
integrity sha512-SZ/fTft5eUhQM6oF/ZaASFDEdbFVe89Imltn9uZr03wdKMcWNVYSMjQPFtg05QuNkt5l5c135ElvXEQG0rk4tw==

is-boolean-object@^1.1.0:
version "1.1.2"
resolved "https://registry.yarnpkg.com/is-boolean-object/-/is-boolean-object-1.1.2.tgz#5c6dc200246dd9321ae4b885a114bb1f75f63719"
Expand Down