-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
135 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,34 @@ | ||
// 이미지들을 jpeg로 변환하여 다운로드 | ||
const imageZipDownloader = async (imageUrls: string[]) => { | ||
imageUrls.forEach((url, index) => { | ||
const img = new Image(); | ||
img.crossOrigin = 'anonymous'; | ||
img.src = url; | ||
img.onload = () => { | ||
// create Canvas | ||
const canvas = document.createElement('canvas'); | ||
const ctx: CanvasRenderingContext2D | null = canvas.getContext('2d'); | ||
if (!ctx) return; | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
ctx?.drawImage(img, 0, 0); | ||
// for create tag anchor | ||
const a = document.createElement('a'); | ||
a.download = `image-${index}-download`; | ||
a.href = canvas.toDataURL('image/jpeg'); | ||
a.click(); | ||
}; | ||
}); | ||
import JSZip from 'jszip'; | ||
|
||
// 이미지들을 jpeg로 변환하여 zip 파일로 다운로드 | ||
const imageZipDownloader = async ({ | ||
imageUrls, | ||
}: { | ||
imageUrls: string[]; | ||
}): Promise<void> => { | ||
const zip = new JSZip(); | ||
|
||
for (const url of imageUrls) { | ||
try { | ||
const response = await fetch(url); | ||
const blob = await response.blob(); | ||
const fileName = url.split('/').pop() || 'image'; | ||
zip.file(fileName, blob); | ||
} catch (error) { | ||
console.error('Error processing image:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
const zipBlob = await zip.generateAsync({ type: 'blob' }); | ||
const url = window.URL.createObjectURL(zipBlob); | ||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = 'images.zip'; | ||
document.body.appendChild(a); | ||
a.click(); | ||
document.body.removeChild(a); | ||
window.URL.revokeObjectURL(url); | ||
}; | ||
|
||
export default imageZipDownloader; |