You are here

function image_load in Image 5.2

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

Implementation of hook_load

File

./image.module, line 648

Code

function image_load(&$node) {
  $result = db_query("SELECT i.image_size, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid);
  $node->images = array();
  while ($file = db_fetch_object($result)) {
    $node->images[$file->image_size] = 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.
      $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) {
    image_update($node);
    watchdog('image', t('Derivative images were regenerated for %title.', array(
      '%title' => $node->title,
    )), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
  }
}