Crush your PNGs

PNG Crush. Free! Provides an API. Simple Quick and Fast.

DNSimple

Domains made simple with DNSimple. 30 day free trial plus ONE MONTH hosted DNS free!

Curl

# Crushing a PNG file (e.g. filename.png -> crushed.png)
curl -X POST -s --form "input=@filename.png;type=image/png" http://pngcrush.com/crush > crushed.png

Node.js

// 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('http://pngcrush.com/crush')
    .attach('input', 'filename.png')
;

// save the returned file
req.end(function(res) {
    res.pipe(outStream);
});

PHP

Many thanks to Arjan Haverkamp for this example and Kevin Op den Kamp for an update.

function PNGcrush($PNGfile, &$error = '')
{
   $ch = curl_init('http://pngcrush.com/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 = "pngcrush.com request failed: HTTP code {$status}";
      return false;
   }
   $curl_error = curl_error($ch);
   if (!empty($curl_error)) {
      $error = "pngcrush.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);

More Languages

I'd love to add more examples in your favourite programming language, so feel free to send them my way. andychilton at that gmail place in the sky.

(Ends)