feat: utilitaires client heicConverter, bgRemover, imageRotator, zipImporter (TS)
Porte les 4 utilitaires de traitement d'images en TypeScript pour utilisation côté client : - heicConverter.ts : convertit HEIC/HEIF en JPEG via heic2any - bgRemover.ts : supprime les fonds d'images avec @imgly/background-removal + composite sur blanc - imageRotator.ts : pivote les images JPEG via Canvas - zipImporter.ts : extrait les images d'un fichier ZIP avec structure dossier-ref/image.ext Tous les modules utilisent des APIs du navigateur et doivent être importés dynamiquement depuis du code 'use client'. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import JSZip from 'jszip'
|
||||
import type { ZipEntry } from '@/types/images'
|
||||
|
||||
const IMAGE_EXTS = new Set(['heic', 'heif', 'jpg', 'jpeg', 'png'])
|
||||
|
||||
/**
|
||||
* Extrait toutes les images d'un fichier ZIP.
|
||||
* Structure attendue : dossier-ref/image.ext (le nom du dossier = référence produit).
|
||||
*/
|
||||
export async function readImagesFromZip(file: File): Promise<ZipEntry[]> {
|
||||
const zip = await JSZip.loadAsync(file)
|
||||
|
||||
const entries: Array<{ relativePath: string; entry: JSZip.JSZipObject }> = []
|
||||
zip.forEach((relativePath, entry) => {
|
||||
if (!entry.dir) entries.push({ relativePath, entry })
|
||||
})
|
||||
|
||||
const results: ZipEntry[] = []
|
||||
|
||||
for (const { relativePath, entry } of entries) {
|
||||
if (relativePath.startsWith('__MACOSX/')) continue
|
||||
|
||||
const parts = relativePath.split('/').filter((p) => p.length > 0)
|
||||
const filename = parts[parts.length - 1]
|
||||
if (filename.startsWith('.')) continue
|
||||
|
||||
const dotIdx = filename.lastIndexOf('.')
|
||||
if (dotIdx === -1) continue
|
||||
const ext = filename.slice(dotIdx + 1).toLowerCase()
|
||||
if (!IMAGE_EXTS.has(ext)) continue
|
||||
|
||||
const folder = parts.length > 1 ? parts[0] : filename.slice(0, dotIdx)
|
||||
const isHeic = ext === 'heic' || ext === 'heif'
|
||||
const mimeType = isHeic
|
||||
? 'image/heic'
|
||||
: ext === 'jpg' || ext === 'jpeg'
|
||||
? 'image/jpeg'
|
||||
: 'image/png'
|
||||
|
||||
const rawBlob = await entry.async('blob')
|
||||
const blob = new Blob([rawBlob], { type: mimeType })
|
||||
|
||||
results.push({ folder, filename: filename.slice(0, dotIdx), ext, blob, isHeic })
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user