? watermark_implement_image_alter.patch
Index: watermark.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/watermark/watermark.module,v
retrieving revision 1.10
diff -u -p -r1.10 watermark.module
--- watermark.module	23 Jun 2008 23:20:38 -0000	1.10
+++ watermark.module	22 Apr 2010 20:24:36 -0000
@@ -213,7 +213,9 @@ function watermark_check_functions() {
   return TRUE;
 }
 
-function watermark_nodeapi(&$node, $op, $teaser = null, $page = null) {
+
+/* Implement image.module hook_image_alter */
+function watermark_image_alter($node, $filename, $size) {
   if ($node->type != 'image') {
     return;
   }
@@ -226,71 +228,63 @@ function watermark_nodeapi(&$node, $op, 
   if (_watermark_is_node_excluded($node)) {
     return;
   }
-
-  // We hook into node validation to be able to have a watermark on all uploads, also in previews!
-  if ($op == 'validate') {
-    // Was a new file uploaded?
-    $is_new_file = $node->new_file && is_array($node->images);
-    // Has this node already an image?
-    $has_image = !empty($node->images['_original']);
-    // for users with "manage watermarks" permission bypass automatic watermark process
-    if (user_access(WATERMARK_PERM_MANAGE)) {
-      if ($node->wm_apply) {
-        // do we need a watermark, is there any image?
-        if (!$has_image && !$is_new_file) {
-          return;
-        }
-        _watermark_apply($node);
-      }
-    }
-    // normal user, automatic watermark process
-    else if ($is_new_file) {
-      _watermark_apply($node);
-    }
+  
+  // If we applied the watermark to the original image,
+  // don't apply it to any derivatives because it will
+  // already be there.
+  if (variable_get(WATERMARK . IMAGE_ORIGINAL, FALSE) && $size != IMAGE_ORIGINAL) {
+	return;
+  }
+  
+  // If we've configured watermarks for this derivative size,
+  // add the watermark.
+  if (variable_get(WATERMARK . $size, FALSE)) {
+    _watermark_apply_size($filename, $size);
   }
 }
 
-function watermark_form_alter(&$form, &$form_state, $form_id) {
-  // additional option to toggle watermark process when a user has "manage watermarks" permission
-  if ($form_id == 'image_node_form' && user_access(WATERMARK_PERM_MANAGE)) {
-    $apply_watermark = TRUE;
 
-    if (preg_match('/^\/node\/[0-9]+\/edit$/i', $form['#action'])) {
-      $apply_watermark = FALSE;
-    }
-    else if ($form['#action'] != '/node/add/image') {
-      return;
-    }
 
-    $form['watermark'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Watermark settings'),
-      '#collapsible' => TRUE,
-      '#collapsed' => FALSE,
-    );
-    $form['watermark']['wm_apply'] = array(
-      '#title' => t('Apply watermark'),
-      '#type' => 'checkbox',
-      '#description' => t('<strong>Automatic watermark process bypass:</strong> Please decide yourself if a watermark needs to be applied. <strong>Be careful, do not apply watermarks twice!</strong>'),
-      '#default_value' => $apply_watermark,
-    );
+function watermark_node_operations() {
+  $operations = array(
+    'add_watermark' => array(
+      'label' => t('Add watermark to images'),
+      'callback' => 'watermark_operations_add',
+    ),
+  );
+  return $operations;
+}
+
+function watermark_operations_add($nids) {
+  foreach ($nids as $nid) {
+    if ($node = node_load($nid)) {
+      if ($node->type == 'image') {
+        _watermark_apply($node);
+      }
+    }
   }
 }
 
+// Apply watermark to all (configured) derivative image sizes.
 function _watermark_apply($node) {
+  foreach ($node->images as $size => $filepath) {
+    if (variable_get(WATERMARK . $size, FALSE)) {
+      _watermark_apply_size($filename, $size);
+	}
+  }
+}
+// Apply watermark for a single derivative image size.
+function _watermark_apply_size($filepath, $size) {
   $watermark = variable_get(WATERMARK_PATH, '');
   $location = variable_get(WATERMARK_LOCATION, 0);
   $sizes = _image_get_sizes();
-  foreach ($node->images as $size => $filepath) {
-    if (variable_get(WATERMARK . $size, FALSE)) {
-      if (_watermark_process($filepath, $watermark, $location)) {
-        drupal_set_message(t('Watermark applied to image: %file', array('%file' => $sizes[$size]['label'])));
-      }
-      else {
-        drupal_set_message(t('Error adding watermark.'), 'error');
-        watchdog('error', 'Error adding watermark');
-      }
-    }
+
+  if (_watermark_process($filepath, $watermark, $location)) {
+    drupal_set_message(t('Watermark applied to image: %file', array('%file' => $sizes[$size]['label'])));
+  }
+  else {
+    drupal_set_message(t('Error adding watermark.'), 'error');
+    watchdog('error', 'Error adding watermark');
   }
 }
 
