API Reference

The Tinify API allows you to compress and optimize JPEG and PNG images. It is designed as a REST service. We maintain client libraries in various languages that make it very easy to interact with the Tinify API.


Using Composer

You can use the PHP client by installing the Composer package and adding it to your application’s dependencies:

composer require tinify/tinify

Make sure to require Composer’s autoloader:


Using manual installation

If you do not like to use Composer you can also manually install the PHP client. Download the Tinify archive from Github, extract the files to a folder of your choice and include them in your project as shown below.


The source code is available on Github.


To use the API you must provide your API key. You can get an API key by registering with your name and email address. Always keep your API key secret!


All requests will be made over an encrypted HTTPS connection.

You can instruct the API client to make all requests over an HTTP proxy. Set the URL of your proxy server, which can optionally include credentials.


Compressing images

You can upload any JPEG or PNG image to the Tinify API to compress it. We will automatically detect the type of image and optimise with the TinyPNG or TinyJPG engine accordingly. Compression will start as soon as you upload a file or provide the URL to the image.

You can choose a local file as the source and write it to another file.

$source = \Tinify\fromFile("unoptimized.jpg");

You can also upload an image from a buffer (a string with binary) and get the compressed image data.

$sourceData = file_get_contents("unoptimized.jpg");
$resultData = \Tinify\fromBuffer($sourceData)->toBuffer();

You can provide a URL to your image instead of having to upload it.

$source = \Tinify\fromUrl("https://tinypng.com/images/panda-happy.png");

Resizing images

Use the API to create resized versions of your uploaded images. By letting the API handle resizing you avoid having to write such code yourself and you will only have to upload your image once. The resized images will be optimally compressed with a nice and crisp appearance.

You can also take advantage of intelligent cropping to create thumbnails that focus on the most visually important areas of your image.

Resizing counts as one additional compression. For example, if you upload a single image and retrieve the optimized version plus 2 resized versions this will count as 3 compressions in total.

To resize an image, call the resize method on an image source:

$source = \Tinify\fromFile("large.jpg");
$resized = $source->resize(array(
    "method" => "fit",
    "width" => 150,
    "height" => 100

The method describes the way your image will be resized. The following methods are available:

Scales the image down proportionally. You must provide either a target width or a target height, but not both. The scaled image will have exactly the provided width or height.
Scales the image down proportionally so that it fits within the given dimensions. You must provide both a width and a height. The scaled image will not exceed either of these dimensions.
Scales the image proportionally and crops it if necessary so that the result has exactly the given dimensions. You must provide both a width and a height. Which parts of the image are cropped away is determined automatically. An intelligent algorithm determines the most important areas and leaves these intact. We’d love to have feedback!
A more advanced implemetation of cover that detects images with solid backgrounds. Your image is scaled down so that the result has exactly the given dimensions. You must provide both a width and a height. If an image is detected on a plain background it will either scale down the image and crop unimportant parts or add extra background intelligently.

If the target dimensions are larger than the original dimensions, the image will not be scaled up. Scaling up is prevented in order to protect the quality of your images.

Preserving metadata

You can request that specific metadata is copied from the uploaded image to the compressed version. Preserving copyright information, the GPS location and the creation date are currently supported. Preserving metadata adds to the compressed file size, so you should only preserve metadata that is important to keep.

Preserving metadata will not count as an extra compression. However, in the background the image will be created again with the additional metadata.

To preserve specific metadata, call the preserve method on an image source:

$source = \Tinify\fromFile("unoptimized.jpg");
$copyrighted = $source->preserve("copyright", "creation");

You can provide the following options to preserve specific metadata. No metadata will be added if the requested metadata is not present in the uploaded image.

Preserves any copyright information. This includes the EXIF copyright tag (JPEG), the XMP rights tag (PNG) as well as a Photoshop copyright flag or URL. Uses up to 90 additional bytes, plus the length of the copyright data.
creation (JPEG only)
Preserves any creation date or time. This is the moment the image or photo was originally created. This includes the EXIF original date time tag (JPEG). Uses around 70 additional bytes.
location (JPEG only)
Preserves any GPS location data that describes where the image or photo was taken. This includes the EXIF GPS latitude and GPS longitude tags (JPEG). Uses around 130 additional bytes.

Saving to Amazon S3

You can tell the Tinify API to save compressed images directly to Amazon S3. If you use S3 to host your images this saves you the hassle of downloading images to your server and uploading them to S3 yourself.

To save an image to S3, call the store method on an image source:

$source = \Tinify\fromFile("unoptimized.jpg");
    "service" => "s3",
    "aws_access_key_id" => "AKIAIOSFODNN7EXAMPLE",
    "aws_secret_access_key" => "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
    "region" => "us-west-1",
    "path" => "example-bucket/my-images/optimized.jpg"

You need to provide the following options in order to save an image on Amazon S3:

Must be s3 as currently only Amazon S3 is supported.
Your AWS access key ID and secret access key. These are the credentials to an Amazon AWS user account. Find out how to obtain them in Amazon’s documentation. The user must have the correct permissions, see below for details.
The AWS region in which your S3 bucket is located.
The path at which you want to store the image including the bucket name. The path must be supplied in the following format: <bucket>/<path>/<filename>.

The user that corresponds to your AWS access key ID must have the PutObject and PutObjectAcl permissions on the paths of the objects you intend to create.

Example S3 access policy

If you want to create a user with limited access specifically for the Tinify API, you can use the following example policy as a starting point:

  "Statement": {
    "Effect": "Allow",
    "Action": [
    "Resource": [

Error handling

The Tinify API uses HTTP status codes to indicate success or failure. Any HTTP errors are converted into exceptions, which are thrown by the client library.

There are four distinct types of errors. The exception message will contain a more detailed description of the error condition.

There was a problem with your API key or with your API account. Your request could not be authorized. If your compression limit is reached, you can wait until the next calendar month or upgrade your subscription. After verifying your API key and your account status, you can retry the request.
The request could not be completed because of a problem with the submitted data. The exception message will contain more information. You should not retry the request.
The request could not be completed because of a temporary problem with the Tinify API. It is safe to retry the request after a few minutes. If you see this error repeatedly for a longer period of time, please contact us.
The request could not be sent because there was an issue connecting to the Tinify API. You should verify your network connection. It is safe to retry the request.

You can handle each type of error separately:

try {
    // Use the Tinify API client.
} catch(\Tinify\AccountException $e) {
    print("The error message is: " . $e.getMessage());
    // Verify your API key and account limit.
} catch(\Tinify\ClientException $e) {
    // Check your source image and request options.
} catch(\Tinify\ServerException $e) {
    // Temporary issue with the Tinify API.
} catch(\Tinify\ConnectionException $e) {
    // A network connection error occurred.
} catch(Exception $e) {
    // Something else went wrong, unrelated to the Tinify API.

If you are writing code that uses an API key configured by your users, you may want to validate the API key before attempting to compress images. The validation makes a dummy request to check the network connection and verify the API key. An error is thrown if the dummy request fails.

try {
} catch(\Tinify\Exception $e) {
    // Validation of API key failed.

Compression count

The API client automatically keeps track of the number of compressions you have made this month. You can get the compression count after you have validated your API key or after you have made at least one compression request.

$compressionsThisMonth = \Tinify\compressionCount();

Need help? Got feedback?

We’re always here to help, so if you’re stuck just drop us a note on support@tinify.com. It’s also the perfect place to send us all your suggestions and feedback.

Try TinyPNG with a new browser

TinyPNG is created for modern browers with HTML5 & CSS3 support. We have not tried it out in other browsers. The site may work, or it may not. If you see this message you may want to try a different browser!