Shell

1
curl --user api:$YOUR_API_KEY --data-binary @unoptimized.png -i https://api.tinify.com/shrink

PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$data = file_get_contents($file_path);

$request = curl_init();
curl_setopt($request, CURLOPT_URL, "https://api.tinify.com/shrink");
curl_setopt($request, CURLOPT_USERPWD, "api:YOUR_API_KEY");
curl_setopt($request, CURLOPT_POSTFIELDS, $data);
curl_setopt($request, CURLOPT_BINARYTRANSFER, true);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($request);
curl_close($request);

$dic = json_decode($response, true);
if($dic != null && $dic['output'] != null && $dic['output']['url'] != null)
{
$imgURL = $dic['output']['url'];
$data = file_get_contents($imgURL);
file_put_contents($file_path, $data); // 覆盖源文件
}