1

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");
 }
anand
  • 1,711
  • 2
  • 24
  • 59
  • The code is from https://tinypng.com/developers/reference - have you contacted their support? What have they responded? – blue Apr 08 '14 at 09:34
  • yeah , I have send them the mail regarding this issue but still i have got no response from their side. – anand Apr 08 '14 at 10:13
  • Now its working fine i am able to get the image file with user-defined name and its able to download in the user-specified directory; with file_put_contents($output,curl_exec($request)) ; this statements download the file to the specified directory ($output) ; my file was not getting downloaded in many of the folders except /tmp folder in ubuntu 12.04 ; – anand Apr 08 '14 at 11:05
  • i think this png/jpg compressor is better way2enjoy.com/compress-png as it allows 50 files in one chance. if you know any better then kindly share link – Steeve Feb 13 '17 at 08:10

0 Answers0