I am trying to compress images 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 on the client-side, so I tried to do the same with react-native since react-native 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 is the list of thing I tried
- Tried giving input in hex, buffer, base64 and plain URL string.
- Since the Android URL contains
file://
as prefix I tried to remove them also.
mozjpeg
expects the file to be binary encoded maybe something likeBuffer.from(data, 'utf8')
asreadFile
fromreact-native-fs
isutf8
by default. – Eyecup