Skip to content

Commit 492071d

Browse files
committed
add behavior detection to resolve #52
1 parent 12e951c commit 492071d

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/image-compression.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
followExifOrientation,
66
getExifOrientation,
77
getNewCanvasAndCtx,
8-
handleMaxWidthOrHeight
8+
handleMaxWidthOrHeight,
9+
isAutoOrientationInBrowser
910
} from './utils'
1011

1112
/**
@@ -53,7 +54,7 @@ export default async function compress (file, options) {
5354
// exifOrientation
5455
options.exifOrientation = options.exifOrientation || await getExifOrientation(file)
5556
incProgress()
56-
const orientationFixedCanvas = followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)
57+
const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)
5758
incProgress()
5859

5960
let quality = 1

lib/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,30 @@ const moduleMapper = isBrowser && window.cordova && window.cordova.require && wi
55
export const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)
66
export const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)
77

8+
// Check if browser supports automatic image orientation
9+
// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53
10+
export const isAutoOrientationInBrowser = (async () => {
11+
// black 2x1 JPEG, with the following meta information set:
12+
// EXIF Orientation: 6 (Rotated 90° CCW)
13+
const testImageURL =
14+
'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +
15+
'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +
16+
'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +
17+
'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +
18+
'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +
19+
'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='
20+
const testImageFile = await getFilefromDataUrl(testImageURL, 'tmp.jpg', Date.now())
21+
22+
const testImageCanvas = (await drawFileInCanvas(testImageFile))[1]
23+
const testImageFile2 = await canvasToFile(testImageCanvas, 'image/jpeg', 'test.jpg', Date.now())
24+
cleanupCanvasMemory(testImageCanvas)
25+
const testImageURL2 = await getDataUrlFromFile(testImageFile2)
26+
const img = await loadImage(testImageURL2)
27+
// console.log('img', img.width, img.height)
28+
29+
return img.width === 1 && img.height === 2
30+
})()
31+
832
/**
933
* getDataUrlFromFile
1034
*

0 commit comments

Comments
 (0)