You are here

function kraken_optimize in Kraken 7.2

Same name and namespace in other branches
  1. 7 kraken.module \kraken_optimize()

Callback to optimize the image with the kraken.io service.

1 string reference to 'kraken_optimize'
kraken_image_effect_info in ./kraken.module
Implements hook_image_effect_info().

File

./kraken.module, line 130
Provides optimised images via http://kraken.io.

Code

function kraken_optimize(&$image, $data) {
  $libraries = libraries_detect('kraken-php');
  if (isset($libraries['error'])) {
    drupal_set_message(t('!error_msg Kraken.io integration requires the
      <a href="">Kraken-PHP library</a> to be installed in your libraries folder
      as kraken-php (sites/all/libraries/kraken-php).', array(
      '!error_msg' => $libraries['error message'],
      '!kraken-lib' => l(t('Kraken-PHP library'), 'https://github.com/kraken-io/kraken-php'),
    )), 'error');
    return FALSE;
  }

  // Ensure the API credentials have been set.
  $kraken_settings = variable_get('kraken', FALSE);
  if (empty($kraken_settings['api_key']) || empty($kraken_settings['api_secret'])) {
    drupal_set_message(t('Kraken.io API settings are not set. Go to !img_toolkit
      to provide the crendentials from your !kraken_acct.', array(
      '!img_toolkit' => l(t('Image toolkit'), 'admin/config/media/image-toolkit'),
      '!kraken_acct' => l(t('Kraken.io account'), 'https://kraken.io/account/login'),
    )), 'error');
    return FALSE;
  }
  if (!libraries_load('kraken-php')) {
    drupal_set_message(t('The kraken-php library has not loaded correctly. Please
      review the installation instructions'), 'error');
  }
  $kraken = new Kraken($kraken_settings['api_key'], $kraken_settings['api_secret']);
  $params = array(
    'file' => drupal_realpath($image->source),
    'wait' => TRUE,
    'lossy' => TRUE,
  );
  $data = $kraken
    ->upload($params);
  if ($data['success']) {
    $result = drupal_http_request($data['kraked_url']);
    if (!isset($result->error)) {
      file_unmanaged_save_data($result->data, $image->source, 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_byes' => $data['saved_bytes'],
      ));
      return TRUE;
    }
  }
}