tinify / tinify-nodejs

Node.js client for the Tinify API.
https://tinypng.com/developers
MIT License
414 stars 74 forks source link

How to get size of tinified images #26

Open theahmadzai opened 3 years ago

theahmadzai commented 3 years ago

How to get metadata of images, size before and after tinification are there any methods available on node.js API, I'm sorry I searched a lot but couldn't find useful resources. If anyone could help?

mattijsvandruenen commented 3 years ago

@theahmadzai the API returns both the input size (before compression) and output size (after compression), but in the nodejs library we currently don't have a function available to get the input size. Feel free to add it and provide a pull request. You can refer to the documentation on https://tinypng.com/developers/reference for the API calls available via HTTP.

We do have a function available in the nodejs library to get the size of the compressed image. A simple example of printing it is

const source = tinify.fromUrl("https://tinypng.com/images/panda-happy.png");
source.result().size(function(err, size) {
  console.log(size);
});

To get the size before compression it might be easiest to use the filesystem module within nodejs.

theahmadzai commented 3 years ago

Thanks, I'm tinkering with the library. I have a question, why is upload from file response object key different from upload from URL i.e upload from file returns

{
  "input": {
    "size": 207565,
    "type": "image/jpeg"
  }
}

while uploading from URL returns

{
  "output": {
    "size": 30734,
    "type": "image/png"
  }
}
mattijsvandruenen commented 3 years ago

@theahmadzai it looks like the documentation on https://tinypng.com/developers/reference isn't complete in regards to the json returned for some API calls. If you give any of those curl commands a try you will see that for both upload from file and url return a response including both input and output, like:

{
  "input": {
    "size": 76606,
    "type": "image/jpeg"
  },
  "output": {
    "size": 41892,
    "type": "image/jpeg",
    "width": 480,
    "height": 600,
    "ratio": 0.5469,
    "url": "https://api.tinify.com/output/..."
  }
}