import React, { useState } from 'react'; import { Upload, Download, ArrowRight, X } from 'lucide-react'; interface UploadedImage { id: string; file: File; preview: string; name: string; size: number; originalFormat: string; } interface ConvertedImage { id: string; originalName: string; originalFormat: string; outputFormat: string; preview: string; size: number; downloadCount: number; } const IMAGE_FORMATS = [ { format: 'JPEG', extension: 'jpg', estimatedSize: '~45-65%' }, { format: 'PNG', extension: 'png', estimatedSize: '~80-120%' }, { format: 'WebP', extension: 'webp', estimatedSize: '~30-50%' }, { format: 'GIF', extension: 'gif', estimatedSize: '~60-100%' }, { format: 'BMP', extension: 'bmp', estimatedSize: '~150-200%' }, { format: 'TIFF', extension: 'tiff', estimatedSize: '~100-150%' }, { format: 'SVG', extension: 'svg', estimatedSize: '~10-30%' }, { format: 'ICO', extension: 'ico', estimatedSize: '~5-15%' }, { format: 'AVIF', extension: 'avif', estimatedSize: '~25-40%' }, { format: 'HEIC', extension: 'heic', estimatedSize: '~35-55%' }, ]; export default function ImageFormatConverter() { const [uploadedImages, setUploadedImages] = useState([]); const [selectedFormat, setSelectedFormat] = useState('JPEG'); const [convertedImages, setConvertedImages] = useState([]); const [currentPage, setCurrentPage] = useState<'upload' | 'download'>('upload'); const [dragActive, setDragActive] = useState(false); const handleDrag = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); if (e.type === 'dragenter' || e.type === 'dragover') { setDragActive(true); } else if (e.type === 'dragleave') { setDragActive(false); } }; const handleDrop = (e: React.DragEvent) => { e.preventDefault(); e.stopPropagation(); setDragActive(false); const files = Array.from(e.dataTransfer.files).filter((file) => file.type.startsWith('image/') ); processFiles(files); }; const handleFileInput = (e: React.ChangeEvent) => { const files = Array.from(e.target.files || []); processFiles(files); }; const processFiles = (files: File[]) => { files.forEach((file) => { const reader = new FileReader(); reader.onload = (e) => { const format = file.type.split('/')[1].toUpperCase(); const newImage: UploadedImage = { id: Math.random().toString(36).substr(2, 9), file, preview: e.target?.result as string, name: file.name, size: file.size, originalFormat: format, }; setUploadedImages((prev) => [...prev, newImage]); }; reader.readAsDataURL(file); }); }; const removeImage = (id: string) => { setUploadedImages((prev) => prev.filter((img) => img.id !== id)); }; const formatFileSize = (bytes: number): string => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i]; }; const handleStartConversion = () => { const converted = uploadedImages.map((img) => ({ id: img.id, originalName: img.name.split('.')[0], originalFormat: img.originalFormat, outputFormat: selectedFormat, preview: img.preview, size: Math.round(img.size * (Math.random() * 0.5 + 0.5)), downloadCount: 0, })); setConvertedImages(converted); setCurrentPage('download'); }; const getDownloadFilename = (image: ConvertedImage): string => { const baseName = image.originalName; const extension = IMAGE_FORMATS.find((f) => f.format === image.outputFormat)?.extension || 'jpg'; if (image.downloadCount === 0) { return `${baseName}.${extension}`; } return `${baseName}(${image.downloadCount}).${extension}`; }; const handleDownload = (imageId: string) => { setConvertedImages((prev) => prev.map((img) => img.id === imageId ? { ...img, downloadCount: img.downloadCount + 1 } : img ) ); const image = convertedImages.find((img) => img.id === imageId); if (image) { const link = document.createElement('a'); link.href = image.preview; link.download = getDownloadFilename(image); document.body.appendChild(link); link.click(); document.body.removeChild(link); } }; const handleBackToUpload = () => { setCurrentPage('upload'); setUploadedImages([]); setConvertedImages([]); setSelectedFormat('JPEG'); }; if (currentPage === 'download') { return (

Converted Images

Your images have been converted to {selectedFormat}. Download them below.

{convertedImages.map((image) => (
{image.originalName}

{image.originalName}

Original: {image.originalFormat}

Output: {image.outputFormat}

Size: {formatFileSize(image.size)}

{image.downloadCount > 0 && (

Downloaded {image.downloadCount} time(s)

)}
))}
); } return (

Image Format Converter

Upload images and convert them to your desired format

{/* Upload Section */}

Upload Images

{/* Drag and Drop Area */}

Drag and drop images here

or

{/* Uploaded Images List */} {uploadedImages.length > 0 && (

Uploaded Images ({uploadedImages.length})

{uploadedImages.map((image) => (
{image.name}

{image.name}

{image.originalFormat} • {formatFileSize(image.size)}

))}
)}
{/* Format Selection Section */}

Output Format

{IMAGE_FORMATS.map((fmt) => ( ))}
{uploadedImages.length === 0 && (

Upload images to start conversion

)}
); }