Skip to content

Instantly share code, notes, and snippets.

@kaueDM
Created April 23, 2018 16:02
Show Gist options
  • Save kaueDM/5528765cd7ea2a74e3f85cfdd0beacbd to your computer and use it in GitHub Desktop.
Save kaueDM/5528765cd7ea2a74e3f85cfdd0beacbd to your computer and use it in GitHub Desktop.
import Compress from 'compress.js';
export const compress = image => (
new Promise((resolve, reject) => {
const cmp = new Compress()
const byteString = atob(image.split(',')[1]);
const mimeType = image.split(',')[0].split(':')[1].split(';')[0];
let ab = new ArrayBuffer(byteString.length);
let ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i) };
let initialBlob = new Blob([ab], { type: mimeType });
//Tem que passar como array pra essa biblioteca quebrada
cmp.compress([initialBlob], { size: 10, maxWidth: 500, maxHeight: 500, quality: .8, resize: true })
.then(output => {
const outputBase64 = output[0].data;
const ext = output[0].ext;
const avatar = Compress.convertBase64ToFile(outputBase64, ext);
resolve(avatar)
})
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment