mirror of
https://github.com/mito-systems/ranger-app.git
synced 2026-04-28 22:53:47 +00:00
48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
// src/services/imageService.ts
|
|
import imageCompression from 'browser-image-compression'
|
|
import * as FileType from 'file-type'
|
|
|
|
export interface ImageConversionOptions {
|
|
maxSizeMB?: number;
|
|
maxWidthOrHeight?: number;
|
|
useWebWorker?: boolean;
|
|
}
|
|
|
|
export async function normalizeImageUpload(
|
|
file: File,
|
|
options: ImageConversionOptions = {}
|
|
): Promise<File> {
|
|
try {
|
|
// Default conversion options with cross-browser considerations
|
|
const defaultOptions = {
|
|
maxSizeMB: 2, // Keep original size limit
|
|
maxWidthOrHeight: 1920, // Standard max dimension
|
|
useWebWorker: true,
|
|
maxIteration: 10, // Aggressive compression
|
|
fileType: 'image/jpeg' // Force JPEG conversion
|
|
}
|
|
|
|
const mergedOptions = { ...defaultOptions, ...options }
|
|
|
|
// Force conversion to ensure compatibility
|
|
const compressedFile = await imageCompression(file, {
|
|
...mergedOptions,
|
|
fileType: 'image/jpeg', // Force JPEG
|
|
initialQuality: 0.8
|
|
})
|
|
|
|
// Create a new File object with explicit JPEG type
|
|
const normalizedImage = new File(
|
|
[compressedFile],
|
|
'converted-image.jpg',
|
|
{ type: 'image/jpeg' }
|
|
)
|
|
|
|
return normalizedImage
|
|
} catch (error) {
|
|
// Keep error logging for debugging
|
|
console.error('Image normalization error:', error)
|
|
throw error
|
|
}
|
|
}
|