--- imagecache.module.orig	2007-06-16 19:22:41.000000000 +0200
+++ imagecache.module	2007-09-07 10:37:20.000000000 +0200
@@ -201,6 +201,61 @@ function imagecache_cache() {
             watchdog('imagecache', t('Imagecache crop action ID %id failed.', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
           }
           break;
+        case 'watermark':
+          // Copy the watermark image to temporaty directory
+          $watermark = file_directory_path() . '/' . $action['data']['wm_image'];
+          file_copy( $watermark, file_directory_temp());
+          $tmpwatermark = file_directory_temp() . '/' . $action['data']['wm_image'];
+          
+          $wm_size = getimagesize( $tmpwatermark);
+          
+          
+          switch( $action['data']['location'] ) {
+            case 0:
+              $start_x = 0;
+              $start_y = 0;
+              break;
+            case 1:
+              $start_x = floor(($size[0]-$wm_size[0])/2);
+              $start_y = 0;
+              break;
+            case 2:
+              $start_x = $size[0]-$wm_size[0];
+              $start_y = 0;              
+              break;
+            case 3:
+              $start_x = 0;
+              $start_y = floor(($size[1]-$wm_size[1])/2);
+              break;
+            case 4:
+              $start_x = floor(($size[0]-$wm_size[0])/2);
+              $start_y = floor(($size[1]-$wm_size[1])/2);
+              break;
+            case 5:
+              $start_x = $size[0]-$wm_size[0];
+              $start_y = floor(($size[1]-$wm_size[1])/2);
+              break;
+            case 6:
+              $start_x = 0;
+              $start_y = $size[1]-$wm_size[1];
+              break;
+            case 7:
+              $start_x = floor(($size[0]-$wm_size[0])/2);
+              $start_y = $size[1]-$wm_size[1];
+              break;
+            case 8:
+              $start_x = $size[0]-$wm_size[0];
+              $start_y = $size[1]-$wm_size[1];
+              break;
+          }
+
+          if ( !image_watermark($tmpdestination, $tmpwatermark, $tmpdestination, $start_x, $start_y )) {
+            watchdog('imagecache', t('Imagecache watermark action ID %id failed', array('%id' => $action['actionid'])), WATCHDOG_ERROR);
+          }
+          
+          // Remove temporary watermark image
+          file_delete( $tmpwatermark);
+          break;
       }
     }
     file_move($tmpdestination, $destination);
@@ -226,6 +281,44 @@ function imagecache_cache() {
 }
 
 /**
+ * Watermark a source image with a watermark image at pos x,y of the source image.
+ *
+ * @param $source        The filepath of the source image
+ * @param $watermark     The filepath of the watermark image
+ * @param $destination   The filepath of the destination image
+ * @param $x             The top left co-ordinate of the watermark place (x axis value)
+ * @param $y             The top left co-ordinate of the watermark place (y axis value)
+ */
+function image_watermark($source, $watermark, $destination, $x, $y ) {
+  return image_toolkit_invoke('watermark', array($source, $watermark, $destination, $x, $y ));
+}
+
+/**
+ * Watermark an image using the GD toolkit.
+ */
+function image_gd_watermark( $source, $watermark, $destination, $x, $y ) {
+  $info = image_get_info($source);
+  $wm_info = image_get_info( $watermark);
+  if ( !$info || !$wm_info) {
+    drupal_set_message( 'wm: ' . $watermark);
+    return FALSE;
+  }
+
+  $image = image_gd_open( $source, $info['extension']);
+  $wm_image = image_gd_open( $watermark, $wm_info['extension']);
+  
+  imagecopy( $image, $wm_image, $x, $y, 0, 0, $wm_info['width'], $wm_info['height'] );
+  
+  $result = image_gd_close( $image, $destination, $info['extension']);
+  
+  imagedestroy($image);
+  imagedestroy($wm_image);
+  
+  return $result;
+  
+}
+
+/**
  * Implementation of hook_field_formatter_info().
  */
 function imagecache_field_formatter_info() {
@@ -675,6 +768,20 @@ function _imagecache_actions_form($prese
         );
         break;
       case 'watermark':
+        $form[$actionid]['data']['wm_image'] = array(
+          '#type' => 'textfield',
+          '#title' => t('Watermark image'),
+          '#default_value' => $action['data']['wm_image'],
+          '#description' => t('Enter the path and image filename of the watermark relative to drupal/files/imagecache/'),
+        );
+
+        $form[$actionid]['data']['location'] = array(
+          '#type' => 'select',
+          '#options' => array('0' => t('Top left'), '1' => t('Top center'), '2' => t('Top right'),  '3' => t('Middle left'), '4' => t('Middle center'), '5' => t('Middle right'), '6' => t('Bottom left'), '7' => t('Bottom center'), '8' => t('Bottom right')),
+          '#title' => t('Location'),
+          '#description' => t('Location on the image where the watermark will be placed.'),
+          '#default_value' => $action['data']['location'],
+         );
         // Think about this one...        
     }
     $form[$actionid]['remove'] = array(
@@ -687,11 +794,12 @@ function _imagecache_actions_form($prese
   $helptext['scale']  = t('<strong>Scale</strong>: Resize an image maintaining the original aspect-ratio (only one value necessary).');
   $helptext['resize'] = t('<strong>Resize</strong>: Resize an image to an exact set of dimensions, ignoring aspect ratio.');
   $helptext['crop']   = t('<strong>Crop</strong>: Crop an image to the rectangle specified by the given offsets and dimensions.');
+  $helptext['watermark'] = t('<strong>Watermark</strong>: Apply a watermark image to the image.');
   $description = '<ul><li>'. implode('</li><li>', $helptext) .'</li><ul>';
 
   $form['newaction'] = array(
     '#type' => 'select',
-    '#options' => array('' => t('select...'), 'scale' => t('Scale'), 'resize' => t('Resize'), 'crop' => t('Crop')),
+    '#options' => array('' => t('select...'), 'scale' => t('Scale'), 'resize' => t('Resize'), 'crop' => t('Crop'), 'watermark' => t('Watermark')),
     '#title' => t('Add a new action'),
     '#description' => $description,
   );
