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
file://
as prefix I tried to remove them also.Actually I don't know about mozjpeg
but I prefer to use pure JavaScript way on the environment of problem to solve my problem.
I guess your thought fell in one-way bios, leave away the NodeJS solution, you are in the react-native environment so use React Native Compress Image or React Native Image Resizer.
Based on my experiences I prefer to use the second one, the React Native Image Resizer.
After install, use it like below:
ImageResizer.createResizedImage(
imageUri,
newWidth,
newHeight,
compressFormat,
quality
)
.then( resizedImageUri => {
// the resizedImageUri is accessible here to use.
}).catch( err => {
// Catch any error here.
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With