You are here

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

Overrides ImageAPIOptimizeProcessorInterface::process

File

modules/kraken/plugins/ImageAPIOptimizeProcessorKraken.inc, line 150

Class

ImageAPIOptimizeProcessorKraken

Code

public function process($image, $dst) {
  $real_path = drupal_realpath($dst);
  if (!$this
    ->libraryPresent()) {
    return FALSE;
  }
  if ($this
    ->getApiKey() && $this
    ->getApiSecret()) {
    $proxy_params = array();
    $proxy_server = variable_get('proxy_server', '');
    if ($proxy_server && _drupal_http_use_proxy('api.kraken.io')) {
      $proxy_params['proxy'] = $proxy_server;
      $proxy_params['port'] = variable_get('proxy_port', 8080);
      $proxy_params['type'] = 'HTTP';
    }
    $kraken = new Kraken($this
      ->getApiKey(), $this
      ->getApiSecret(), $this
      ->getTimeout(), $proxy_params);
    $params = array(
      'file' => $real_path,
      'wait' => TRUE,
      'lossy' => (bool) $this
        ->isLossy(),
      'webp' => (bool) $this
        ->isWebp(),
    );

    // Send the request to Kraken.
    $data = $kraken
      ->upload($params);
    if (!empty($data['success']) && !empty($data['kraked_url'])) {
      $result = drupal_http_request($data['kraked_url']);
      if (!isset($result->error)) {
        file_unmanaged_save_data($result->data, $dst, FILE_EXISTS_REPLACE);

        // @todo: check to see if watchdog entries are enabled for kraken
        watchdog('kraken.io', '@file_name was successfully processed by Kraken.io.
        Original size: @original_size; Kraked size: @kraked_size; Total saved:
        @saved_bytes. All figures in bytes', array(
          '@file_name' => $data['file_name'],
          '@original_size' => $data['original_size'],
          '@kraked_size' => $data['kraked_size'],
          '@saved_bytes' => $data['saved_bytes'],
        ));
        return TRUE;
      }
    }
  }
}