Skip to content

Commit 5f737b7

Browse files
committed
Add workaround for Chromium orientation crop bug.
See https://bugs.chromium.org/p/chromium/issues/detail?id=1074354
1 parent a0866d9 commit 5f737b7

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

js/load-image-orientation.js

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,43 @@ Exif orientation values to correctly display the letter F:
6464
var originalTransformCoordinates = loadImage.transformCoordinates
6565
var originalGetTransformedOptions = loadImage.getTransformedOptions
6666

67-
;(function () {
68-
// black 2x1 JPEG, with the following meta information set:
67+
;(function ($) {
68+
// black+white 3x2 JPEG, with the following meta information set:
6969
// - EXIF Orientation: 6 (Rotated 90° CCW)
70+
// Image data layout (B=black, F=white):
71+
// BFF
72+
// BBB
7073
var testImageURL =
7174
'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +
7275
'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +
7376
'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +
74-
'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +
75-
'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +
76-
'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='
77+
'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAIAAwMBEQACEQEDEQH/x' +
78+
'ABRAAEAAAAAAAAAAAAAAAAAAAAKEAEBAQADAQEAAAAAAAAAAAAGBQQDCAkCBwEBAAAAAAA' +
79+
'AAAAAAAAAAAAAABEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8AG8T9NfSMEVMhQ' +
80+
'voP3fFiRZ+MTHDifa/95OFSZU5OzRzxkyejv8ciEfhSceSXGjS8eSdLnZc2HDm4M3BxcXw' +
81+
'H/9k='
7782
var img = document.createElement('img')
7883
img.onload = function () {
79-
// Check if browser supports automatic image orientation:
80-
loadImage.orientation = img.width === 1 && img.height === 2
84+
// Check if the browser supports automatic image orientation:
85+
$.orientation = img.width === 2 && img.height === 3
86+
if ($.orientation) {
87+
var canvas = document.createElement('canvas')
88+
canvas.width = canvas.height = 1
89+
var ctx = canvas.getContext('2d')
90+
ctx.drawImage(img, 1, 1, 1, 1, 0, 0, 1, 1)
91+
// Check if the source image coordinates (sX, sY, sWidth, sHeight) are
92+
// correctly applied to the auto-orientated image, which should result
93+
// in a white opaque pixel (e.g. in Safari).
94+
// Browsers that show a transparent pixel (e.g. Chromium) fail to crop
95+
// auto-oriented images correctly and require a workaround, e.g.
96+
// drawing the complete source image to an intermediate canvas first.
97+
// See https://bugs.chromium.org/p/chromium/issues/detail?id=1074354
98+
$.orientationCropBug =
99+
ctx.getImageData(0, 0, 1, 1).data.toString() !== '255,255,255,255'
100+
}
81101
}
82102
img.src = testImageURL
83-
})()
103+
})(loadImage)
84104

85105
/**
86106
* Determines if the image requires orientation.

js/load-image-scale.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,31 @@
230230
destHeight *= pixelRatio
231231
canvas.getContext('2d').scale(pixelRatio, pixelRatio)
232232
}
233+
// Check if workaround for Chromium orientation crop bug is required:
234+
// https://bugs.chromium.org/p/chromium/issues/detail?id=1074354
235+
if (
236+
loadImage.orientationCropBug &&
237+
!img.getContext &&
238+
(sourceX || sourceY || sourceWidth !== width || sourceHeight !== height)
239+
) {
240+
// Write the complete source image to an intermediate canvas first:
241+
tmp = img
242+
// eslint-disable-next-line no-param-reassign
243+
img = document.createElement('canvas')
244+
img.width = width
245+
img.height = height
246+
loadImage.drawImage(
247+
tmp,
248+
img,
249+
0,
250+
0,
251+
width,
252+
height,
253+
width,
254+
height,
255+
options
256+
)
257+
}
233258
downsamplingRatio = options.downsamplingRatio
234259
if (
235260
downsamplingRatio > 0 &&

0 commit comments

Comments
 (0)