I am trying to compress image with mozjpeg, when i implemented it in node.js according to the docs it worked fine.
const input = fs.readFileSync("in.ppm");
const out = mozjpeg.encode(input, { quality: 85 });
I need to do the compression in client side, so i tried to do the same with react-native, since RN doesn't contain core node modules such as fs, i need to go for a third party library react-native-fs for file reading.
When i tried to execute mozjpeg.encode(input, { quality: 85 });
in react-native it throws Unrecognized input file format --- perhaps you need -targa
server side implementation
const mozjpeg = require("mozjpeg-js");
const fs = require("fs");
const input = fs.readFileSync(filePath);
const out = mozjpeg.encode(input, { quality: 85 });
console.error(out.stderr);
fs.writeFileSync("out.jpg", out.data);
client side implementation
fs.readFile(image.path).then(data => {
const out = mozjpeg.encode(data, { quality: 85 });
console.log(out);
}
Here are the list of thing i tried
- Tried giving input in hex, buffer, base64 and plain URL string.
- Since android URL contains file:// as prefix i tried to remove them also.