-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed issue #34: Increased upload image size, added image compression…
… and functionality to remove image metadata (#79)
- Loading branch information
1 parent
ee5c092
commit 396a2ca
Showing
5 changed files
with
192 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,39 @@ | ||
import Compressor from 'compressorjs'; | ||
|
||
const Compress = (images, state, handleSetState) => { | ||
let compressed = []; | ||
|
||
for (let index = 0; index < images.length; index += 1) { | ||
let image = images[index]; | ||
|
||
if (image && image.type.split('/')[1] !== 'gif') { | ||
new Compressor(image, { | ||
quality: 0.6, | ||
convertSize: 100000, | ||
success: result => { | ||
compressed.push(result); | ||
shouldSetImages(compressed, images, state, handleSetState); | ||
}, | ||
error: error => { | ||
console.warn(error.message); | ||
compressed.push(image); | ||
shouldSetImages(compressed, images, state, handleSetState); | ||
}, | ||
}); | ||
} else { | ||
compressed.push(image); | ||
shouldSetImages(compressed, images, state, handleSetState); | ||
} | ||
} | ||
}; | ||
|
||
const shouldSetImages = (compressed, images, state, handleSetState) => { | ||
if (compressed.length === images.length) { | ||
const { image_upload } = state; | ||
image_upload.images_to_upload = compressed; | ||
|
||
handleSetState(image_upload); | ||
} | ||
}; | ||
|
||
export default Compress; |
62 changes: 62 additions & 0 deletions
62
zubhub_frontend/zubhub/src/assets/js/removeMetaDataWorker.js
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,62 @@ | ||
export const removeMetaData = imageArr => { | ||
let newImageArr = []; | ||
|
||
for (let index = 0; index < imageArr.length; index++) { | ||
let fr = new FileReader(); | ||
fr.onload = process; | ||
fr.mainFile = imageArr[index]; | ||
fr.readAsArrayBuffer(imageArr[index]); | ||
} | ||
|
||
function process() { | ||
let dv = new DataView(this.result); | ||
let offset = 0, | ||
recess = 0; | ||
let pieces = []; | ||
let i = 0; | ||
if (dv.getUint16(offset) === 0xffd8) { | ||
offset += 2; | ||
let app1 = dv.getUint16(offset); | ||
offset += 2; | ||
while (offset < dv.byteLength) { | ||
if (app1 === 0xffe1) { | ||
pieces[i] = { recess: recess, offset: offset - 2 }; | ||
recess = offset + dv.getUint16(offset); | ||
i++; | ||
} else if (app1 === 0xffda) { | ||
break; | ||
} | ||
offset += dv.getUint16(offset); | ||
app1 = dv.getUint16(offset); | ||
offset += 2; | ||
} | ||
|
||
if (pieces.length > 0) { | ||
let newPieces = []; | ||
pieces.forEach(function (v) { | ||
newPieces.push(this.result.slice(v.recess, v.offset)); | ||
}, this); | ||
newPieces.push(this.result.slice(recess)); | ||
newImageArr.push( | ||
new Blob(newPieces, { type: imageArr[newImageArr.length].type }), | ||
); | ||
|
||
if (newImageArr.length === imageArr.length) { | ||
postMessage(newImageArr); | ||
} | ||
} else { | ||
newImageArr.push(this.mainFile); | ||
|
||
if (newImageArr.length === imageArr.length) { | ||
postMessage(newImageArr); | ||
} | ||
} | ||
} else { | ||
newImageArr.push(this.mainFile); | ||
|
||
if (newImageArr.length === imageArr.length) { | ||
postMessage(newImageArr); | ||
} | ||
} | ||
} | ||
}; |
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