Watermarks not applied to selected built-in image derivatives due to mismatch between variable names and node->images keys

grateful_drupal_user - July 15, 2007 - 23:32
Project:Image watermark
Version:5.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

On my freshly installed Drupal 5 system, there seems to be a mismatch between the $node->images key name and that expected by the watermark module when processing watermarks. The watermark module seems to be keying off the derivative 'labels' rather than the actual derivative values stored in the files table and loaded into $node->images by the image.module. This causes the watermark module to fail to apply watermarks to the builtin derivatives (_original, thumbnail, and preview) if they are selected.

<?php
// $Id: image.module,v 1.209.2.36 2007/07/06 15:22:19 drewish Exp $
// $Id: watermark.module,v 1.5.2.5 2007/07/07 17:53:29 kbahey Exp $
?>

If I dump the value of $node->images in _watermark_apply:

<?php
function _watermark_apply($node) {
 
$watermark = variable_get(WATERMARK_PATH, '');
 
$location = variable_get(WATERMARK_LOCATION, 0);
  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');
      }
    }
  }
}
?>

The results show that the keys for the derivatives are not the derivative labels used by the watermark module's admin configuration code:

<?php
Array
(
    [
_original] => sites/default/files/images/laguna seca motogp 2003 064.jpg
   
[thumbnail] => sites/default/files/images/laguna seca motogp 2003 064.thumbnail.jpg
   
[preview] => sites/default/files/images/laguna seca motogp 2003 064.jpg
   
[Small] => sites/default/files/images/laguna seca motogp 2003 064.Small.jpg
)
?>

In order to work around the problem, I modified the watermark module thusly, and it now applies watermarks to all selected derivatives, including the built-in image module derivatives.

--- watermark.module.original 2007-07-15 15:08:35.000000000 -0400
+++ watermark.module 2007-07-15 19:16:44.000000000 -0400
@@ -137,10 +137,10 @@
     '#description' => t('Check which images you want to apply watermarks for.'),
   );

-  foreach (_image_get_sizes() as $size) {
-    $label_list[] = $size['label'];
+  foreach (_image_get_sizes() as $key => $size) {
+    $label_list[] = $key; // $size['label'];
   }
-  $label_list[] = IMAGE_ORIGINAL;
+//  $label_list[] = IMAGE_ORIGINAL;

   foreach ($label_list as $label) {
     $label_var = WATERMARK . $label;
@@ -286,7 +286,9 @@
function _watermark_apply($node) {
   $watermark = variable_get(WATERMARK_PATH, '');
   $location = variable_get(WATERMARK_LOCATION, 0);
+  // _w_dbg($node->images);
   foreach ($node->images as $label => $filepath) {
+    // drupal_set_message(WATERMARK . $label);
     if (variable_get(WATERMARK . $label, false)) {
       if (_watermark_process($filepath, $watermark, $location)) {
         drupal_set_message(t('Watermark applied to image: %file', array('%file' => $label)));
@@ -535,7 +537,7 @@
         }
       }
       // it's not a multiple select vocabulary
-      else if (!empty($tids) && $vid == $gallery_vid && in_array($tids, $excluded_tids)) {
+      else if (false /*!empty($tids) && $vid == $gallery_vid && in_array($tids, $excluded_tids)*/) {
         // excluded term found
         return true;
       }
@@ -543,3 +545,8 @@
   }
   return false;
}
+
+function _w_dbg($thing, $heading=NULL) {
+  drupal_set_message("<strong>$heading</strong><pre>".htmlspecialchars(print_r($thing, true)).'</pre>');
+}
+

#1

grateful_drupal_user - July 15, 2007 - 23:37

Oops, sorry, that quick and dirty patch included my workarounds for http://drupal.org/node/159543 as well. Here are the pertinent changes for this issue:

--- watermark.module.original 2007-07-15 15:08:35.000000000 -0400
+++ watermark.module 2007-07-15 19:16:44.000000000 -0400
@@ -137,10 +137,10 @@
     '#description' => t('Check which images you want to apply watermarks for.'),
   );

-  foreach (_image_get_sizes() as $size) {
-    $label_list[] = $size['label'];
+  foreach (_image_get_sizes() as $key => $size) {
+    $label_list[] = $key; // $size['label'];
   }
-  $label_list[] = IMAGE_ORIGINAL;
+//  $label_list[] = IMAGE_ORIGINAL;

   foreach ($label_list as $label) {
     $label_var = WATERMARK . $label;
@@ -286,7 +286,9 @@
function _watermark_apply($node) {
   $watermark = variable_get(WATERMARK_PATH, '');
   $location = variable_get(WATERMARK_LOCATION, 0);
+  // _w_dbg($node->images);
   foreach ($node->images as $label => $filepath) {
+    // drupal_set_message(WATERMARK . $label);
     if (variable_get(WATERMARK . $label, false)) {
       if (_watermark_process($filepath, $watermark, $location)) {
         drupal_set_message(t('Watermark applied to image: %file', array('%file' => $label)));

Note that I have not attached a patch file as I do not consider this a fix, rather, the code is intended to help demonstrate the nature of the problem - the watermark module uses the display labels as the variable names in the variable table ("watermark_Original", "watermark_Preview", etc.) but the $node->images[] keys are the internal name ("_original", "preview", etc.) and thus the logic fails in function _watermark_apply()

#2

schnizZzla - July 19, 2007 - 13:07

Thank you for the details.

I'm going to investigate and trying to fix that on this weekend.

#3

schnizZzla - July 29, 2007 - 03:14

Unfortunately, I didn't have the time last weekend to take on this problem. The latest watermark version was optimized for image 1.2. After other small patches, not applied here, I've finally brought it online. Now I'm finally using watermark, but without the taxonomy toggle. It works fine, so far.

The first time I've tried the module with a newer version than 1.2, it didn't work at all. I saw you were also using 1.2, but I think these are some basic problems, that need to be solved. All this taxonomy stuff, I did that using "debug mode", means printing out node taxonomy, but without deeper knowledge about taxonomy represantation...

As long this is a DEV version, I think watermark should be compatible with the latest version of Image module. As soon as I have the time, I'm going to setup a demo page with the latest drupal and image version, get it working and on that way discovering what the problems really are.

#4

briandwest - May 17, 2008 - 17:52

I had the same problems as described above. The only form of my images that would watermark was "_original". I only needed to apply (manually) the first half of the above patch code, then re-check the newly-named checkboxes in the config page to get my watermarking to work. Thumbs up on the patch!

#5

kbahey - May 17, 2008 - 18:47
Status:active» needs work

In order to include this in the released version, some volunteer has to provide a proper tested patch in the correct format. See http://drupal.org/patch on how to do this. Attach it here (do not paste it).

#6

grateful_drupal_user - May 17, 2008 - 20:11
Status:needs work» needs review

ok, here's my patch.

AttachmentSize
watermark-derivatives.patch 817 bytes

#7

kbahey - May 17, 2008 - 20:38
Status:needs review» fixed

Committed.

Thank you.

#8

Anonymous (not verified) - May 31, 2008 - 20:42
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.