You are here

function _image_build_derivatives_if_needed in Image 6

Rebuild derivatives if needed. Helper function for image_load().

1 call to _image_build_derivatives_if_needed()
image_load in ./image.module
Implementation of hook_load().

File

./image.module, line 487

Code

function _image_build_derivatives_if_needed(&$node) {
  $node->rebuild_images = FALSE;

  // Figure out which sizes should have been generated.
  $all_sizes = image_get_sizes();
  unset($all_sizes[IMAGE_ORIGINAL]);
  $needed_sizes = array_keys(image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]));
  $unneeded_sizes = array_diff(array_keys($all_sizes), $needed_sizes);

  // Derivative sizes that are larger than the original get set to the
  // original.
  foreach ($unneeded_sizes as $key) {
    if (empty($node->images[$key])) {
      $node->images[$key] = $node->images[IMAGE_ORIGINAL];
    }
    else {

      // Need to remove an extra derivative image in the database.
      $node->rebuild_images = TRUE;
    }
  }

  // Check that the derivative images are present and current.
  foreach ($needed_sizes as $key) {

    // If the file is missing or created after the last change to the sizes,
    // rebuild the derivatives.
    if (empty($node->images[$key]) || !file_exists($node->images[$key])) {
      $node->rebuild_images = TRUE;
    }
    elseif (filemtime($node->images[$key]) < variable_get('image_updated', 0)) {
      $node->rebuild_images = TRUE;
    }
  }

  // Correct any problems with the derivative images.
  if ($node->rebuild_images) {

    // Possibly TODO: calling a hook implementation here isn't very elegant.
    // but the code there is tangled and it works, so maybe leave it ;)
    image_update($node);
    watchdog('image', 'Derivative images were regenerated for %title.', array(
      '%title' => $node->title,
    ), WATCHDOG_INFO, l(t('view'), 'node/' . $node->nid));
  }
}