Skip to content

Commit 576c3bb

Browse files
committed
fix: wrong image orientation on Safari
1 parent 585b4dc commit 576c3bb

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/utils.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
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';
34

45
const isBrowser = typeof window !== 'undefined'; // change browser environment to support SSR
56

@@ -86,21 +87,25 @@ export function loadImage(src) {
8687
* @returns {string}
8788
*/
8889
export function getBrowserName() {
89-
let browserName = 'etc';
90+
if (getBrowserName.cachedResult !== undefined) {
91+
return getBrowserName.cachedResult;
92+
}
93+
let browserName = BROWSER_NAME.ETC;
9094
const { userAgent } = navigator;
9195
if (/Chrom(e|ium)/i.test(userAgent)) {
92-
browserName = 'chrome';
96+
browserName = BROWSER_NAME.CHROME;
9397
} else if (/iP(ad|od|hone)/i.test(userAgent) && /WebKit/i.test(userAgent) && !(/(CriOS|FxiOS|OPiOS|mercury)/i.test(userAgent))) {
9498
// see: https://stackoverflow.com/a/35813965
95-
browserName = 'mobile safari';
99+
browserName = BROWSER_NAME.MOBILE_SAFARI;
96100
} else if (/Safari/i.test(userAgent)) {
97-
browserName = 'desktop safari';
101+
browserName = BROWSER_NAME.DESKTOP_SAFARI;
98102
} else if (/Firefox/i.test(userAgent)) {
99-
browserName = 'firefox';
103+
browserName = BROWSER_NAME.FIREFOX;
100104
} else if (/MSIE/i.test(userAgent) || (!!document.documentMode) === true) { // IF IE > 10
101-
browserName = 'internet explorer';
105+
browserName = BROWSER_NAME.IE;
102106
}
103-
return browserName;
107+
getBrowserName.cachedResult = browserName;
108+
return getBrowserName.cachedResult;
104109
}
105110

106111
/**
@@ -114,7 +119,7 @@ export function getBrowserName() {
114119
*/
115120
export function approximateBelowMaximumCanvasSizeOfBrowser(initWidth, initHeight) {
116121
const browserName = getBrowserName();
117-
const maximumCanvasSize = MaxCanvasSize[browserName];
122+
const maximumCanvasSize = MAX_CANVAS_SIZE[browserName];
118123

119124
let width = initWidth;
120125
let height = initHeight;
@@ -215,8 +220,8 @@ export function isIOS() {
215220
export async function drawFileInCanvas(file, options = {}) {
216221
let img;
217222
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
220225
}
221226
img = await createImageBitmap(file);
222227
} catch (e) {
@@ -287,7 +292,7 @@ export function cleanupCanvasMemory(canvas) {
287292
// Check if browser supports automatic image orientation
288293
// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53
289294
export async function isAutoOrientationInBrowser() {
290-
if (isAutoOrientationInBrowser.result !== undefined) return isAutoOrientationInBrowser.result;
295+
if (isAutoOrientationInBrowser.cachedResult !== undefined) return isAutoOrientationInBrowser.cachedResult;
291296

292297
// black 2x1 JPEG, with the following meta information set:
293298
// EXIF Orientation: 6 (Rotated 90° CCW)
@@ -305,8 +310,8 @@ export async function isAutoOrientationInBrowser() {
305310
const img = (await drawFileInCanvas(testImageFile2))[0];
306311
// console.log('img', img.width, img.height)
307312

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;
310315
}
311316

312317
/**

0 commit comments

Comments
 (0)