You are here

function _imageapi_optimize_service_smushit in Image Optimize (or ImageAPI Optimize) 6

Use smushit.com to optimize

File

./imageapi_optimize.module, line 237
Image optimize functionalities

Code

function _imageapi_optimize_service_smushit($image, $dst) {
  if (!function_exists('json_decode')) {
    drupal_set_message(t('Required function, json_decode(), is not available.'), 'error');
    return FALSE;
  }
  $url = 'http://www.smushit.com/ysmush.it/ws.php';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    'files' => '@' . $dst,
  ));
  $data = curl_exec($ch);
  curl_close($ch);
  $json = json_decode($data);

  // SmushIt return an error if it cannot optimize the image. Otherwise, it
  // returns an object, with 'dest' (temporary file) and 'percent' (savings)
  // among other properties.
  if (!$json->error) {
    $result = drupal_http_request($json->dest);
    if (!isset($result->error)) {
      file_save_data($result->data, $dst, FILE_EXISTS_REPLACE);
      return TRUE;
    }
  }
}