forked from juliangruber/fix-orientation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
66 lines (54 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var process = require('process');
var exif = require('exif-component');
var toArray = require('data-uri-to-u8');
var rotate = require('rotate-component');
var resize = require('./lib/resize');
var urlToImage = require('./lib/url-to-image');
var size = {
'image/png': require('png-size'),
'image/jpeg': require('jpeg-size')
};
module.exports = fixOrientation;
function fixOrientation (url, opts, fn) {
if (typeof opts == 'function') {
fn = opts;
opts = {};
}
var buf = toArray(url);
var tags = {};
try { tags = exif(buf.buffer) } catch (err) {}
var toRotate = tags.Orientation
&& typeof tags.Orientation.value == 'number'
&& (tags.Orientation.value == 6
|| tags.Orientation.value == 8);
if (!toRotate) {
process.nextTick(function () {
fn(url, opts.image && urlToImage(url));
});
return;
}
var s = size[buf.type](buf);
var max = Math.max(s.width, s.height);
var half = max / 2;
var dir = { 6: 1, 8: -1 }[tags.Orientation.value];
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = canvas.height = max;
rotate(ctx, { x: half, y: half, degrees: dir * 90 });
urlToImage(url, function (img) {
if (dir == 1) {
ctx.drawImage(img, 0, max - s.height);
} else {
ctx.drawImage(img, max - s.width, 0);
}
rotate(ctx, { x: half, y: half, degrees: -1 * dir * 90 });
resize(canvas, {
width: s.height,
height: s.width
});
var url = buf.type == 'image/png'
? canvas.toDataURL()
: canvas.toDataURL('image/jpeg', 1);
fn(url, opts.image && urlToImage(url));
});
}