Skip to content

Instantly share code, notes, and snippets.

Created November 1, 2012 10:11
Show Gist options
  • Save anonymous/3992885 to your computer and use it in GitHub Desktop.
Save anonymous/3992885 to your computer and use it in GitHub Desktop.
File to minify all png files in a dir
<?php
/*
* Usage: php tinypng.php /absolute/path/
* OR
* Usage: php tinypng.php relative/path/
*
* Requirement: "tmp" dir in the same path of tinypng.php
*/
$web = curl_init();
curl_setopt($web, CURLOPT_HEADER, 0);
curl_setopt($web, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($web, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($web, CURLOPT_RETURNTRANSFER, true);
curl_setopt($web, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0");
curl_setopt($web, CURLOPT_URL, 'http://tinypng.org/api/shrink');
curl_setopt($web, CURLOPT_POST, 1);
$dir = realpath($argv[1]);
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
$filename = $dir.'/'.$entry;
if(mime_content_type($filename) == 'image/png') {
echo $filename."\n";
curl_setopt($web, CURLOPT_POSTFIELDS, file_get_contents($filename));
$result = json_decode(curl_exec($web));
file_put_contents(realpath('tmp').'/'.$entry, file_get_contents($result->output->url));
}
}
closedir($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment