6
6
getExifOrientation ,
7
7
getNewCanvasAndCtx ,
8
8
handleMaxWidthOrHeight ,
9
- isAutoOrientationInBrowser
10
- } from './utils'
9
+ isAutoOrientationInBrowser ,
10
+ } from './utils' ;
11
11
12
12
/**
13
13
* Compress an image file.
@@ -25,93 +25,95 @@ import {
25
25
* @param {number } previousProgress - for internal try catch rerunning start from previous progress
26
26
* @returns {Promise<File | Blob> }
27
27
*/
28
- export default async function compress ( file , options , previousProgress = 0 ) {
29
- let progress = previousProgress
28
+ export default async function compress ( file , options , previousProgress = 0 ) {
29
+ let progress = previousProgress ;
30
30
31
- function incProgress ( inc = 5 ) {
32
- progress += inc
33
- options . onProgress ( Math . min ( progress , 100 ) )
31
+ function incProgress ( inc = 5 ) {
32
+ progress += inc ;
33
+ options . onProgress ( Math . min ( progress , 100 ) ) ;
34
34
}
35
35
36
- function setProgress ( p ) {
37
- progress = Math . min ( Math . max ( p , progress ) , 100 )
38
- options . onProgress ( progress )
36
+ function setProgress ( p ) {
37
+ progress = Math . min ( Math . max ( p , progress ) , 100 ) ;
38
+ options . onProgress ( progress ) ;
39
39
}
40
40
41
- let remainingTrials = options . maxIteration || 10
41
+ let remainingTrials = options . maxIteration || 10 ;
42
42
43
- const maxSizeByte = options . maxSizeMB * 1024 * 1024
43
+ const maxSizeByte = options . maxSizeMB * 1024 * 1024 ;
44
44
45
- incProgress ( )
45
+ incProgress ( ) ;
46
46
47
47
// drawFileInCanvas
48
- let [ img , origCanvas ] = await drawFileInCanvas ( file )
48
+ const [ , origCanvas ] = await drawFileInCanvas ( file ) ;
49
49
50
- incProgress ( )
50
+ incProgress ( ) ;
51
51
52
52
// handleMaxWidthOrHeight
53
- const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight ( origCanvas , options )
53
+ const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight ( origCanvas , options ) ;
54
54
55
- incProgress ( )
55
+ incProgress ( ) ;
56
56
57
57
// exifOrientation
58
- const exifOrientation = options . exifOrientation || await getExifOrientation ( file )
59
- incProgress ( )
60
- const orientationFixedCanvas = ( await isAutoOrientationInBrowser ( ) ) ? maxWidthOrHeightFixedCanvas : followExifOrientation ( maxWidthOrHeightFixedCanvas , exifOrientation )
61
- incProgress ( )
58
+ const exifOrientation = options . exifOrientation || await getExifOrientation ( file ) ;
59
+ incProgress ( ) ;
60
+ const orientationFixedCanvas = ( await isAutoOrientationInBrowser ( ) ) ? maxWidthOrHeightFixedCanvas : followExifOrientation ( maxWidthOrHeightFixedCanvas , exifOrientation ) ;
61
+ incProgress ( ) ;
62
62
63
- let quality = options . initialQuality || 1.0
63
+ let quality = options . initialQuality || 1.0 ;
64
64
65
- const outputFileType = options . fileType || file . type
65
+ const outputFileType = options . fileType || file . type ;
66
66
67
- let tempFile = await canvasToFile ( orientationFixedCanvas , outputFileType , file . name , file . lastModified , quality )
68
- incProgress ( )
67
+ const tempFile = await canvasToFile ( orientationFixedCanvas , outputFileType , file . name , file . lastModified , quality ) ;
68
+ incProgress ( ) ;
69
69
70
- const origExceedMaxSize = tempFile . size > maxSizeByte
71
- const sizeBecomeLarger = tempFile . size > file . size
70
+ const origExceedMaxSize = tempFile . size > maxSizeByte ;
71
+ const sizeBecomeLarger = tempFile . size > file . size ;
72
72
// console.log('original file size', file.size)
73
73
// console.log('current file size', tempFile.size)
74
74
75
75
// check if we need to compress or resize
76
76
if ( ! origExceedMaxSize && ! sizeBecomeLarger ) {
77
77
// no need to compress
78
- setProgress ( 100 )
79
- return tempFile
78
+ setProgress ( 100 ) ;
79
+ return tempFile ;
80
80
}
81
81
82
- const sourceSize = file . size
83
- const renderedSize = tempFile . size
84
- let currentSize = renderedSize
85
- let compressedFile
86
- let newCanvas , ctx
87
- let canvas = orientationFixedCanvas
82
+ const sourceSize = file . size ;
83
+ const renderedSize = tempFile . size ;
84
+ let currentSize = renderedSize ;
85
+ let compressedFile ;
86
+ let newCanvas ; let
87
+ ctx ;
88
+ let canvas = orientationFixedCanvas ;
88
89
while ( remainingTrials -- && ( currentSize > maxSizeByte || currentSize > sourceSize ) ) {
89
- const newWidth = origExceedMaxSize ? canvas . width * 0.95 : canvas . width
90
+ const newWidth = origExceedMaxSize ? canvas . width * 0.95 : canvas . width ;
90
91
const newHeight = origExceedMaxSize ? canvas . height * 0.95 : canvas . height ;
91
92
// console.log('current width', newWidth);
92
93
// console.log('current height', newHeight);
93
- [ newCanvas , ctx ] = getNewCanvasAndCtx ( newWidth , newHeight )
94
+ [ newCanvas , ctx ] = getNewCanvasAndCtx ( newWidth , newHeight ) ;
94
95
95
- ctx . drawImage ( canvas , 0 , 0 , newWidth , newHeight )
96
+ ctx . drawImage ( canvas , 0 , 0 , newWidth , newHeight ) ;
96
97
97
- quality *= 0.95
98
- compressedFile = await canvasToFile ( newCanvas , outputFileType , file . name , file . lastModified , quality )
98
+ quality *= 0.95 ;
99
+ // eslint-disable-next-line no-await-in-loop
100
+ compressedFile = await canvasToFile ( newCanvas , outputFileType , file . name , file . lastModified , quality ) ;
99
101
100
- cleanupCanvasMemory ( canvas )
102
+ cleanupCanvasMemory ( canvas ) ;
101
103
102
- canvas = newCanvas
104
+ canvas = newCanvas ;
103
105
104
- currentSize = compressedFile . size
106
+ currentSize = compressedFile . size ;
105
107
// console.log('currentSize', currentSize)
106
- setProgress ( Math . min ( 99 , Math . floor ( ( renderedSize - currentSize ) / ( renderedSize - maxSizeByte ) * 100 ) ) )
108
+ setProgress ( Math . min ( 99 , Math . floor ( ( ( renderedSize - currentSize ) / ( renderedSize - maxSizeByte ) ) * 100 ) ) ) ;
107
109
}
108
110
109
- cleanupCanvasMemory ( canvas )
110
- cleanupCanvasMemory ( newCanvas )
111
- cleanupCanvasMemory ( maxWidthOrHeightFixedCanvas )
112
- cleanupCanvasMemory ( orientationFixedCanvas )
113
- cleanupCanvasMemory ( origCanvas )
111
+ cleanupCanvasMemory ( canvas ) ;
112
+ cleanupCanvasMemory ( newCanvas ) ;
113
+ cleanupCanvasMemory ( maxWidthOrHeightFixedCanvas ) ;
114
+ cleanupCanvasMemory ( orientationFixedCanvas ) ;
115
+ cleanupCanvasMemory ( origCanvas ) ;
114
116
115
- setProgress ( 100 )
116
- return compressedFile
117
+ setProgress ( 100 ) ;
118
+ return compressedFile ;
117
119
}
0 commit comments