All of these examples should be immediately runnable in your language of choice. If you don't see your language feel free to contribute an example.
# Crushing a PNG file (e.g. filename.png -> crushed.png) curl -X POST -s --form "input=@filename.png;type=image/png" https://www.toptal.com/developers/pngcrush/crush > crushed.png
// core var fs = require('fs'); // from npm var superagent = require('superagent'); // open the output file var outStream = fs.createWriteStream('crushed.png'); // do the request var req = superagent .post('https://www.toptal.com/developers/pngcrush/crush') .attach('input', 'filename.png') ; // save the returned file req.end(function(res) { res.pipe(outStream); });
Many thanks toArjan Haverkamp for this example andKevin Op den Kamp for an update.
function PNGcrush($PNGfile, &$error = '') { $ch = curl_init('https://www.toptal.com/developers/pngcrush/crush'); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_FAILONERROR, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, [ 'input' => new CurlFile($PNGfile, 'image/png', $PNGfile) ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $png = curl_exec($ch); $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($status !== 200) { $error = "www.toptal.com request failed: HTTP code {$status}"; return false; } $curl_error = curl_error($ch); if (!empty($curl_error)) { $error = "www.toptal.com request failed: CURL error ${$curl_error}"; return false; } curl_close($ch); return $png; } $result = PNGcrush('input.png', $error); if (false === $result) { die("{$error}\n"); } file_put_contents('crushed.png', $result);