You are here

function image_update in Image 6

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

Implementation of hook_update().

Take $node by reference so we can use this to save the node after rebuilding derivatives.

2 calls to image_update()
image_operations_rebuild in ./image.module
_image_build_derivatives_if_needed in ./image.module
Rebuild derivatives if needed. Helper function for image_load().

File

./image.module, line 566

Code

function image_update(&$node) {
  if (!empty($node->new_file) || !empty($node->rebuild_images)) {

    // Derivative images that aren't needed are set to the original file. Make
    // note of the current path before calling _image_insert() because if it's
    // in the temp directory it'll be moved. We'll need it later to determine
    // which derivative images need to be saved with _image_insert().
    $original_path = $node->images[IMAGE_ORIGINAL];
    if (!empty($node->new_file)) {

      // The derivative images were built during image_prepare() or
      // image_create_node_from() so all we need to do is remove all the old,
      // existing images.
      // Remove all the existing images.
      $result = db_query("SELECT f.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d", $node->nid);
      while ($file = db_fetch_object($result)) {
        db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid);
        _image_file_remove($file);
      }

      // Save the original first so that it if it's moved the derivatives are
      // placed in the correct directory.
      _image_insert($node, IMAGE_ORIGINAL, $original_path);
    }
    else {
      if (!empty($node->rebuild_images)) {

        // Find the original image.
        $original_file = db_fetch_object(db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND i.image_size = '%s'", $node->nid, IMAGE_ORIGINAL));

        // Delete all but the original image.
        $result = db_query("SELECT i.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.nid = %d AND f.fid <> %d", $node->nid, $original_file->fid);
        while ($file = db_fetch_object($result)) {
          db_query("DELETE FROM {image} WHERE nid = %d AND fid = %d", $node->nid, $file->fid);

          // Beware of derivative images that have the same path as the original.
          if ($file->filepath != $original_file->filepath) {
            _image_file_remove($file);
          }
        }
        $node->images = _image_build_derivatives($node, FALSE);

        // Prevent multiple rebuilds.
        $node->rebuild_images = FALSE;
      }
    }
    $sizes = image_get_derivative_sizes($node->images[IMAGE_ORIGINAL]);
    foreach ($sizes as $key => $size_info) {
      if (!empty($node->images[$key]) && $node->images[$key] != $original_path) {
        _image_insert($node, $key, $node->images[$key]);
      }
    }
  }
}