git ssb

7+

dinoworm 🐛 / patchcore



Commit f147a2ac73f68764ed6b87c1a090b4418470d6e3

blob.html.input: auto orient images images based on exif data

Matt McKegg committed on 2/19/2017, 6:12:54 AM
Parent: f6852d8a65ff7eebb26e615d8b3da4bbb9f45cdd

Files changed

blob/html/input.jschanged
blob/html/input.jsView
@@ -17,26 +17,47 @@
1717 'ev-change': function (ev) {
1818 var file = ev.target.files[0]
1919 if (!file) return
2020
21- if (opts.resize) {
22- resize(file, opts.resize, next)
23- } else {
24- next(null, file)
25- }
21 + var mimeType = mime(file.name)
22 + var fileName = file.name
2623
27- function next (err, file) {
28- if (err) return console.error(err)
24 + // handle exif orientation data and resize
25 + getOrientation(file, (orientation) => {
26 + if (orientation >= 3 || opts.resize) {
27 + getImage(file, (image) => {
28 + image = rotate(image, orientation)
29 + if (opts.resize) {
30 + image = resize(image, opts.resize.width, opts.resize.height)
31 + }
32 + if (image.toBlob) {
33 + if (mimeType !== 'image/jpeg' || mimeType !== 'image/png') {
34 + mimeType = 'image/jpeg'
35 + }
36 + image.toBlob(blob => {
37 + next(blob)
38 + }, mimeType, 0.85)
39 + } else {
40 + next(file)
41 + }
42 + })
43 + } else {
44 + // don't process
45 + next(file)
46 + }
47 + })
48 +
49 + function next (file) {
2950 var reader = new global.FileReader()
3051 reader.onload = function () {
3152 var stream = pull.values(split(new Buffer(reader.result), 64 * 1024))
3253 api.sbot.async.addBlob(stream, function (err, blob) {
3354 if (err) return console.error(err)
3455 onAdded({
3556 link: blob,
36- name: file.name,
57 + name: fileName,
3758 size: reader.result.length || reader.result.byteLength,
38- type: mime(file.name)
59 + type: mimeType
3960 })
4061 })
4162 }
4263 reader.readAsArrayBuffer(file)
@@ -46,39 +67,92 @@
4667 })
4768 }
4869 }
4970
50-function resize (file, opts, cb) {
71 +function getImage (file, cb) {
5172 var image = document.createElement('img')
52-
53- image.onload = next
73 + image.onload = () => cb(image)
5474 image.src = global.URL.createObjectURL(file)
5575 image.style.display = 'block'
76 + if (image.complete) cb(image)
77 +}
5678
57- if (image.complete) next()
79 +function resize (image, width, height) {
80 + var imageHeight = image.height
81 + var imageWidth = image.width
5882
59- function next () {
60- var imageHeight = image.height
61- var imageWidth = image.width
83 + var multiplier = (height / image.height)
84 + if (multiplier * imageWidth < width) {
85 + multiplier = width / image.width
86 + }
6287
63- var multiplier = (opts.height / image.height)
64- if (multiplier * imageWidth < opts.width) {
65- multiplier = opts.width / image.width
66- }
88 + var finalWidth = imageWidth * multiplier
89 + var finalHeight = imageHeight * multiplier
6790
68- var finalWidth = imageWidth * multiplier
69- var finalHeight = imageHeight * multiplier
91 + var offsetX = (finalWidth - width) / 2
92 + var offsetY = (finalHeight - height) / 2
7093
71- var offsetX = (finalWidth - opts.width) / 2
72- var offsetY = (finalHeight - opts.height) / 2
94 + var canvas = document.createElement('canvas')
95 + canvas.width = width
96 + canvas.height = height
97 + var ctx = canvas.getContext('2d')
98 + ctx.drawImage(image, -offsetX, -offsetY, finalWidth, finalHeight)
99 + return canvas
100 +}
73101
74- var canvas = document.createElement('canvas')
75- canvas.width = opts.width
76- canvas.height = opts.height
77- var ctx = canvas.getContext('2d')
78- ctx.drawImage(image, -offsetX, -offsetY, finalWidth, finalHeight)
79- canvas.toBlob(blob => {
80- blob.name = file.name
81- cb(null, blob)
82- }, 'image/' + opts.type, 0.85)
102 +function getOrientation (file, callback) {
103 + var reader = new global.FileReader()
104 + reader.onload = function (e) {
105 + var view = new DataView(e.target.result)
106 + if (view.getUint16(0, false) !== 0xFFD8) return callback(-2)
107 + var length = view.byteLength
108 + var offset = 2
109 + while (offset < length) {
110 + var marker = view.getUint16(offset, false)
111 + offset += 2
112 + if (marker === 0xFFE1) {
113 + if (view.getUint32(offset += 2, false) !== 0x45786966) return callback(-1)
114 + var little = view.getUint16(offset += 6, false) === 0x4949
115 + offset += view.getUint32(offset + 4, little)
116 + var tags = view.getUint16(offset, little)
117 + offset += 2
118 + for (var i = 0; i < tags; i++) {
119 + if (view.getUint16(offset + (i * 12), little) === 0x0112) {
120 + return callback(view.getUint16(offset + (i * 12) + 8, little))
121 + }
122 + }
123 + } else if ((marker & 0xFF00) !== 0xFF00) {
124 + break
125 + } else {
126 + offset += view.getUint16(offset, false)
127 + }
128 + }
129 + return callback(-1)
83130 }
131 + reader.readAsArrayBuffer(file)
84132 }
133 +
134 +function rotate (img, orientation) {
135 + var canvas = document.createElement('canvas')
136 + var ctx = canvas.getContext('2d')
137 +
138 + if (orientation === 6 || orientation === 8) {
139 + canvas.width = img.height
140 + canvas.height = img.width
141 + ctx.translate(img.height / 2, img.width / 2)
142 + if (orientation === 6) {
143 + ctx.rotate(0.5 * Math.PI)
144 + } else {
145 + ctx.rotate(1.5 * Math.PI)
146 + }
147 + } else if (orientation === 3) {
148 + canvas.width = img.width
149 + canvas.height = img.height
150 + ctx.rotate(1 * Math.PI)
151 + ctx.translate(img.width / 2, img.height / 2)
152 + } else {
153 + return img
154 + }
155 +
156 + ctx.drawImage(img, -img.width / 2, -img.height / 2)
157 + return canvas
158 +}

Built with git-ssb-web