I am using npm install --save tinify
then uploading my image files using nodejs but I need compressed .zip file in my system .
When I'm uploading my image files through browser at https://tinypng.com after uploading it shows download option that is perfect. But how can we do same through nodejs?
Here is my code:
var tinify = require("tinify");
tinify.key = "myRightApiCode";
var fs = require("fs");
fs.readFile("C:/Users/sourav/images/pgL_NA-10005_5.jpg", function(err,
sourceData) {
if (err) throw err;
tinify.fromBuffer(sourceData).toBuffer(function(err, resultData) {
if (err) throw err;
// ...
console.log(resultData);
//need compressed file in my system
});
});
You can use tinify's method that converts and writes the compressed image at once:
var sourceFile = tinify.fromFile("uncompressed.jpg");
sourceFile.toFile("compressed.jpg");
Alternatively in your method try:
var tinify = require("tinify");
tinify.key = "myRightApiCode";
var fs = require("fs");
fs.readFile("C:/Users/sourav/images/pgL_NA-10005_5.jpg", function(err,
sourceData) {
if (err) throw err;
tinify.fromBuffer(sourceData).toBuffer(function(err, resultData) {
if (err) throw err;
fs.writeFile('C:/Users/sourav/images/optimized.jpg', resultData, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
});
Hope this solves your query :)