PHP class for basic interaction with the TinyPNG API.
Latest commit bcfbfa7 Dec 19, 2013 @michaelw90 Merge pull request #4 from rolftimmermans/public-api-endpoint
Update endpoint and HTTP response code for public API release.


PHP class for basic interaction with the TinyPNG API. TinyPNG is a great site to optimise and reduce the size of your PNG images that you use.


To be able to use this you will require an API key from the TinyPNG team, you can do this by getting in touch with them through their Twitter or email.


As TinyPNG is funded by donations alone it is worth considering the usage that you force upon their API before excessively using it.

Getting Started

There are 2 methods that you need to use, once you have created a new instance, begin by invoking a new instance of the class with your API key as the only parameter:

$api = new TinyPNG('API_Key_Here');

Next we need to send the request to shrink an image using the shrink method, which takes a string path to the file:


This method simply returns true or false depending on whether the request succeeded. You can then get the response of the shrink request through a subsequent call to getResult().

Get the result as object using the following method

$result = $api->getResult();

To get the result object as JSON call

$resultJson = $api->getResultJson();


In the wild

This API is used in a few other projects around the internet:

  • TinyPNGCache - cache and serve your PNG images on the fly in a reduced and optimised form