I have implemented a PHP code from tiny png API in which i want to compress my PNG file. But as i an executing this code it is providing me the link to download those file. Instead i want all those images file to be saved automatically to my specified directory.
$key = "tiny png api key";
$input = "images2.png";
$output = "images2.png";
$request = curl_init();
curl_setopt_array($request, array(
CURLOPT_URL => "https://api.tinypng.com/shrink",
CURLOPT_USERPWD => "api:" . $key,
CURLOPT_POSTFIELDS => file_get_contents($input),
CURLOPT_BINARYTRANSFER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
/* Uncomment below if you have trouble validating our SSL certificate.
Download cacert.pem from: http://curl.haxx.se/ca/cacert.pem */
// CURLOPT_CAINFO => __DIR__ . "/cacert.pem",
CURLOPT_SSL_VERIFYPEER => true
));
$response = curl_exec($request);
echo $response."<BR>";
if (curl_getinfo($request, CURLINFO_HTTP_CODE) === 201) {
/* Compression was successful, retrieve output from Location header. */
$headers = substr($response, 0, curl_getinfo($request, CURLINFO_HEADER_SIZE));
foreach (explode("\r\n", $headers) as $header) {
if (substr($header, 0, 10) === "Location: ") {
$request = curl_init();
curl_setopt_array($request, array(
CURLOPT_URL => substr($header, 10),
CURLOPT_RETURNTRANSFER => true,
/* Uncomment below if you have trouble validating our SSL certificate. */
// CURLOPT_CAINFO => __DIR__ . "/cacert.pem",
CURLOPT_SSL_VERIFYPEER => true
));
file_put_contents($output, curl_exec($request));
}
}
} else {
print(curl_error($request));
/* Something went wrong! */
print("Compression failed");
}