Skip to content

Commit

Permalink
perf: regenerate the url only when the orientation was reset
Browse files Browse the repository at this point in the history
Closes #63
  • Loading branch information
fengyuanchen committed Jan 19, 2019
1 parent 2d3a78c commit a0c7b78
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## next

- Regenerate the initial url only when the orientation was reset for better performance (#63).

## 1.0.3 (Dec 18, 2018)

- Convert `TypedArray` to `Array` manually instead of using Babel helpers for better browser compatibility (#60).
Expand Down
21 changes: 15 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,24 @@ export default class Compressor {
this.reader = reader;
reader.onload = ({ target }) => {
const { result } = target;
const data = {
url: result,
};

this.load(checkOrientation ? {
if (checkOrientation) {
// Should reset the orientation value to its default value first
// as some iOS browsers will render image with its orientation
...parseOrientation(resetAndGetOrientation(result)),
url: arrayBufferToDataURL(result, mimeType),
} : {
url: result,
});
const orientation = resetAndGetOrientation(result);

Object.assign(data, parseOrientation(orientation));

// Regenerate the url only when the orientation was reset for better performance.
if (orientation > 1) {
data.url = arrayBufferToDataURL(result, mimeType);
}
}

this.load(data);
};
reader.onabort = () => {
this.fail(new Error('Aborted to read the image with FileReader.'));
Expand Down
1 change: 1 addition & 0 deletions src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function arrayBufferToDataURL(arrayBuffer, mimeType) {
let uint8 = new Uint8Array(arrayBuffer);

while (uint8.length > 0) {
// XXX: Babel's `toConsumableArray` helper will throw error in IE or Safari 9
// eslint-disable-next-line prefer-spread
chunks.push(fromCharCode.apply(null, toArray(uint8.subarray(0, chunkSize))));
uint8 = uint8.subarray(chunkSize);
Expand Down

0 comments on commit a0c7b78

Please sign in to comment.