import {heicTo, isHeic} from '../dist/heic-to.js'
const fileInput = document.getElementById("fileInput")
const statusMessage = document.getElementById("statusMessage")
const resultContainer = document.getElementById("resultContainer")
const originalImage = document.getElementById("originalImage")
const convertedImage = document.getElementById("convertedImage")
function showStatus(message, type = 'info') {
statusMessage.textContent = message
statusMessage.className = `status-message ${type}`
statusMessage.style.display = 'block'
}
function hideStatus() {
statusMessage.style.display = 'none'
}
function showResult() {
resultContainer.style.display = 'grid'
}
function hideResult() {
resultContainer.style.display = 'none'
}
function createImageElement(src, alt = '') {
const img = document.createElement('img')
img.src = src
img.alt = alt
img.style.maxWidth = '100%'
img.style.maxHeight = '300px'
img.style.objectFit = 'contain'
return img
}
function clearContainer(container) {
container.innerHTML = '
Processing...
'
}
function formatFileSize(bytes) {
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 parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
fileInput.addEventListener('change', async (event) => {
const file = event.target.files[0]
if (!file) {
hideStatus()
hideResult()
return
}
try {
// Reset UI
hideResult()
clearContainer(originalImage)
clearContainer(convertedImage)
showStatus('đ Analyzing file...', 'loading')
// Check if it's a HEIC file
const isHeicFile = await isHeic(file)
if (isHeicFile) {
showStatus('â
HEIC/HEIF file detected! Converting to JPEG...', 'loading')
// Show original file info
originalImage.innerHTML = `
đˇ
${file.name}
HEIC/HEIF âĸ ${formatFileSize(file.size)}
`
showResult()
// Convert to JPEG
const startTime = Date.now()
const convertedBlob = await heicTo({
blob: file,
type: 'image/jpeg',
quality: 0.8
})
const conversionTime = Date.now() - startTime
// Show converted image
const convertedUrl = URL.createObjectURL(convertedBlob)
const convertedImg = createImageElement(convertedUrl, 'Converted JPEG image')
convertedImage.innerHTML = ''
convertedImage.appendChild(convertedImg)
// Add download button and info
const infoDiv = document.createElement('div')
infoDiv.style.marginTop = '15px'
infoDiv.style.textAlign = 'center'
infoDiv.innerHTML = `
JPEG âĸ ${formatFileSize(convertedBlob.size)} âĸ ${conversionTime}ms
`
convertedImage.appendChild(infoDiv)
const compressionRatio = ((file.size - convertedBlob.size) / file.size * 100).toFixed(1)
const sizeChange = convertedBlob.size > file.size ? 'larger' : 'smaller'
showStatus(
`đ Successfully converted! File is ${Math.abs(compressionRatio)}% ${sizeChange} (${conversionTime}ms)`,
'success'
)
} else {
// Not a HEIC file - show as regular image if possible
showStatus('âšī¸ This is not a HEIC/HEIF file. Displaying as regular image.', 'info')
if (file.type.startsWith('image/')) {
const imageUrl = URL.createObjectURL(file)
const img = createImageElement(imageUrl, 'Original image')
originalImage.innerHTML = ''
originalImage.appendChild(img)
const infoDiv = document.createElement('div')
infoDiv.style.marginTop = '15px'
infoDiv.style.textAlign = 'center'
infoDiv.innerHTML = `
${file.name}
${file.type} âĸ ${formatFileSize(file.size)}
`
originalImage.appendChild(infoDiv)
convertedImage.innerHTML = `
âšī¸
No conversion needed
This file is already in a web-compatible format
`
showResult()
} else {
originalImage.innerHTML = `
â
Unsupported file type
Please select a HEIC/HEIF or image file
`
showResult()
}
}
} catch (error) {
console.error('Conversion error:', error)
showStatus(`â Error: ${error.message || 'Failed to process file'}`, 'error')
hideResult()
}
})
// Global function for download button
window.downloadImage = function(url, filename) {
const a = document.createElement('a')
a.href = url
a.download = filename
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
// Clean up object URLs when page unloads
window.addEventListener('beforeunload', () => {
const images = document.querySelectorAll('img[src^="blob:"]')
images.forEach(img => URL.revokeObjectURL(img.src))
})