1
1
import UPNG from './UPNG' ;
2
- import MaxCanvasSize from './config/max-canvas-size' ;
2
+ import MAX_CANVAS_SIZE from './config/max-canvas-size' ;
3
+ import BROWSER_NAME from './config/browser-name' ;
3
4
4
5
const isBrowser = typeof window !== 'undefined' ; // change browser environment to support SSR
5
6
@@ -86,21 +87,25 @@ export function loadImage(src) {
86
87
* @returns {string }
87
88
*/
88
89
export function getBrowserName ( ) {
89
- let browserName = 'etc' ;
90
+ if ( getBrowserName . cachedResult !== undefined ) {
91
+ return getBrowserName . cachedResult ;
92
+ }
93
+ let browserName = BROWSER_NAME . ETC ;
90
94
const { userAgent } = navigator ;
91
95
if ( / C h r o m ( e | i u m ) / i. test ( userAgent ) ) {
92
- browserName = 'chrome' ;
96
+ browserName = BROWSER_NAME . CHROME ;
93
97
} else if ( / i P ( a d | o d | h o n e ) / i. test ( userAgent ) && / W e b K i t / i. test ( userAgent ) && ! ( / ( C r i O S | F x i O S | O P i O S | m e r c u r y ) / i. test ( userAgent ) ) ) {
94
98
// see: https://stackoverflow.com/a/35813965
95
- browserName = 'mobile safari' ;
99
+ browserName = BROWSER_NAME . MOBILE_SAFARI ;
96
100
} else if ( / S a f a r i / i. test ( userAgent ) ) {
97
- browserName = 'desktop safari' ;
101
+ browserName = BROWSER_NAME . DESKTOP_SAFARI ;
98
102
} else if ( / F i r e f o x / i. test ( userAgent ) ) {
99
- browserName = 'firefox' ;
103
+ browserName = BROWSER_NAME . FIREFOX ;
100
104
} else if ( / M S I E / i. test ( userAgent ) || ( ! ! document . documentMode ) === true ) { // IF IE > 10
101
- browserName = 'internet explorer' ;
105
+ browserName = BROWSER_NAME . IE ;
102
106
}
103
- return browserName ;
107
+ getBrowserName . cachedResult = browserName ;
108
+ return getBrowserName . cachedResult ;
104
109
}
105
110
106
111
/**
@@ -114,7 +119,7 @@ export function getBrowserName() {
114
119
*/
115
120
export function approximateBelowMaximumCanvasSizeOfBrowser ( initWidth , initHeight ) {
116
121
const browserName = getBrowserName ( ) ;
117
- const maximumCanvasSize = MaxCanvasSize [ browserName ] ;
122
+ const maximumCanvasSize = MAX_CANVAS_SIZE [ browserName ] ;
118
123
119
124
let width = initWidth ;
120
125
let height = initHeight ;
@@ -215,8 +220,8 @@ export function isIOS() {
215
220
export async function drawFileInCanvas ( file , options = { } ) {
216
221
let img ;
217
222
try {
218
- if ( isIOS ( ) ) {
219
- throw new Error ( 'Skip createImageBitmap on IOS device ' ) ; // see https://github.com/Donaldcwl/browser-image-compression/issues/118
223
+ if ( isIOS ( ) || [ BROWSER_NAME . DESKTOP_SAFARI , BROWSER_NAME . MOBILE_SAFARI ] . includes ( getBrowserName ( ) ) ) {
224
+ throw new Error ( 'Skip createImageBitmap on IOS and Safari ' ) ; // see https://github.com/Donaldcwl/browser-image-compression/issues/118
220
225
}
221
226
img = await createImageBitmap ( file ) ;
222
227
} catch ( e ) {
@@ -287,7 +292,7 @@ export function cleanupCanvasMemory(canvas) {
287
292
// Check if browser supports automatic image orientation
288
293
// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53
289
294
export async function isAutoOrientationInBrowser ( ) {
290
- if ( isAutoOrientationInBrowser . result !== undefined ) return isAutoOrientationInBrowser . result ;
295
+ if ( isAutoOrientationInBrowser . cachedResult !== undefined ) return isAutoOrientationInBrowser . cachedResult ;
291
296
292
297
// black 2x1 JPEG, with the following meta information set:
293
298
// EXIF Orientation: 6 (Rotated 90° CCW)
@@ -305,8 +310,8 @@ export async function isAutoOrientationInBrowser() {
305
310
const img = ( await drawFileInCanvas ( testImageFile2 ) ) [ 0 ] ;
306
311
// console.log('img', img.width, img.height)
307
312
308
- isAutoOrientationInBrowser . result = img . width === 1 && img . height === 2 ;
309
- return isAutoOrientationInBrowser . result ;
313
+ isAutoOrientationInBrowser . cachedResult = img . width === 1 && img . height === 2 ;
314
+ return isAutoOrientationInBrowser . cachedResult ;
310
315
}
311
316
312
317
/**
0 commit comments