git ssb

7+

dinoworm 🐛 / patchcore



Commit d801cfacf24b25bc56f7812fe6ba1fd287ef9a56

Add the ability to remove exif data

Anders Rune Jensen committed on 5/29/2018, 11:50:56 PM
Parent: ba3a2674f63fa05229e42bfed91b68333405c1a9

Files changed

blob/html/input.jschanged
package.jsonchanged
blob/html/input.jsView
@@ -8,13 +8,13 @@
88 var Defer = require('pull-defer')
99 var BoxStream = require('pull-box-stream')
1010 var crypto = require('crypto')
1111 var zeros = Buffer.alloc(24, 0)
12 +var piexif = require('piexifjs')
1213
1314 module.exports = {
1415 needs: nest({
1516 'sbot.obs.connection': 'first'
16-
1717 }),
1818 gives: nest('blob.html.input'),
1919 create: function (api) {
2020 return nest('blob.html.input', function FileInput (onAdded, opts = {}) {
@@ -27,12 +27,17 @@
2727
2828 var mimeType = mime(file.name)
2929 var fileName = file.name
3030
31- // handle exif orientation data and resize
32- getOrientation(file, (orientation) => {
31 + getFileData(file, function(fileData) {
32 + var orientation = getOrientation(fileData)
33 +
34 + if (opts.removeExif)
35 + fileData = removeExif(fileData, orientation)
36 +
37 + // handle exif orientation data and resize
3338 if (orientation >= 3 || opts.resize) {
34- getImage(file, (image) => {
39 + getImage(fileData, (image) => {
3540 image = rotate(image, orientation)
3641 if (opts.resize) {
3742 image = resize(image, opts.resize.width, opts.resize.height)
3843 }
@@ -43,17 +48,28 @@
4348 image.toBlob(blob => {
4449 next(blob)
4550 }, mimeType, 0.85)
4651 } else {
47- next(file)
52 + next(dataURItoBlob(fileData))
4853 }
4954 })
5055 } else {
5156 // don't process
52- next(file)
57 + next(dataURItoBlob(fileData))
5358 }
5459 })
5560
61 + function dataURItoBlob(dataURI) {
62 + var byteString = atob(dataURI.split(',')[1]);
63 + var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
64 + var ab = new ArrayBuffer(byteString.length);
65 + var ia = new Uint8Array(ab);
66 + for (var i = 0; i < byteString.length; i++) {
67 + ia[i] = byteString.charCodeAt(i);
68 + }
69 + return new Blob([ab], {type: mimeString});
70 + }
71 +
5672 function next (file) {
5773 var reader = new global.FileReader()
5874 reader.onload = function () {
5975 var stream = pull.values(split(new Buffer(reader.result), 64 * 1024))
@@ -82,9 +98,9 @@
8298
8399 function getImage (file, cb) {
84100 var image = document.createElement('img')
85101 image.onload = () => cb(image)
86- image.src = global.URL.createObjectURL(file)
102 + image.src = file
87103 image.style.display = 'block'
88104 if (image.complete) cb(image)
89105 }
90106
@@ -110,40 +126,34 @@
110126 ctx.drawImage(image, -offsetX, -offsetY, finalWidth, finalHeight)
111127 return canvas
112128 }
113129
114-function getOrientation (file, callback) {
130 +function getFileData(file, cb)
131 +{
115132 var reader = new global.FileReader()
116133 reader.onload = function (e) {
117- var view = new DataView(e.target.result)
118- if (view.getUint16(0, false) !== 0xFFD8) return callback(-2)
119- var length = view.byteLength
120- var offset = 2
121- while (offset < length) {
122- var marker = view.getUint16(offset, false)
123- offset += 2
124- if (marker === 0xFFE1) {
125- if (view.getUint32(offset += 2, false) !== 0x45786966) return callback(-1)
126- var little = view.getUint16(offset += 6, false) === 0x4949
127- offset += view.getUint32(offset + 4, little)
128- var tags = view.getUint16(offset, little)
129- offset += 2
130- for (var i = 0; i < tags; i++) {
131- if (view.getUint16(offset + (i * 12), little) === 0x0112) {
132- return callback(view.getUint16(offset + (i * 12) + 8, little))
133- }
134- }
135- } else if ((marker & 0xFF00) !== 0xFF00) {
136- break
137- } else {
138- offset += view.getUint16(offset, false)
139- }
140- }
141- return callback(-1)
134 + cb(e.target.result)
142135 }
143- reader.readAsArrayBuffer(file)
136 + reader.readAsDataURL(file)
144137 }
145138
139 +function removeExif (fileData, orientation) {
140 + var clean = piexif.remove(fileData)
141 + if (orientation != undefined) { // preserve
142 + var exifData = { "0th": {} }
143 + exifData["0th"][piexif.ImageIFD.Orientation] = orientation
144 + var exifStr = piexif.dump(exifData)
145 + return piexif.insert(exifStr, clean)
146 + }
147 + else
148 + return clean
149 +}
150 +
151 +function getOrientation (fileData) {
152 + var exif = piexif.load(fileData);
153 + return exif["0th"][piexif.ImageIFD.Orientation]
154 +}
155 +
146156 function rotate (img, orientation) {
147157 var canvas = document.createElement('canvas')
148158 var ctx = canvas.getContext('2d')
149159
package.jsonView
@@ -42,8 +42,9 @@
4242 "html-escape": "^2.0.0",
4343 "human-time": "0.0.1",
4444 "mutant": "^3.21.2",
4545 "mutant-pull-reduce": "^1.1.0",
46 + "piexifjs": "^1.0.4",
4647 "pull-abortable": "^4.1.0",
4748 "pull-box-stream": "~1.0.13",
4849 "pull-cat": "^1.1.11",
4950 "pull-defer": "~0.2.2",

Built with git-ssb-web