网页 综合 更新时间 2026-11-15 07:27:03

Image Compression REST API (HTTP) | Tinify Docs

自动蜘蛛 tinypng.com 正文约 6000 字
Image Compression REST API (HTTP) | Tinify Docs
The Tinify API allows you to compress and optimize AVIF, JPEG XL, WebP, JPEG and PNG images. It is designed as a REST service. This page describes the raw HTTP API. We also maintain client libraries in various languages that make it very easy to interact with the Tinify API.
The API is hosted at api.tinify.com
curl must be installed to follow the examples below. Verify with:
If it’s not installed, see the official installation instructions:
https://curl.se/download.html
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! Make your requests from your server, not from a web page: the API doesn’t accept requests from browsers on other websites, and a web page would reveal your key.
Authentication to the API is done with HTTP Basic Auth. All requests require an Authorization header that contains the Base64 encoding of the authentication string api:YOUR_API_KEY, where YOUR_API_KEY is the key that can be found on your API account page.
All requests must be made over an encrypted HTTPS connection, using TLS 1.2 or later.
You can upload any AVIF, JPEG XL, WebP, JPEG or PNG image to the Tinify API to compress it. We will automatically detect the type of image and optimize with the TinyPNG or TinyJPG engine accordingly. You can also upload HEIC images, which are compressed to JPEG. Compression will start as soon as you upload a file or provide the URL to the image.
Images can be up to 500 MB and 256 megapixels, and at most 150,000 pixels wide or high. The compressed image is available for 8 hours after you upload it, for you to download, resize, convert or store it.
You can upload a local file. The post data contains the image binary.
For example, if you have a file named unoptimized.jpg in the current directory:
You can also provide a URL to your image instead of having to upload it. The API accepts a JSON body with the image URL as a source location.
For example, if you have https://tinypng.com/images/panda-happy.png as your source:
The compressed image can be retrieved by using the URL in the Location header from the previous step.
For example, you can save the compressed image to a file named optimized.jpg and open it:
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.
You can resize an image by using the URL that was returned in the Location header after compressing. A JSON request body has to be provided together with a Content-Type: application/json header.
For example, to save the resized and compressed image to a file named thumbnail.jpg:
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 of your image.
A more advanced implementation of cover that also detects cut out images with plain backgrounds. The image is scaled down to the width and height you provide. If an image is detected with a free-standing object it will add more background space where necessary or crop the unimportant parts.
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. This means that cover can return an image smaller than the target dimensions, while thumb still returns a cut-out image at the target dimensions by adding background around it.
You can use the API to convert your images to your desired image type. Tinify currently supports converting between AVIF, JPEG XL, WebP, JPEG and PNG. HEIC images you upload are compressed to JPEG, and you can convert them to any of these types with a convert request. When you provide more than one image type in your convert request, the smallest version will be returned to you.
Converting an image counts as one additional compression.
For example, to save a compressed PNG or JPEG image as a WebP file named panda-sticker.webp:
To convert an image with a transparent background to JPEG, which does not support transparency, specify a background color that replaces the transparency using the transform object. Without one, JPEG is skipped when you request more than one image type, and a request for JPEG alone fails with a 400 Bad Request error.
If you wish to convert an image with a transparent background to one with a solid background, specify a background property in the transform object. If this property is provided, the background of a transparent image will be filled.
For example, if you have an image with a transparent background and you want to save it as JPEG:
You can provide the following options to convert your image:
Your desired image types. The following options are available as a type:
The transform object specifies the stylistic transformations that will be applied to your image. Include a background property to fill a transparent im
访问原文 返回搜索 https://tinypng.com/developers/reference/http
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。