Skip to content

Commit 12d112d

Browse files
authored
Merge pull request #53 from gambitph/fix/52-bulk-heic
fix: get the supported format properly
2 parents c4a5aae + 26f8019 commit 12d112d

1 file changed

Lines changed: 25 additions & 13 deletions

File tree

src/shared/converters/image-converter.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ const supportedFormats = [
99
{ value: 'png', mimeType: 'image/png' },
1010
]
1111

12+
// Get the supported format object based on a string input (either MIME type or format value)
13+
const getSupportedFormat = ( formatString = '' ) => {
14+
const requestedFormat = typeof formatString === 'string' ? formatString.trim().toLowerCase() : ''
15+
const normalizedFormat = requestedFormat === 'image/jpg'
16+
? 'image/jpeg'
17+
: requestedFormat === 'jpeg'
18+
? 'jpg'
19+
: requestedFormat
20+
return (
21+
normalizedFormat.startsWith( 'image/' )
22+
? supportedFormats.find( f => f.mimeType === normalizedFormat )
23+
: supportedFormats.find( f => f.value === normalizedFormat )
24+
) || supportedFormats[ 0 ]
25+
}
26+
1227
/**
1328
* ImageConverter
1429
* Client-side image converter using Canvas APIs.
@@ -178,7 +193,7 @@ class ImageConverter extends Converter {
178193
// Draw image with cropping to maintain aspect ratio
179194
ctx.drawImage( img, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, width, height )
180195

181-
const format = supportedFormats.find( f => f.value === outputFormat )
196+
const format = getSupportedFormat( outputFormat )
182197
// Only use quality for lossy formats
183198
// const q = ( outputFormat === 'webp' || outputFormat === 'jpg' ) ? quality : undefined
184199

@@ -227,10 +242,6 @@ class ImageConverter extends Converter {
227242
isSmartOptimization = this.options?.isSmartOptimization || false,
228243
} = options
229244

230-
let formatTo = options.format || this.options?.format || ''
231-
formatTo = ( formatTo.startsWith( 'image/' ) ? supportedFormats.find( f => f.mimeType === formatTo )?.value : formatTo ) ||
232-
'webp'
233-
234245
// Not an image; return original file unchanged.
235246
if ( ! file.type || ! file.type.startsWith( 'image/' ) ) {
236247
return {
@@ -240,14 +251,15 @@ class ImageConverter extends Converter {
240251
}
241252
}
242253

243-
const formatInfo = supportedFormats.find( f => f.value === formatTo )
254+
const formatString = options.format || this.options?.format || ''
255+
const format = getSupportedFormat( formatString )
244256

245257
// Check if the browser supports the desired output format
246258
const testCanvas = document.createElement( 'canvas' )
247-
if ( formatInfo && ! testCanvas.toDataURL( formatInfo.mimeType ).startsWith( `data:${ formatInfo.mimeType }` ) ) {
259+
if ( format && ! testCanvas.toDataURL( format.mimeType ).startsWith( `data:${ format.mimeType }` ) ) {
248260
// If not supported, skip conversion and return the original file
249261
// eslint-disable-next-line no-console
250-
console.error( '[Cimo] ' + formatTo + ' is not supported by the browser, please use another modern browser' )
262+
console.error( '[Cimo] ' + format.value + ' is not supported by the browser, please use another modern browser' )
251263
return {
252264
file,
253265
metadata: null,
@@ -297,7 +309,7 @@ class ImageConverter extends Converter {
297309
const start = performance.now()
298310

299311
let convertedBlob
300-
if ( formatTo === 'png' ) {
312+
if ( format.value === 'png' ) {
301313
const { default: imageCompression } = await import( /* webpackChunkName: "browser-image-compression" */ 'browser-image-compression' )
302314
convertedBlob = await imageCompression( fileItem.file, {
303315
useWebWorker: true,
@@ -306,7 +318,7 @@ class ImageConverter extends Converter {
306318
// initialQuality: quality, // Use default
307319
} )
308320
} else {
309-
convertedBlob = await this.convertImage( fileItem, formatTo, {
321+
convertedBlob = await this.convertImage( fileItem, format.value, {
310322
quality,
311323
maxDimension: this.options?.maxDimension || 0,
312324
} )
@@ -324,7 +336,7 @@ class ImageConverter extends Converter {
324336
}
325337

326338
// Get the file extension for the new format
327-
const extension = formatTo === 'jpeg' ? 'jpg' : formatTo
339+
const extension = format.value === 'jpeg' ? 'jpg' : format.value
328340
// Prepend a unique identified to the filename
329341
// const prefix = Math.random().toString( 36 ).substring( 2, 10 )
330342
const newName = file.name.replace( /\.[^/.]+$/, '' ) + '.' + extension
@@ -333,7 +345,7 @@ class ImageConverter extends Converter {
333345
filename: newName,
334346
originalFormat: file.type,
335347
originalFilesize: file.size,
336-
convertedFormat: formatInfo.mimeType,
348+
convertedFormat: format.mimeType,
337349
convertedFilesize: convertedBlob.size,
338350
conversionTime: end - start,
339351
compressionSavings: file.size > 0 ? convertedBlob.size / file.size : null,
@@ -343,7 +355,7 @@ class ImageConverter extends Converter {
343355
// This ensures cross-context compatibility (iframe vs main window)
344356
const fileConstructor = file.constructor
345357
const outFile = new fileConstructor( [ convertedBlob ], newName, {
346-
type: formatInfo.mimeType,
358+
type: format.mimeType,
347359
lastModified: Date.now(),
348360
} )
349361

0 commit comments

Comments
 (0)