I have a social networking PWA in flutter where I allow users to upload images and I am storing those images on Firebase storage. Now the issue is, currently, I am not able to find any proper compression method within Flutter. Hence, I decided to use JS for it, but unfortunately, that JS is returning the compressed file as null.
function compressAndDownloadImage(base64) {
var url = base64;
fetch(url)
.then(res => res.blob())
.then(blob => {
var imageFile = blob;
console.log('originalFile instanceof Blob', imageFile instanceof Blob);
console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`);
var options = {
maxSizeMB: 0.2,
maxWidthOrHeight: 1920,
useWebWorker: true
}
imageCompression(imageFile, options)
.then(function (compressedFile) {
console.log('compressedFile instanceof Blob', compressedFile instanceof Blob);
console.log(`compressedFile size ${compressedFile.size / 1024 / 1024} MB`);
console.log(compressedFile);
saveAs(new Blob([compressedFile], { type: "image/jpeg" }), Math.floor(Date.now() / 1000) + '.jpeg');
return compressedFile;
})
.catch(function (error) {
console.log(error.message);
});
})
}
Please let me know what's wrong with the JS script that it is returning null.
Thanks in advance.