You are here

function resmushit_effect in reSmush.it image style optimizer 7

Same name and namespace in other branches
  1. 7.2 resmushit.inc \resmushit_effect()

Parameters

\stdClass $image:

Return value

bool Execute image optimalization through reSmush.it.

1 string reference to 'resmushit_effect'
resmushit_image_effect_info in ./resmushit.inc

File

./resmushit.inc, line 75
Additional functions.

Code

function resmushit_effect(&$image, $data) {

  // https://api.drupal.org/api/examples/image_example%21image_example.module/function/image_example_colorize_effect/7.x-1.x
  // Image manipulation should be done to the $image->resource, which will be automatically saved as a new image once all effects have been applied.
  // If your effect makes changes to the $image->resource that relate to any information stored in the $image->info array (width, height, etc.) you should update that information as well.
  // Note that the size of the file here is probably smaller than the original image already because of any preceding image styles (e.g. resizing)
  // Any manipulation must be applied to the $image object. Working with $image->source would negate any results of preceding effects.
  $debug = FALSE;
  if (variable_get('resmushit_debug', 0) == 1) {
    $debug = TRUE;
  }
  $temp_path = FALSE;

  // dpm('image coming in: ');
  // dpm($image);
  // dpm('$image->resource: ' . $image->resource);
  // First we have to get a string corresponding to the what $image->resource points to.
  // Unfortunately we have to use imagejpeg or similar function, which will apply the quality settings set in admin/config/media/image-toolkit and thereby ALWAYS reduce the size even before we send it to reSmush.it
  // This is then what reSmush.it will then get as its source image.
  ob_start();
  resmushit_image_gd_save($image);
  $image_contents = ob_get_contents();
  ob_end_clean();

  // dpm('obstarted size good: ' . strlen($image_contents));
  $apiurl = 'http://api.resmush.it/ws.php';

  // See https://www.resmush.it/api
  if ($debug) {

    // dpm('Using drupal_http_request().');
    watchdog('reSmush.it', 'Using drupal_http_request().', array(), WATCHDOG_INFO);
  }
  $boundary = 'A0sFSD';
  $filename = basename($image->source);
  $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 .= file_get_contents(drupal_realpath($image->source)) . "\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,
  );

  // dpm($options);
  $gotten = drupal_http_request($apiurl, $options);

  // dpm($gotten);
  if ($gotten->code != 200) {

    // Important to test for an error immediately here. If no response from WP or other error, end immediately.
    watchdog('reSmush.it', 'Could not optimize image %dst -- the reSmush.it response code was: %gottencode.', array(
      '%dst' => $image->source,
      '%gottencode' => $gotten->code,
    ), WATCHDOG_ERROR);
    resmushit_log($image->source, 'API request error: ' . $gotten->code, '', '', '', $temp_path);
    return FALSE;
  }
  $jsondecoded = json_decode($gotten->data);

  // dpm('$jsondecoded: ');
  // dpm($jsondecoded);
  if (@$jsondecoded->error > 200) {

    // reSmush.it responded but has the image had NOT been optimized?
    // dpm('reSmush.it could not optimize image ' . $image->source . ' (response code was: ... ' . $jsondecoded->error . ' : ' . $jsondecoded->error_long);
    watchdog('reSmush.it', 'Could not optimize image %dst -- the reSmush.it response code was: %gottencode1: %gottencode2.', array(
      '%dst' => $image->source,
      '%gottencode1' => $jsondecoded->error,
      '%gottencode2' => $jsondecoded->error_long,
    ), WATCHDOG_ERROR);
    resmushit_log($image->source, $jsondecoded->error . ' : ' . $jsondecoded->error_long, '', '', '', $temp_path);
    return FALSE;
  }

  // reSmush.it web service did the job and we can download the optimized image
  $result = drupal_http_request($jsondecoded->dest);

  // dpm($jsondecoded->dest);
  // dpm($result);
  if (!isset($result->error)) {

    // dpm('result of http request for optimized image: ');
    // dpm($result);
    // dpm('strlen: ' . strlen($result->data));
    $optimized_image_resource = imagecreatefromstring($result->data);

    // dpm('optimized_image_resource: ');
    // dpm($optimized_image_resource);
    // dpm($image->resource);
    imagedestroy($image->resource);

    // Destroy old first, as in https://api.drupal.org/api/drupal/modules%21system%21image.gd.inc/function/image_gd_load/7.x
    $image->resource = $optimized_image_resource;

    // dpm($image->resource);
    // dpm('modified old image: ');
    // dpm($image);
    // dpm('$image->resource: ' . $image->resource);
    if ($debug) {

      // dpm("reSmush.it has successfully optimized image " . $image->source . ". Size reduction " . number_format($jsondecoded->src_size) . " bytes -> " . number_format($jsondecoded->dest_size) . " bytes ($jsondecoded->percent%).");
      watchdog('reSmush.it', 'Successfully optimized image %dst. Size reduction %before bytes -> %after bytes (%percent%).', array(
        '%dst' => $image->source,
        '%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($image->source, 'SUCCESS', number_format($jsondecoded->src_size) . ' bytes', number_format($jsondecoded->dest_size) . ' bytes', $jsondecoded->percent . '%', $temp_path);
    return TRUE;
  }
  else {

    // We did not manage to download the optimized image from reSmush.it.
    watchdog('reSmush.it', 'We were unable to retrieve the optimalized image %dst from reSmush.it.', array(
      '%dst' => $image->source,
    ), WATCHDOG_ERROR);
    resmushit_log($image->source, 'Error retrieving from reSmush.it: ' . $result->error, '', '', '', $temp_path);
    return FALSE;
  }
}