Optimise your JPGs

JPG Optimiser. Free! Provides an API. Simple Quick and Fast.

Digital Ocean

We recommend Digital Ocean for hosting your sites. Free $10 credit when you sign up.

Curl

# Optimising a JPG file (e.g. filename.jpg -> optimised.jpg)
curl -X POST -s --form "input=@filename.jpg;type=image/jpg" http://jpgoptimiser.com/optimise > optimised.jpg

Node.js

// core
var fs = require('fs');

// from npm
var superagent = require('superagent');

// open the output file
var outStream = fs.createWriteStream('optimised.jpg');

// do the request
var req = superagent
    .post('http://jpgoptimiser.com/optimise')
    .attach('input', 'filename.jpg')
;

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

PHP

Many thanks to Arjan Haverkamp for this example.

function JPGoptimiser($JPGfile, &$error = '')
{
   $ch = curl_init('http://jpgoptimiser.com/optimise');
   curl_setopt($ch, CURLOPT_TIMEOUT, 10);
   curl_setopt($ch, CURLOPT_FAILONERROR, 1);
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, array('input' => '@'.$JPGfile));
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $jpg = curl_exec($ch);
   $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
   if ($status !== 200) {
      $error = 'jpgoptimiser.com request failed: HTTP code ' . $status;
      return false;
   }
   $curl_error = curl_error($ch);
   if (!empty($curl_error)) {
      $error = 'jpgoptimiser.com request failed: CURL error ' . $curl_error;
      return false;
   }
   curl_close($ch);
   return $jpg;
}

$result = JPGoptimiser('input.jpg', $error);
if (false === $result) { die("{$error}\n"); }
file_put_contents('crushed.jpg', $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)