-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'davejm-feature-remove-exif'
- Loading branch information
Showing
5 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* piexifjs | ||
The MIT License (MIT) | ||
Copyright (c) 2014, 2015 hMatoba(https://github.com/hMatoba) | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
import Utils from "../Utils.js"; | ||
|
||
// Param jpeg should be a binaryArray | ||
function removeEXIF(jpeg) { | ||
// Convert binaryArray to char string | ||
jpeg = Utils.byteArrayToChars(jpeg); | ||
if (jpeg.slice(0, 2) != "\xff\xd8") { | ||
throw ("Given data is not jpeg."); | ||
} | ||
|
||
var segments = splitIntoSegments(jpeg); | ||
if (segments[1].slice(0, 2) == "\xff\xe1" && | ||
segments[1].slice(4, 10) == "Exif\x00\x00") { | ||
segments = [segments[0]].concat(segments.slice(2)); | ||
} else if (segments[2].slice(0, 2) == "\xff\xe1" && | ||
segments[2].slice(4, 10) == "Exif\x00\x00") { | ||
segments = segments.slice(0, 2).concat(segments.slice(3)); | ||
} else { | ||
throw ("Exif not found."); | ||
} | ||
|
||
var new_data = segments.join(""); | ||
|
||
// Convert back to binaryArray | ||
new_data = Utils.strToCharcode(new_data); | ||
|
||
return new_data; | ||
}; | ||
|
||
function splitIntoSegments(data) { | ||
if (data.slice(0, 2) != "\xff\xd8") { | ||
throw ("Given data isn't JPEG."); | ||
} | ||
|
||
var head = 2; | ||
var segments = ["\xff\xd8"]; | ||
while (true) { | ||
if (data.slice(head, head + 2) == "\xff\xda") { | ||
segments.push(data.slice(head)); | ||
break; | ||
} else { | ||
var length = unpack(">H", data.slice(head + 2, head + 4))[0]; | ||
var endPoint = head + length + 2; | ||
segments.push(data.slice(head, endPoint)); | ||
head = endPoint; | ||
} | ||
|
||
if (head >= data.length) { | ||
throw ("Wrong JPEG data."); | ||
} | ||
} | ||
return segments; | ||
} | ||
|
||
function unpack(mark, str) { | ||
if (typeof(str) != "string") { | ||
throw ("'unpack' error. Got invalid type argument."); | ||
} | ||
var l = 0; | ||
for (var markPointer = 1; markPointer < mark.length; markPointer++) { | ||
if (mark[markPointer].toLowerCase() == "b") { | ||
l += 1; | ||
} else if (mark[markPointer].toLowerCase() == "h") { | ||
l += 2; | ||
} else if (mark[markPointer].toLowerCase() == "l") { | ||
l += 4; | ||
} else { | ||
throw ("'unpack' error. Got invalid mark."); | ||
} | ||
} | ||
|
||
if (l != str.length) { | ||
throw ("'unpack' error. Mismatch between symbol and string length. " + l + ":" + str.length); | ||
} | ||
|
||
var littleEndian; | ||
if (mark[0] == "<") { | ||
littleEndian = true; | ||
} else if (mark[0] == ">") { | ||
littleEndian = false; | ||
} else { | ||
throw ("'unpack' error."); | ||
} | ||
var unpacked = []; | ||
var strPointer = 0; | ||
var p = 1; | ||
var val = null; | ||
var c = null; | ||
var length = null; | ||
var sliced = ""; | ||
|
||
while (c = mark[p]) { | ||
if (c.toLowerCase() == "b") { | ||
length = 1; | ||
sliced = str.slice(strPointer, strPointer + length); | ||
val = sliced.charCodeAt(0); | ||
if ((c == "b") && (val >= 0x80)) { | ||
val -= 0x100; | ||
} | ||
} else if (c == "H") { | ||
length = 2; | ||
sliced = str.slice(strPointer, strPointer + length); | ||
if (littleEndian) { | ||
sliced = sliced.split("").reverse().join(""); | ||
} | ||
val = sliced.charCodeAt(0) * 0x100 + | ||
sliced.charCodeAt(1); | ||
} else if (c.toLowerCase() == "l") { | ||
length = 4; | ||
sliced = str.slice(strPointer, strPointer + length); | ||
if (littleEndian) { | ||
sliced = sliced.split("").reverse().join(""); | ||
} | ||
val = sliced.charCodeAt(0) * 0x1000000 + | ||
sliced.charCodeAt(1) * 0x10000 + | ||
sliced.charCodeAt(2) * 0x100 + | ||
sliced.charCodeAt(3); | ||
if ((c == "l") && (val >= 0x80000000)) { | ||
val -= 0x100000000; | ||
} | ||
} else { | ||
throw ("'unpack' error. " + c); | ||
} | ||
|
||
unpacked.push(val); | ||
strPointer += length; | ||
p += 1; | ||
} | ||
|
||
return unpacked; | ||
} | ||
|
||
export default removeEXIF; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import * as ExifParser from "exif-parser"; | ||
import removeEXIF from "../lib/remove-exif.js"; | ||
import Utils from "../Utils.js"; | ||
import FileType from "./FileType.js"; | ||
|
||
|
@@ -23,7 +24,7 @@ const Image = { | |
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
runEXIF(input, args) { | ||
runExtractEXIF(input, args) { | ||
try { | ||
const bytes = Uint8Array.from(input); | ||
const parser = ExifParser.create(bytes.buffer); | ||
|
@@ -44,6 +45,30 @@ const Image = { | |
}, | ||
|
||
|
||
/** | ||
* Remove EXIF operation. | ||
* | ||
* Removes EXIF data from a byteArray, representing a JPG. | ||
* | ||
* @author David Moodie [[email protected]] | ||
* @param {byteArray} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
runRemoveEXIF(input, args) { | ||
// Do nothing if input is empty | ||
if (input.length === 0) return input; | ||
|
||
try { | ||
return removeEXIF(input); | ||
} catch (err) { | ||
// Simply return input if no EXIF data is found | ||
if (err === "Exif not found.") return input; | ||
throw "Could not remove EXIF data from image: " + err; | ||
} | ||
}, | ||
|
||
|
||
/** | ||
* @constant | ||
* @default | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.