Skip to content

Commit 1e4df70

Browse files
committed
Support automatic browser image orientation.
Resolves blueimp#97 See also: w3c/csswg-drafts#4666
1 parent c395458 commit 1e4df70

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ The optional third argument to **loadImage()** is a map of options:
228228
When set to `true`, it will set the orientation value based on the EXIF data
229229
of the image, which will be parsed automatically if the exif library is
230230
available.
231-
Setting the `orientation` also enables the `canvas` option.
232-
Setting `orientation` to `true` also enables the `meta` option.
231+
Setting `orientation` to an integer in the range of `2` to `8` enables the
232+
`canvas` option.
233+
Setting `orientation` to `true` enables the `canvas` and `meta` options,
234+
unless the browser supports automatic image orientation (see
235+
[browser support for image-orientation](https://caniuse.com/#feat=css-image-orientation)).
233236
- **meta**: Automatically parses the image meta data if set to `true`.
234237
The meta data is passed to the callback as part of the second argument.
235238
If the file is given as URL and the browser supports the

js/load-image-orientation.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,37 @@
3434
var originalTransformCoordinates = loadImage.transformCoordinates
3535
var originalGetTransformedOptions = loadImage.getTransformedOptions
3636

37+
;(function () {
38+
// black 2x1 JPEG, with the following meta information set:
39+
// - EXIF Orientation: 6 (Rotated 90° CCW)
40+
var testImageURL =
41+
'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +
42+
'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +
43+
'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +
44+
'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +
45+
'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +
46+
'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='
47+
var img = document.createElement('img')
48+
img.onload = function () {
49+
// Check if browser supports automatic image orientation:
50+
loadImage.orientation = img.width === 1 && img.height === 2
51+
}
52+
img.src = testImageURL
53+
})()
54+
3755
// Determines if the target image should be a canvas element:
3856
loadImage.hasCanvasOption = function (options) {
3957
return (
40-
!!options.orientation || originalHasCanvasOption.call(loadImage, options)
58+
(!!options.orientation === true && !loadImage.orientation) ||
59+
(options.orientation > 1 && options.orientation < 9) ||
60+
originalHasCanvasOption.call(loadImage, options)
4161
)
4262
}
4363

4464
// Determines if meta data should be loaded automatically:
4565
loadImage.hasMetaOption = function (options) {
4666
return (
47-
(options && options.orientation === true) ||
67+
(options && options.orientation === true && !loadImage.orientation) ||
4868
originalHasMetaOption.call(loadImage, options)
4969
)
5070
}
@@ -115,8 +135,12 @@
115135
var orientation = options.orientation
116136
var newOptions
117137
var i
118-
if (orientation === true && data && data.exif) {
119-
orientation = data.exif.get('Orientation')
138+
if (orientation === true) {
139+
if (loadImage.orientation) {
140+
// Browser supports automatic image orientation
141+
return options
142+
}
143+
orientation = data && data.exif && data.exif.get('Orientation')
120144
}
121145
if (!(orientation > 1 && orientation < 9)) {
122146
return options

test/test.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@
877877
expect(imageData[3 + 20]).to.equal(255)
878878
done()
879879
},
880-
{ orientation: true }
880+
{ orientation: true, meta: true, canvas: true }
881881
)
882882
).to.be.ok
883883
})
@@ -895,6 +895,27 @@
895895
)
896896
).to.be.ok
897897
})
898+
899+
describe('from-image', function () {
900+
if (!loadImage.orientation) return
901+
902+
it('Should use automatic browser image orientation', function (done) {
903+
expect(
904+
loadImage(
905+
blobJPEG,
906+
function (img, data) {
907+
expect(data).to.be.ok
908+
expect(data.exif).to.be.undefined
909+
expect(img.getContext).to.be.undefined
910+
expect(img.width).to.equal(2)
911+
expect(img.height).to.equal(3)
912+
done()
913+
},
914+
{ orientation: true }
915+
)
916+
).to.be.ok
917+
})
918+
})
898919
})
899920

900921
describe('Canvas', function () {

0 commit comments

Comments
 (0)