You are here

public function ImageAPIOptimizeProcessorReSmushIt::process in Image Optimize (or ImageAPI Optimize) 7.2

Overrides ImageAPIOptimizeProcessorInterface::process

File

plugins/imageapi_optimize/ImageAPIOptimizeProcessorReSmushIt.inc, line 44

Class

ImageAPIOptimizeProcessorReSmushIt

Code

public function process($image, $dst) {
  $dst = drupal_realpath($dst);
  $url = 'http://www.resmush.it/ws.php';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_POST, TRUE);
  if (!class_exists('CURLFile')) {
    $arg = array(
      'files' => '@' . drupal_realpath($dst),
    );
  }
  else {
    $cfile = new CURLFile($dst);
    $arg = array(
      'files' => $cfile,
    );
  }
  curl_setopt($ch, CURLOPT_POSTFIELDS, $arg);
  $data = curl_exec($ch);
  curl_close($ch);
  if ($data && ($json = json_decode($data))) {

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