tinify-nodejs icon indicating copy to clipboard operation
tinify-nodejs copied to clipboard

How to get size of tinified images

Open theahmadzai opened this issue 5 years ago • 3 comments

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?

theahmadzai avatar Aug 24 '20 00:08 theahmadzai

@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.

mattijsvandruenen avatar Aug 26 '20 09:08 mattijsvandruenen

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"
  }
}

theahmadzai avatar Aug 27 '20 22:08 theahmadzai

@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/..."
  }
}

mattijsvandruenen avatar Aug 28 '20 08:08 mattijsvandruenen