You are here

function image_load in Image 5

Same name and namespace in other branches
  1. 5.2 image.module \image_load()
  2. 6 image.module \image_load()

Implementation of hook_load

File

./image.module, line 591

Code

function image_load(&$node) {
  $result = db_query("SELECT filename, filepath FROM {files} WHERE nid=%d", $node->nid);
  $node->images = array();
  while ($file = db_fetch_object($result)) {
    $node->images[$file->filename] = file_create_path($file->filepath);
  }
  $original_path = $node->images[IMAGE_ORIGINAL];
  if (empty($original_path)) {

    // There's no original image, we're in trouble...
    return;
  }
  $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($original_path));
  $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] = $original_path;
    }
    else {

      // Need to remove an extra derivative image in the database.

      #      drupal_set_message(t("%title's has an unneeded %key derivative images. The derivatives will be rebuilt to remove it.", array('%title' => $node->title, '%key' => $key)));
      $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])) {

      #      drupal_set_message(t("%title's %key derivative image %filepath was missing. The derivatives will be rebuilt to regenerate it.", array('%title' => $node->title, '%key' => $key, '%filepath' => $node->images[$key])));
      $node->rebuild_images = TRUE;
    }
    else {
      if (filemtime($node->images[$key]) < variable_get('image_updated', 0)) {

        #      drupal_set_message(t("%title's %key derivative image had a timestamp (%filetime) that predates the last changes to the image size settings (%vartime). The derivatives will be rebuilt to regenerate it.", array('%title' => $node->title, '%key' => $key, '%filetime' => format_date(filemtime($node->images[$key])), '%vartime' => format_date(variable_get('image_updated', 0)))));
        $node->rebuild_images = TRUE;
      }
    }
  }

  // Correct any problems with the derivative images.
  if ($node->rebuild_images) {
    image_update($node);
    watchdog('image', t('Derivative images were regenerated for %title.', array(
      '%title' => $node->title,
    )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
  }
}