You are here

function resmushit_optimize in reSmush.it image style optimizer 7.2

Parameters

$stylepath:

Return value

bool

1 call to resmushit_optimize()
resmushit_exit in ./resmushit.inc
Implementing hook_exit().

File

./resmushit.inc, line 60
Additional functions.

Code

function resmushit_optimize($stylepath) {
  $debug = FALSE;
  if (variable_get('resmushit_debug', 0) == 1) {
    $debug = TRUE;
  }

  // This is then what reSmush.it will get as its source image.
  $image_contents = file_get_contents($stylepath);
  if ($debug) {
    watchdog('reSmush.it', 'Using drupal_http_request().', array(), WATCHDOG_INFO);
  }
  $boundary = 'A0sFSD';
  $filename = basename($stylepath);
  $mimetype = "application/octet-stream";
  $fileencoded = "--{$boundary}\r\n";
  $fileencoded .= "Content-Disposition: form-data; name=\"files\"; filename=\"{$filename}\"\r\n";
  $fileencoded .= "Content-Transfer-Encoding: binary\r\n";
  $fileencoded .= "Content-Type: {$mimetype}\r\n\r\n";
  $fileencoded .= $image_contents . "\r\n";
  $fileencoded .= "--{$boundary}--";
  $options = array(
    'headers' => array(
      "Content-Type" => "multipart/form-data; boundary={$boundary}",
    ),
    'method' => 'POST',
    'timeout' => variable_get('resmushit_timeout', RESMUSHIT_TIMEOUT),
    'data' => $fileencoded,
  );
  $gotten = drupal_http_request(RESMUSHIT_API_URL, $options);
  if ($gotten->code != 200) {

    // Important to test for an error immediately here. If no response or other error, end immediately.
    watchdog('reSmush.it', 'reSmush.it API request for %dst has failed -- the response code was: %gottencode.', array(
      '%dst' => $stylepath,
      '%gottencode' => $gotten->code,
    ), WATCHDOG_ERROR);
    resmushit_log($stylepath, 'API request error: ' . $gotten->code, '', '', '');
    return FALSE;
  }
  $jsondecoded = json_decode($gotten->data);
  if (!isset($jsondecoded->error)) {
    $result = drupal_http_request($jsondecoded->dest);
    if (!isset($result->error)) {
      file_unmanaged_save_data($result->data, $stylepath, FILE_EXISTS_REPLACE);
    }
    else {
      watchdog('reSmush.it', 'Could not retrieve the optimized image %dst -- the response code was: %gottencode1.', array(
        '%dst' => $stylepath,
        '%gottencode1' => $result->error,
      ), WATCHDOG_ERROR);
      resmushit_log($stylepath, 'Error retrieving from reSmush.it: ' . $result->error, '', '', '');
      return FALSE;
    }
  }
  else {
    watchdog('reSmush.it', 'Could not optimize image %dst -- the reSmush.it response code was: %gottencode1: %gottencode2.', array(
      '%dst' => $stylepath,
      '%gottencode1' => $jsondecoded->error,
      '%gottencode2' => $jsondecoded->error_long,
    ), WATCHDOG_ERROR);
    resmushit_log($stylepath, $jsondecoded->error . ' : ' . $jsondecoded->error_long, '', '', '');
    return FALSE;
  }
  if ($debug) {
    watchdog('reSmush.it', 'Successfully optimized image %dst. Size reduction %before bytes -> %after bytes (%percent%).', array(
      '%dst' => $stylepath,
      '%before' => number_format($jsondecoded->src_size),
      '%after' => number_format($jsondecoded->dest_size),
      '%percent' => $jsondecoded->percent,
    ), WATCHDOG_INFO);
  }

  // Increase the counter of successful optimalizations.
  $total_successes = variable_get('resmushit_total_successes', 0);
  $total_successes++;
  variable_set('resmushit_total_successes', $total_successes);
  resmushit_log($stylepath, 'SUCCESS', number_format($jsondecoded->src_size) . ' bytes', number_format($jsondecoded->dest_size) . ' bytes', $jsondecoded->percent . '%');
  return TRUE;
}