Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

131
Views
Am Using tinify in node js. Wow can we download supressed file using nodejs?

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
});
}); 
9 months ago · Santiago Trujillo
1 answers
Answer question

0

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 :)

9 months ago · Santiago Trujillo Report
Answer question
Find remote jobs