You are here

function image_update in Image 5

Same name and namespace in other branches
  1. 5.2 image.module \image_update()
  2. 6 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_load in ./image.module
Implementation of hook_load
image_operations_rebuild in ./image.module

File

./image.module, line 673

Code

function image_update(&$node) {
  if ($node->new_file || $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 ($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.
      // TODO: this will delete other attachements... which isn't good.
      $result = db_query("SELECT f.fid, f.filepath FROM {files} f WHERE nid=%d", $node->nid);
      while ($file = db_fetch_object($result)) {
        file_delete(file_create_path($file->filepath));
      }
      db_query("DELETE FROM {files} WHERE nid = %d", $node->nid);
      db_query("DELETE FROM {file_revisions} WHERE vid = %d", $node->vid);

      // 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 ($node->rebuild_images) {

        // If we're rebuilding the images, delete all but the original.
        // TODO: this will delete other attachements... which isn't good.
        $original_file = db_fetch_object(db_query("SELECT * FROM {files} WHERE nid=%d AND filename = '%s'", $node->nid, IMAGE_ORIGINAL));
        $result = db_query("SELECT * FROM {files} WHERE nid=%d AND fid <> %d", $node->nid, $original_file->fid);
        while ($file = db_fetch_object($result)) {
          if ($file->filepath != $original_file->filepath) {
            file_delete(file_create_path($file->filepath));
          }
        }
        db_query("DELETE FROM {files} WHERE nid = %d AND fid <> %d", $node->nid, $original_file->fid);
        db_query("DELETE FROM {file_revisions} WHERE vid = %d AND fid <> %d", $node->vid, $original_file->fid);
        _image_build_derivatives($node, FALSE);

        // Display a message to the user if they're be able to modify the node
        // (this might have been called as part of a rebuild by a visitor).
        if (image_access('update', $node)) {
          drupal_set_message(t('The derivative images for <a href="!link">%title</a> have been regenerated.', array(
            '!link' => url('node/' . $node->nid),
            '%title' => $node->title,
          )));
        }

        // Clear the flag so that we don't some how rebuild the images twice.
        $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]);
      }
    }
  }
}