diff -urp ./README.txt ../watermark_new/README.txt
--- ./README.txt	2006-10-05 20:56:54.000000000 +0200
+++ ../watermark_new/README.txt	2007-06-25 01:58:01.843750000 +0200
@@ -1,6 +1,12 @@
 $Id: README.txt,v 1.2 2006/10/05 18:56:55 kbahey Exp $
 
-By Khalid Baheyeldin of http://2bits.com
+Image Watermark
+
+by Khalid Baheyeldin of http://2bits.com
+modified by schnizZzla for http://BerlinerStrassen.com
+
+Scaling option based on patch (http://drupal.org/node/81161)
+by michael curry (inactivist) of http://exodusdev.com/
 
 Description:
 -------------
@@ -12,22 +18,46 @@ with a watermark.
 - The location of the watermark can be any one of nine positions.
 - Can select which images to apply the watermark to (e.g. preview and _original, but
   not thumbnail.
+- Preserve original image type after watermark has been applied
+- Support alpha blending
+- Apply watermark only when a new image is uploaded, do not not apply watermarks twice!
+- Apply watermark on previews, too
+- Watermark can be scaled, percentage of image width and minimum width in pixels can be set.
+- Toggle: Exclude image nodes with specific gallery terms from watermark
+
+
+Known Limitations:
+- Changing watermark settings does not work retroactive, watermark settings apply to new images only
+- Saving image module settings "breaks" watermark SETTINGS, even if you did not change anything.
+  That means watermarks disappear or apply to all image sizes, when the node or gallery is viewed.
+
 
 Requirements:
 -------------
 
 This module requires no patches to Drupal nor image module.
 
-It does require the PHP GD library to be configured by your host, and 
+It does require the PHP GD library to be configured by your host, and
 PHP must be compiled in with --enable-exif.
 
 GD is enabled by default in PHP 4.3 and later, and can be added
 to earlier version if compiled in them.
 For more information see http://php.net/image
 
-The module was tested with PHP 5.1.2, but should work with other
+The module was tested with PHP 4.4.0 and 5.1.2, but should work with other
 versions that had GD.
 
 Check the output of phpinfo() from your server to see if you have all
 required pieces.
 
+Usage:
+------
+
+Enable the module and navigate to admin/settings/image_watermark to enable watermark processing
+for new image uploads.
+
+To do:
+------
+- complete implementation of watermark_admin_settings_validate
+- complete watchdog error reports
+- animated gif support (a detection to exclude gif images automatically or a method to process animated gifs)
\ No newline at end of file
diff -urp ./watermark.info ../watermark_new/watermark.info
--- ./watermark.info	2007-06-19 02:26:34.000000000 +0200
+++ ../watermark_new/watermark.info	2007-06-25 01:57:59.437500000 +0200
@@ -1,5 +1,5 @@
 ; $Id: watermark.info,v 1.1.2.2 2007/06/18 23:07:15 dww Exp $
-name = Watermark
+name = Image Watermark
 description = "Allows the user to apply watermarks on image upload"
 dependancies = image
 package = Image
@@ -8,5 +8,4 @@ package = Image
 ; Information added by drupal.org packaging script on 2007-06-19
 version = "5.x-1.x-dev"
 project = "watermark"
-datestamp = "1182212795"
-
+datestamp = "1182212795"
\ No newline at end of file
diff -urp ./watermark.module ../watermark_new/watermark.module
--- ./watermark.module	2007-02-18 23:59:52.000000000 +0100
+++ ../watermark_new/watermark.module	2007-06-25 01:57:56.437500000 +0200
@@ -3,19 +3,26 @@
 // $Id: watermark.module,v 1.5.2.1 2007/02/18 23:59:52 kbahey Exp $
 // Author: Khalid Baheyeldin of http://2bits.com
 
-define('WATERMARK_ENABLE',           'watermark_enable');
-define('WATERMARK_PATH',             'watermark_path');
-define('WATERMARK_LOCATION',         'watermark_location');
-define('WATERMARK',                  'watermark_');
-define('WATERMARK_POS_MIDDLE',       0);
-define('WATERMARK_POS_MIDDLERIGHT',  1);
-define('WATERMARK_POS_MIDDLELEFT',   2);
-define('WATERMARK_POS_TOPMIDDLE',    3);
-define('WATERMARK_POS_TOPLEFT',      4);
-define('WATERMARK_POS_TOPRIGHT',     5);
-define('WATERMARK_POS_BOTTOMMIDDLE', 6);
-define('WATERMARK_POS_BOTTOMRIGHT',  7);
-define('WATERMARK_POS_BOTTOMLEFT',   8);
+define('WATERMARK_ENABLE',                'watermark_enable');
+define('WATERMARK_PATH',                  'watermark_path');
+define('WATERMARK_LOCATION',              'watermark_location');
+define('WATERMARK_SCALING',               'watermark_scaling');
+define('WATERMARK_SCALING_PERCENTAGE',    'watermark_scaling_percentage');
+define('WATERMARK_MIN_WIDTH',             'watermark_min_width');
+define('WATERMARK_EXCLUDE',               'watermark_exclude');
+define('WATERMARK_EXCLUDE_TERMS',         'watermark_exclude_terms');
+define('WATERMARK_WATCHDOG',              'watermark');
+define('WATERMARK',                       'watermark_');
+define('WATERMARK_POS_MIDDLE',            0);
+define('WATERMARK_POS_MIDDLERIGHT',       1);
+define('WATERMARK_POS_MIDDLELEFT',        2);
+define('WATERMARK_POS_TOPMIDDLE',         3);
+define('WATERMARK_POS_TOPLEFT',           4);
+define('WATERMARK_POS_TOPRIGHT',          5);
+define('WATERMARK_POS_BOTTOMMIDDLE',      6);
+define('WATERMARK_POS_BOTTOMRIGHT',       7);
+define('WATERMARK_POS_BOTTOMLEFT',        8);
+
 
 function watermark_help($section) {
   switch ($section) {
@@ -25,80 +32,155 @@ function watermark_help($section) {
 }
 
 function watermark_menu($may_cache) {
-  return array();
+  $items = array();
+  $items[] = array(
+    'path' => 'admin/settings/image_watermark',
+    'title' => t('Image Watermark'),
+    'description' => t('Configure image watermark settings.'),
+    'callback' => 'drupal_get_form',
+    'callback arguments' => array('watermark_admin_settings'),
+    'access' => user_access('administer images'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+  return $items;
 }
 
-function watermark_form_alter($form_id, &$form) {
-  if ($form_id == 'image_admin_settings') {
+function watermark_admin_settings() {
+  if (!watermark_check_functions()) {
+    return;
+  }
 
-    if (!watermark_check_functions()) {
-      return;
-    }
+  $form['watermark'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Watermark'),
+    '#collapsible' => true,
+    '#weight' => -1,
+  );
+
+  $form['watermark'][WATERMARK_ENABLE] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable watermarking of new images?'),
+    '#default_value' => variable_get(WATERMARK_ENABLE, false),
+    '#description' => t('Check this box to enable watermarking of new images.'),
+    );
 
-    $form['watermark'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Watermark'),
-      '#collapsible' => TRUE,
-      '#weight' => -1,
+  $form['watermark'][WATERMARK_PATH] = array(
+    '#type' => 'textfield',
+    '#title' => t('Watermark overlay image'),
+    '#size' => 70,
+    '#maxlength' => 255,
+    '#required' => true,
+    '#default_value' => variable_get(WATERMARK_PATH, ''),
+    '#description' => t('Path of the image that should be used as the watermark image, relative to the Drupal root.  This should be a truecolor (32 bit) PNG file with an alpha channel, but other formats may work.'),
     );
 
-    $form['watermark'][WATERMARK_ENABLE] = array(
+  $locations = array(
+    WATERMARK_POS_MIDDLE       => t('Middle'),
+    WATERMARK_POS_MIDDLERIGHT  => t('Middle right'),
+    WATERMARK_POS_MIDDLELEFT   => t('Middle left'),
+    WATERMARK_POS_TOPMIDDLE    => t('Top middle'),
+    WATERMARK_POS_TOPLEFT      => t('Top left'),
+    WATERMARK_POS_TOPRIGHT     => t('Top right'),
+    WATERMARK_POS_BOTTOMMIDDLE => t('Bottom middle'),
+    WATERMARK_POS_BOTTOMRIGHT  => t('Bottom right'),
+    WATERMARK_POS_BOTTOMLEFT   => t('Bottom left'),
+  );
+
+  $form['watermark'][WATERMARK_LOCATION] = array(
+    '#type' => 'select',
+    '#title' => t('Location'),
+    '#options' => $locations,
+    '#default_value' => variable_get(WATERMARK_LOCATION, WATERMARK_POS_MIDDLE),
+    '#description' => t('Location on the final image where the watermark will be placed.'),
+  );
+
+  $form['watermark']['scaling'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Watermark scaling'),
+    '#collapsible' => true,
+    '#collapsed' => true,
+  );
+
+  $form['watermark']['scaling'][WATERMARK_SCALING] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Scale watermark'),
+    '#description' => t('Use scaling, if you want the watermark to be proportional to the image width. Please note that the watermark is scaled only, when it is too big.'),
+    '#default_value' => variable_get(WATERMARK_SCALING, false),
+  );
+
+  $form['watermark']['scaling'][WATERMARK_SCALING_PERCENTAGE] = array(
+    '#type' => 'textfield',
+    '#title' => t('Percentage'),
+    '#description' => t('Percentage to apply the watermark to the image.'),
+    '#size' => 3,
+    '#default_value' => variable_get(WATERMARK_SCALING_PERCENTAGE, 50),
+  );
+
+  $form['watermark']['scaling'][WATERMARK_MIN_WIDTH] = array(
+    '#type' => 'textfield',
+    '#title' => t('Minimum width'),
+    '#description' => t('The mimimum of the watermark in pixels. Type 0 to turn this option off.'),
+    '#size' => 3,
+    '#default_value' => variable_get(WATERMARK_MIN_WIDTH, 0),
+  );
+
+  $form['watermark']['labels'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Apply watermark to the following images'),
+    '#collapsible' => false,
+    '#description' => t('Check which images you want to apply watermarks for.'),
+  );
+
+  foreach (_image_get_sizes() as $size) {
+    $label_list[] = $size['label'];
+  }
+  $label_list[] = IMAGE_ORIGINAL;
+
+  foreach ($label_list as $label) {
+    $label_var = WATERMARK . $label;
+    $form['watermark']['labels'][$label_var] = array(
       '#type' => 'checkbox',
-      '#title' => t('Enable watermarking of new images?'),
-      '#default_value' => variable_get(WATERMARK_ENABLE, FALSE),
-      '#description' => t('Check this box to enable watermarking of new images.'),
-      );
-
-    $form['watermark'][WATERMARK_PATH] = array(
-      '#type' => 'textfield',
-      '#title' => t('Watermark overlay image'),
-      '#size' => 70,
-      '#maxlength' => 255,
-      '#required' => TRUE,
-      '#default_value' => variable_get(WATERMARK_PATH, ''),
-      '#description' => t('Path of the image that should be used as the watermark image, relative to the Drupal root. This should be an 8-bit PNG image (tested and verified), although other formats may work (untested).'),
-      );
-
-    $locations = array(
-      WATERMARK_POS_MIDDLE       => t('Middle'),
-      WATERMARK_POS_MIDDLERIGHT  => t('Middle right'),
-      WATERMARK_POS_MIDDLELEFT   => t('Middle left'),
-      WATERMARK_POS_TOPMIDDLE    => t('Top middle'),
-      WATERMARK_POS_TOPLEFT      => t('Top left'),
-      WATERMARK_POS_TOPRIGHT     => t('Top right'),
-      WATERMARK_POS_BOTTOMMIDDLE => t('Bottom middle'),
-      WATERMARK_POS_BOTTOMRIGHT  => t('Bottom right'),
-      WATERMARK_POS_BOTTOMLEFT   => t('Bottom left'),
+      '#title' => $label,
+      '#default_value' => variable_get($label_var, false),
     );
+  }
 
-    $form['watermark'][WATERMARK_LOCATION] = array(
-      '#type' => 'select',
-      '#title' => t('Location'),
-      '#options' => $locations,
-      '#default_value' => variable_get(WATERMARK_LOCATION, WATERMARK_POS_MIDDLE),
-      '#description' => t('Location on the final image where the watermark will be placed.'),
-    );
+  $gallery_enabled = module_exists('image_gallery');
+  $exclude_terms_description = t('Exclude image nodes with specific gallery terms from watermark process.');
+  $exclude_terms_description .= $gallery_enabled ? '' : ' '. t('Note: <strong>This option is disabled</strong> because Image Gallery module must be enabled first.');
+
+  $form['watermark']['toggles'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Toggles'),
+    '#collapsible' => true,
+    '#collapsed' => true,
+  );
+
+  $form['watermark']['toggles'][WATERMARK_EXCLUDE_TERMS] = array(
+    '#type' => 'select',
+    '#disabled' => !$gallery_enabled,
+    '#title' => t('Exclude terms'),
+    '#description' => $exclude_terms_description,
+    '#default_value' => variable_get(WATERMARK_EXCLUDE_TERMS, array()),
+    '#options' => _watermark_get_gallery_terms(),
+    '#multiple' => true,
+  );
 
-    $form['watermark']['labels'] = array(
-      '#type' => 'fieldset',
-      '#title' => t('Apply watermark to the following images'),
-      '#collapsible' => false,
-      '#description' => t('Check which images you want to apply watermarks for.'),
-    );
+  return system_settings_form($form);
+}
 
-    foreach (_image_get_sizes() as $size) {
-      $label_list[] = $size['label'];
-    }
-    $label_list[] = '_original';
+function watermark_admin_settings_validate($form_id, $form_values) {
+  if (!is_numeric($form_values[WATERMARK_SCALING_PERCENTAGE]) || $form_values[WATERMARK_SCALING_PERCENTAGE] <= 0 || $form_values[WATERMARK_SCALING_PERCENTAGE] >= 100) {
+    form_set_error(WATERMARK_SCALING_PERCENTAGE, t('Please enter a number between 1 and 100 for scaling percentage.'));
+  }
+  else if ($form_values[WATERMARK_SCALING_PERCENTAGE] < 10 || $form_values[WATERMARK_MIN_WIDTH] < 50) {
+    // we do not disable scaling for low values, e.g. if the user chooses to scale down to 1%, we just warn
+    drupal_set_message(t('Warning: Scaling is set to @percentage%, minimum width is only @min_width px. This may result in very small or almost invisible watermarks. It is recommended to check twice that an adequate minimum width has been set.',
+                       array('@percentage' => $form_values[WATERMARK_SCALING_PERCENTAGE], '@min_width' => $form_values[WATERMARK_MIN_WIDTH])));
+  }
 
-    foreach ($label_list as $label) {
-      $label_var = WATERMARK . $label;
-      $form['watermark']['labels'][$label_var] = array(
-        '#type' => 'checkbox',
-        '#title' => $label,
-        '#default_value' => variable_get($label_var, false),
-      );
-    }
+  if (intval($form_values[WATERMARK_MIN_WIDTH]) < 0) {
+    form_set_error(WATERMARK_MIN_WIDTH, t('Please enter a positive number for minimum width.'));
   }
 }
 
@@ -109,141 +191,218 @@ function watermark_check_functions() {
 
   $function_list[] = 'exif_imagetype';
   $function_list[] = 'imagecopy';
-  $function_list[] = 'imagejpeg';
+  $function_list[] = 'imagecopyresampled';
   $function_list[] = 'imagedestroy';
   $types = array('gif', 'jpeg', 'png', 'wbmp');
-  foreach($types as $type) {
-    $function_list[] = 'imagecreatefrom' . $type;
+  foreach ($types as $type) {
+    $function_list[] = 'image'. $type;
+    $function_list[] = 'imagecreatefrom'. $type;
   }
-  
-  foreach($function_list as $function) {
+
+  foreach ($function_list as $function) {
     if (!function_exists($function)) {
-      drupal_set_message("Function $function does not exist. Watermarking cannot be done. Please make sure that you are running PHP 4.3 or higher, or that you (or your hosting provider) manually compile the GD library into your PHP installation. You also need PHP to be compiled with --enable-exif.", 'error');
+      drupal_set_message('Function $function does not exist. Watermarking cannot be done. Please make sure that you are running PHP 4.3 or higher, or that you (or your hosting provider) manually compile the GD library into your PHP installation. You also need PHP to be compiled with --enable-exif.', 'error');
       $errors[] = 1;
     }
   }
 
-  if (count($errors)) {
+  if (!empty($errors)) {
     return false;
   }
 
   return true;
 }
 
-function watermark_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
+function watermark_nodeapi(&$node, $op, $teaser = null, $page = null) {
   if ($node->type != 'image') {
     return;
   }
 
-  if (!variable_get(WATERMARK_ENABLE, FALSE)) {
+  if (!variable_get(WATERMARK_ENABLE, false)) {
+    return;
+  }
+
+  // excluding node depends on the toggled options
+  if (_watermark_is_node_excluded($node)) {
     return;
   }
 
   $watermark = variable_get(WATERMARK_PATH, '');
-  $dir = './' . variable_get('file_directory_path', 'files') . '/';
+  // $dir removed --> http://drupal.org/node/142178
+  // see CHANGELOG.txt and TODO.txt
   $location = variable_get(WATERMARK_LOCATION, 0);
 
-  switch($op) {
+  switch ($op) {
+    case 'validate':
+      // watermark on preview!
+      // check for new image upload
+      if ($node->new_file && is_array($node->images)) {
+        foreach ($node->images as $label => $filepath) {
+          if (variable_get(WATERMARK . $label, false)) {
+            if (_watermark_process($filepath, $watermark, $location)) {
+              drupal_set_message(t('Watermark applied to image: %file', array('%file' => $label)));
+            }
+            else {
+              drupal_set_message(t('Error adding watermark.'), 'error');
+              watchdog('error', 'Error adding watermark');
+            }
+          }
+        }
+      }
+    break;
+
     case 'insert':
     case 'update':
-      // We get the image from the database to guarantee it is the correct path, not temp files
-      $result = db_query("SELECT filename, filepath FROM {files} WHERE nid = %d", $node->nid);
-      while($row = db_fetch_object($result)) {
-        if (variable_get(WATERMARK . $row->filename, false)) {
-          if (_watermark_process($dir . $row->filepath, $watermark, $location)) {
-            drupal_set_message(t('Watermark applied to image: %file', array( '%file' => $row->filename ) ) );
-          }
-          else {
-            drupal_set_message(t('Error adding watermark.'), 'error');
-            watchdog('error', 'Error adding watermark');
+      // new image upload check, when the node is saved without preview
+      if ($node->new_file && is_array($node->images)) {
+        // We get the image from the database to guarantee it is the correct path, not temp files
+        $result = db_query('SELECT filename, filepath FROM {files} WHERE nid = %d', $node->nid);
+        while ($row = db_fetch_object($result)) {
+          if (variable_get(WATERMARK . $row->filename, false)) {
+            if (_watermark_process($row->filepath, $watermark, $location)) {
+              drupal_set_message(t('Watermark applied to image: %file', array( '%file' => $row->filename ) ) );
+            }
+            else {
+              drupal_set_message(t('Error adding watermark.'), 'error');
+              watchdog('error', 'Error adding watermark');
+            }
           }
         }
       }
-      break;
+    break;
   }
 }
 
 function _watermark_process($image_path, $watermark_path, $location) {
+  // init return status
+  $status = false;
+
+  $scaling = variable_get(WATERMARK_SCALING, false);
 
-  if (!$wm = _watermark_make_image($watermark_path)) {
+  if (!$wm_info = _watermark_make_image($watermark_path)) {
     return false;
   }
+  $wm = $wm_info['handle'];
 
-  if (!$im = _watermark_make_image($image_path)) {
+  if (!$im_info = _watermark_make_image($image_path)) {
     return false;
   }
+  $im = $im_info['handle'];
 
-  $im_x = imagesx($im);
-  $im_y = imagesy($im);
+  if (!$im_info['truecolor']) {
+    // create truecolor image for alpha blending and better resampling
+    $new_im = imagecreatetruecolor($im_info['width'], $im_info['height']);
+    imagecopy($new_im, $im, 0, 0, 0, 0, $im_info['width'], $im_info['height']);
+    imagedestroy($im);
+    $im = $new_im;
+  }
+  imagealphablending($im, true);
+
+  $im_x = $im_info['width'];
+  $im_y = $im_info['height'];
+
+  if ($scaling) {
+    // rescale wm image to desired percentage of dest image WIDTH
+    $percentage = variable_get(WATERMARK_SCALING_PERCENTAGE, 50) / 100;
+    $min_width = intval(variable_get(WATERMARK_MIN_WIDTH, 0));
+    $desired_width = $im_x * $percentage;
+    if ($desired_width < $min_width) {
+      $desired_width = $min_width;
+    }
+    $scale_ratio = $desired_width / $wm_info['width'];
 
-  $wm_x = imagesx($wm);
-  $wm_y = imagesy($wm);
+    // new size
+    $wm_x = $wm_info['width'] * $scale_ratio;
+    $wm_y = $wm_info['height'] * $scale_ratio;
+  }
+  else {
+    $wm_x = $wm_info['width'];
+    $wm_y = $wm_info['height'];
+  }
 
-  switch($location) {
+  switch ($location) {
     case WATERMARK_POS_MIDDLE:
-      //middle 
-      $dest_x = ($im_x / 2) - ($wm_x / 2); 
-      $dest_y = ($im_y / 2) - ($wm_y / 2); 
+      //middle
+      $dest_x = ($im_x / 2) - ($wm_x / 2);
+      $dest_y = ($im_y / 2) - ($wm_y / 2);
       break;
     case WATERMARK_POS_MIDDLERIGHT:
-      //middle right 
-      $dest_x = $im_x - $wm_x; 
-      $dest_y = ($im_y / 2) - ($wm_y / 2); 
+      //middle right
+      $dest_x = $im_x - $wm_x;
+      $dest_y = ($im_y / 2) - ($wm_y / 2);
       break;
     case WATERMARK_POS_MIDDLELEFT:
-      //middle left 
-      $dest_x = 0; 
-      $dest_y = ($im_y / 2) - ($wm_y / 2); 
+      //middle left
+      $dest_x = 0;
+      $dest_y = ($im_y / 2) - ($wm_y / 2);
       break;
     case WATERMARK_POS_TOPMIDDLE:
-      //top middle 
-      $dest_x = (($im_x - $wm_x) / 2); 
-      $dest_y = 0; 
+      //top middle
+      $dest_x = (($im_x - $wm_x) / 2);
+      $dest_y = 0;
       break;
     case WATERMARK_POS_TOPLEFT:
-      //top left 
-      $dest_x = 0; 
-      $dest_y = 0; 
+      //top left
+      $dest_x = 0;
+      $dest_y = 0;
       break;
     case WATERMARK_POS_TOPRIGHT:
-      //top right 
-      $dest_x = $im_x - $wm_x; 
-      $dest_y = 0; 
+      //top right
+      $dest_x = $im_x - $wm_x;
+      $dest_y = 0;
       break;
     case WATERMARK_POS_BOTTOMMIDDLE:
-      //bottom middle 
-      $dest_x = (($im_x - $wm_x) / 2); 
-      $dest_y = $im_y - $wm_y; 
+      //bottom middle
+      $dest_x = (($im_x - $wm_x) / 2);
+      $dest_y = $im_y - $wm_y;
       break;
     case WATERMARK_POS_BOTTOMRIGHT:
-      //bottom right 
-      $dest_x = $im_x - $wm_x; 
-      $dest_y = $im_y - $wm_y; 
+      //bottom right
+      $dest_x = $im_x - $wm_x;
+      $dest_y = $im_y - $wm_y;
       break;
     case WATERMARK_POS_BOTTOMLEFT:
-      //bottom left 
-      $dest_x = 0; 
-      $dest_y = $im_y - $wm_y; 
+      //bottom left
+      $dest_x = 0;
+      $dest_y = $im_y - $wm_y;
       break;
-    } 
-
-  if (!imagecopy($im, $wm, $dest_x, $dest_y, 0, 0, $wm_x, $wm_y)) {
-    drupal_set_message(t("Failed to merge image with watermark."), 'error');
-    return false;
   }
 
-  if (!imagejpeg($im, $image_path)) {
-    drupal_set_message(t("Failed to save merged image."), 'error');
-    return false;
+  // image create function according to original image type
+  $img_create_func = 'image'. $im_info['type'];
+
+  // scaling on - resampling image
+  if ($scaling &&
+      !imagecopyresampled($im, $wm, $dest_x, $dest_y, 0, 0, $wm_x, $wm_y, $wm_info['width'], $wm_info['height'])) {
+      $msg = t('Failed to merge image with watermark.');
+      drupal_set_message($msg, 'error');
+      watchdog(WATERMARK_WATCHDOG, $msg, WATCHDOG_ERROR);
+  }
+  // scaling off
+  else if (!$scaling &&
+           !imagecopy($im, $wm, $dest_x, $dest_y, 0, 0, $wm_x, $wm_y)) {
+    $msg = t('Failed to merge image with watermark.');
+    drupal_set_message($msg, 'error');
+    watchdog(WATERMARK_WATCHDOG, $msg, WATCHDOG_ERROR);
+  }
+  // create an image of the same type!
+  else if (!$img_create_func($im, $image_path)) {
+    $msg = t('Failed to save merged image.');
+    drupal_set_message($msg, 'error');
+    watchdog(WATERMARK_WATCHDOG, $msg, WATCHDOG_ERROR);
+  }
+  else {
+    $status = true;
   }
 
   imagedestroy($im);
   imagedestroy($wm);
 
-  return true;
+  return $status;
 }
 
 function _watermark_make_image($file) {
+
   if (!file_exists($file)) {
     drupal_set_message(t('Image file %file not found.', array('%file' => $file ) ), 'error');
     return false;
@@ -251,7 +410,7 @@ function _watermark_make_image($file) {
 
   $type = exif_imagetype($file);
 
-  switch($type) {
+  switch ($type) {
     case IMAGETYPE_GIF:
       $type = 'gif';
       break;
@@ -270,7 +429,7 @@ function _watermark_make_image($file) {
       return false;
   }
 
-  $function = 'imagecreatefrom' . $type;
+  $function = 'imagecreatefrom'. $type;
   $handle = $function($file);
 
   if (!$handle) {
@@ -278,6 +437,77 @@ function _watermark_make_image($file) {
     return false;
   }
 
-  return $handle;
+  // create image info
+  $img_info = array(
+    'type' => $type,
+    'truecolor' => imageistruecolor($handle),
+    'colors' => imagecolorstotal($handle),
+    'handle' => $handle,
+    'width'  => imagesx($handle),
+    'height' => imagesy($handle),
+  );
+
+  return $img_info;
+}
+
+function _watermark_get_gallery_terms() {
+  $gallery_terms = array();
+
+  $vid = _watermark_get_gallery_vid();
+  $term_data = db_query('SELECT tid, name FROM {term_data} WHERE vid=%d', $vid);
+  while ($row = db_fetch_array($term_data)) {
+    $gallery_terms[$row['tid']] = $row['name'];
+  }
+
+  return $gallery_terms;
 }
 
+function _watermark_get_excluded_tids() {
+  $excluded_tids = array();
+  foreach (variable_get(WATERMARK_EXCLUDE_TERMS, array()) as $tid => $term) {
+    $excluded_tids[] = $tid;
+  }
+  return $excluded_tids;
+}
+
+function _watermark_get_gallery_vid() {
+
+  // code from image gallery module
+
+  $vid = variable_get('image_gallery_nav_vocabulary', '');
+  if (empty($vid) || !taxonomy_get_vocabulary($vid)) {
+    // Check to see if an image gallery vocabulary exists
+    $vid = db_result(db_query('SELECT vid FROM {vocabulary} WHERE module=%s', 'image_gallery'));
+    if (!$vid) {
+
+      // change: we don't want to create any vocabulary here
+      return false;
+    }
+    variable_set('image_gallery_nav_vocabulary', $vid);
+  }
+
+  return $vid;
+}
+
+function _watermark_is_node_excluded($node) {
+  // for now we have only one toggle:
+  // - exclude nodes with specified vocabulary terms from watermark process
+
+  if (!empty($node->taxonomy)) {
+    $excluded_tids = _watermark_get_excluded_tids();
+    $gallery_vid = _watermark_get_gallery_vid();
+
+    foreach ($node->taxonomy as $vid => $tids) {
+      if ($vid == $gallery_vid) {
+
+        foreach ($tids as $tid => $status) {
+          if ($status!=0 && in_array($tid, $excluded_tids)) {
+            // at least one term matches excluded terms
+            return true;
+          }
+        }
+      }
+    }
+  }
+  return false;
+}
\ No newline at end of file
Only in ./: watermark.multi-issue.patch
