Notice
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- img upload
- 에러
- AWS
- Access Key 생성
- error
- Next.js
- react native 개발
- 리액트 네이티브 에러
- fire base
- react native font
- PongWorld
- s3 upload
- GIT
- React
- 리엑트 네이티브 아이콘
- babel.config.js
- Project
- aws bucket 정책
- 백준
- react native 세팅
- 문자열 대소문자 구별
- 문자열 대소문자
- js
- react native CLI
- 리액트 네이티브
- react native
- 리액트
- firebase 라이브러리
- react native picker
- AWS Access Key
Archives
- Today
- Total
밝을희 클태
[ Next.JS ] Input 태그에서 HEIC 확장자를 jpeg로 변환하기 본문
프로젝트를 진행하던 도중 테스트를 하고 있는데 아이폰에서 찍은 사진을 맥북으로 받아와 업로드하려고 하는데서 문제가 생겼다.
기존에 아래처럼 accept props에 이미지의 모든 확장자를 다 받게 설정을 해뒀는데 HEIC 이미지는 여기에 해당이 되지 않아서 이미지들이 선택이 되지 않았다.
<input
type="file"
multiple
accept="image/*"
ref={fileInputRef}
onChange={handleImageUpload}
id="images"
hidden
/>
아래처럼 image/heic를 추가해 주면 HEIC 이미지들도 input에서 선택이 가능하다.
<input
type="file"
multiple
accept="image/*,image/heic"
ref={fileInputRef}
onChange={handleImageUpload}
id="images"
hidden
/>
그리고 이제 HEIC 파일들을 jpeg로 변환해 주기 위해 heic2any 라이브러리를 다운로드해야 한다. 해당 라이브러리는 HEIC 파일을 JPEG 또는 PNG로 변환할 수 있다.
$ npm install heic2any
https://www.npmjs.com/package/heic2any
heic2any
Converting HEIC/HEIF to PNG/JPEG/GIF in the browser. Latest version: 0.0.4, last published: a year ago. Start using heic2any in your project by running `npm i heic2any`. There are 37 other projects in the npm registry using heic2any.
www.npmjs.com
이미지들을 돌면서 heic 확장자를 찾고 변환을 시켜준다.
promise.all을 사용해서 모든 heic 확장자의 변환이 완료될 때까지 기다려준다.
import heic2any from 'heic2any';
aync function handleImageUpload(e) {
const files = e.target.files;
const filesArray = Array.from(files);
const newArray = await Promise.all(
filesArray.map(async file => {
if (file.type === 'image/heic' || file.name.endsWith('.HEIC')) {
file = await heic2any({
blob: file,
toType: 'image/jpeg',
});
}
return URL.createObjectURL(file);
}),
);
}
r
'NextJS' 카테고리의 다른 글
[ Next.JS ] AWS S3에 이미지(image)를 업로드, 미리보기 (0) | 2024.06.15 |
---|---|
[ Next.js] Image태그의 sizes (1) | 2024.06.12 |
[NextJS] 스크롤 애니메이션 구현하기 IntersectionObserver API (1) | 2023.12.03 |