/[drupal]/contributions/modules/watermark/watermark.module
ViewVC logotype

Diff of /contributions/modules/watermark/watermark.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.9, Sat Jul 7 17:54:09 2007 UTC revision 1.10, Mon Jun 23 23:20:38 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3  // $Id: watermark.module,v 1.5.2.5 2007/07/07 17:53:29 kbahey Exp $  // $Id: watermark.module,v 1.5.2.6 2008/05/17 20:38:05 kbahey Exp $
4  // Author: Khalid Baheyeldin of http://2bits.com  // Author: Khalid Baheyeldin of http://2bits.com
5  // Additional features by schnizZzla for http://BerlinerStrassen.com  // Additional features by schnizZzla for http://BerlinerStrassen.com
6    
# Line 26  define('WATERMARK_POS_BOTTOMLEFT', Line 26  define('WATERMARK_POS_BOTTOMLEFT',
26    
27  define('WATERMARK_PERM_MANAGE',           'manage watermarks');  define('WATERMARK_PERM_MANAGE',           'manage watermarks');
28    
29  function watermark_help($section) {  function watermark_help($path, $arg) {
30    switch ($section) {    switch ($path) {
31      case 'admin/help#description':      case 'admin/help#description':
32        return t('Adds watermark to image.module. Requires image.module');        return t('Adds watermark to image.module. Requires image.module');
33    }    }
34  }  }
35    
36  function watermark_menu($may_cache) {  function watermark_menu() {
37    $items = array();    $items = array();
38    $items[] = array(    $items['admin/settings/image_watermark'] = array(
39      'path' => 'admin/settings/image_watermark',      'title' => 'Image Watermark',
40      'title' => t('Image Watermark'),      'description' => 'Configure image watermark settings.',
41      'description' => t('Configure image watermark settings.'),      'page callback' => 'drupal_get_form',
42      'callback' => 'drupal_get_form',      'page arguments' => array('watermark_admin_settings'),
43      'callback arguments' => array('watermark_admin_settings'),      'access arguments' => array('administer images'),
     'access' => user_access('administer images'),  
44      'type' => MENU_NORMAL_ITEM,      'type' => MENU_NORMAL_ITEM,
45    );    );
46    return $items;    return $items;
# Line 59  function watermark_admin_settings() { Line 58  function watermark_admin_settings() {
58    $form['watermark'] = array(    $form['watermark'] = array(
59      '#type' => 'fieldset',      '#type' => 'fieldset',
60      '#title' => t('Watermark'),      '#title' => t('Watermark'),
61      '#collapsible' => true,      '#collapsible' => TRUE,
62      '#weight' => -1,      '#weight' => -1,
63    );    );
64    
65    $form['watermark'][WATERMARK_ENABLE] = array(    $form['watermark'][WATERMARK_ENABLE] = array(
66      '#type' => 'checkbox',      '#type' => 'checkbox',
67      '#title' => t('Enable watermarking of new images?'),      '#title' => t('Enable watermarking of new images?'),
68      '#default_value' => variable_get(WATERMARK_ENABLE, false),      '#default_value' => variable_get(WATERMARK_ENABLE, FALSE),
69      '#description' => t('Check this box to enable watermarking of new images.'),      '#description' => t('Check this box to enable watermarking of new images.'),
70      );      );
71    
# Line 75  function watermark_admin_settings() { Line 74  function watermark_admin_settings() {
74      '#title' => t('Watermark overlay image'),      '#title' => t('Watermark overlay image'),
75      '#size' => 70,      '#size' => 70,
76      '#maxlength' => 255,      '#maxlength' => 255,
77      '#required' => true,      '#required' => TRUE,
78      '#default_value' => variable_get(WATERMARK_PATH, ''),      '#default_value' => variable_get(WATERMARK_PATH, ''),
79      '#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.'),      '#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.'),
80      );      );
# Line 103  function watermark_admin_settings() { Line 102  function watermark_admin_settings() {
102    $form['watermark']['scaling'] = array(    $form['watermark']['scaling'] = array(
103      '#type' => 'fieldset',      '#type' => 'fieldset',
104      '#title' => t('Watermark scaling'),      '#title' => t('Watermark scaling'),
105      '#collapsible' => true,      '#collapsible' => TRUE,
106      '#collapsed' => true,      '#collapsed' => TRUE,
107    );    );
108    
109    $form['watermark']['scaling'][WATERMARK_SCALING] = array(    $form['watermark']['scaling'][WATERMARK_SCALING] = array(
110      '#type' => 'checkbox',      '#type' => 'checkbox',
111      '#title' => t('Scale watermark'),      '#title' => t('Scale watermark'),
112      '#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.'),      '#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.'),
113      '#default_value' => variable_get(WATERMARK_SCALING, false),      '#default_value' => variable_get(WATERMARK_SCALING, FALSE),
114    );    );
115    
116    $form['watermark']['scaling'][WATERMARK_SCALING_PERCENTAGE] = array(    $form['watermark']['scaling'][WATERMARK_SCALING_PERCENTAGE] = array(
# Line 133  function watermark_admin_settings() { Line 132  function watermark_admin_settings() {
132    $form['watermark']['labels'] = array(    $form['watermark']['labels'] = array(
133      '#type' => 'fieldset',      '#type' => 'fieldset',
134      '#title' => t('Apply watermark to the following images'),      '#title' => t('Apply watermark to the following images'),
135      '#collapsible' => false,      '#collapsible' => FALSE,
136      '#description' => t('Check which images you want to apply watermarks for.'),      '#description' => t('Check which images you want to apply watermarks for.'),
137    );    );
138    
139    foreach (_image_get_sizes() as $size) {    foreach (_image_get_sizes() as $id => $size) {
140      $label_list[] = $size['label'];      $label_var = WATERMARK . $id;
   }  
   $label_list[] = IMAGE_ORIGINAL;  
   
   foreach ($label_list as $label) {  
     $label_var = WATERMARK . $label;  
141      $form['watermark']['labels'][$label_var] = array(      $form['watermark']['labels'][$label_var] = array(
142        '#type' => 'checkbox',        '#type' => 'checkbox',
143        '#title' => $label,        '#title' => $size['label'] ." ($id)",
144        '#default_value' => variable_get($label_var, false),        '#default_value' => variable_get($label_var, FALSE),
145      );      );
146    }    }
147    
# Line 158  function watermark_admin_settings() { Line 152  function watermark_admin_settings() {
152    $form['watermark']['toggles'] = array(    $form['watermark']['toggles'] = array(
153      '#type' => 'fieldset',      '#type' => 'fieldset',
154      '#title' => t('Toggles'),      '#title' => t('Toggles'),
155      '#collapsible' => true,      '#collapsible' => TRUE,
156      '#collapsed' => true,      '#collapsed' => TRUE,
157    );    );
158    
159    $form['watermark']['toggles'][WATERMARK_EXCLUDE_TERMS] = array(    $form['watermark']['toggles'][WATERMARK_EXCLUDE_TERMS] = array(
# Line 169  function watermark_admin_settings() { Line 163  function watermark_admin_settings() {
163      '#description' => $exclude_terms_description,      '#description' => $exclude_terms_description,
164      '#default_value' => variable_get(WATERMARK_EXCLUDE_TERMS, array()),      '#default_value' => variable_get(WATERMARK_EXCLUDE_TERMS, array()),
165      '#options' => _watermark_get_gallery_terms(),      '#options' => _watermark_get_gallery_terms(),
166      '#multiple' => true,      '#multiple' => TRUE,
167    );    );
168    
169    return system_settings_form($form);    return system_settings_form($form);
170  }  }
171    
172  function watermark_admin_settings_validate($form_id, $form_values) {  function watermark_admin_settings_validate($form, &$form_state) {
173    if (!is_numeric($form_values[WATERMARK_SCALING_PERCENTAGE]) || $form_values[WATERMARK_SCALING_PERCENTAGE] <= 0 || $form_values[WATERMARK_SCALING_PERCENTAGE] >= 100) {    if (!is_numeric($form_state['values'][WATERMARK_SCALING_PERCENTAGE]) || $form_state['values'][WATERMARK_SCALING_PERCENTAGE] <= 0 || $form_state['values'][WATERMARK_SCALING_PERCENTAGE] >= 100) {
174      form_set_error(WATERMARK_SCALING_PERCENTAGE, t('Please enter a number between 1 and 100 for scaling percentage.'));      form_set_error(WATERMARK_SCALING_PERCENTAGE, t('Please enter a number between 1 and 100 for scaling percentage.'));
175    }    }
176    else if ($form_values[WATERMARK_SCALING_PERCENTAGE] < 10 || $form_values[WATERMARK_MIN_WIDTH] < 50) {    else if ($form_state['values'][WATERMARK_SCALING_PERCENTAGE] < 10 || $form_state['values'][WATERMARK_MIN_WIDTH] < 50) {
177      // we do not disable scaling for low values, e.g. if the user chooses to scale down to 1%, we just warn      // we do not disable scaling for low values, e.g. if the user chooses to scale down to 1%, we just warn
178      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.',      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.',
179                         array('@percentage' => $form_values[WATERMARK_SCALING_PERCENTAGE], '@min_width' => $form_values[WATERMARK_MIN_WIDTH])));                         array('@percentage' => $form_state['values'][WATERMARK_SCALING_PERCENTAGE], '@min_width' => $form_state['values'][WATERMARK_MIN_WIDTH])));
180    }    }
181    
182    if (intval($form_values[WATERMARK_MIN_WIDTH]) < 0) {    if (intval($form_state['values'][WATERMARK_MIN_WIDTH]) < 0) {
183      form_set_error(WATERMARK_MIN_WIDTH, t('Please enter a positive number for minimum width.'));      form_set_error(WATERMARK_MIN_WIDTH, t('Please enter a positive number for minimum width.'));
184    }    }
185  }  }
# Line 213  function watermark_check_functions() { Line 207  function watermark_check_functions() {
207    }    }
208    
209    if (!empty($errors)) {    if (!empty($errors)) {
210      return false;      return FALSE;
211    }    }
212    
213    return true;    return TRUE;
214  }  }
215    
216  function watermark_nodeapi(&$node, $op, $teaser = null, $page = null) {  function watermark_nodeapi(&$node, $op, $teaser = null, $page = null) {
# Line 224  function watermark_nodeapi(&$node, $op, Line 218  function watermark_nodeapi(&$node, $op,
218      return;      return;
219    }    }
220    
221    if (!variable_get(WATERMARK_ENABLE, false)) {    if (!variable_get(WATERMARK_ENABLE, FALSE)) {
222      return;      return;
223    }    }
224    
# Line 256  function watermark_nodeapi(&$node, $op, Line 250  function watermark_nodeapi(&$node, $op,
250    }    }
251  }  }
252    
253  function watermark_form_alter($form_id, &$form) {  function watermark_form_alter(&$form, &$form_state, $form_id) {
254    // additional option to toggle watermark process when a user has "manage watermarks" permission    // additional option to toggle watermark process when a user has "manage watermarks" permission
255    if ($form_id == 'image_node_form' && user_access(WATERMARK_PERM_MANAGE)) {    if ($form_id == 'image_node_form' && user_access(WATERMARK_PERM_MANAGE)) {
256      $apply_watermark = true;      $apply_watermark = TRUE;
257    
258      if (preg_match('/^\/node\/[0-9]+\/edit$/i', $form['#action'])) {      if (preg_match('/^\/node\/[0-9]+\/edit$/i', $form['#action'])) {
259        $apply_watermark = false;        $apply_watermark = FALSE;
260      }      }
261      else if ($form['#action'] != '/node/add/image') {      else if ($form['#action'] != '/node/add/image') {
262        return;        return;
# Line 271  function watermark_form_alter($form_id, Line 265  function watermark_form_alter($form_id,
265      $form['watermark'] = array(      $form['watermark'] = array(
266        '#type' => 'fieldset',        '#type' => 'fieldset',
267        '#title' => t('Watermark settings'),        '#title' => t('Watermark settings'),
268        '#collapsible' => true,        '#collapsible' => TRUE,
269        '#collapsed' => false,        '#collapsed' => FALSE,
270      );      );
271      $form['watermark']['wm_apply'] = array(      $form['watermark']['wm_apply'] = array(
272        '#title' => t('Apply watermark'),        '#title' => t('Apply watermark'),
# Line 286  function watermark_form_alter($form_id, Line 280  function watermark_form_alter($form_id,
280  function _watermark_apply($node) {  function _watermark_apply($node) {
281    $watermark = variable_get(WATERMARK_PATH, '');    $watermark = variable_get(WATERMARK_PATH, '');
282    $location = variable_get(WATERMARK_LOCATION, 0);    $location = variable_get(WATERMARK_LOCATION, 0);
283    foreach ($node->images as $label => $filepath) {    $sizes = _image_get_sizes();
284      if (variable_get(WATERMARK . $label, false)) {    foreach ($node->images as $size => $filepath) {
285        if (variable_get(WATERMARK . $size, FALSE)) {
286        if (_watermark_process($filepath, $watermark, $location)) {        if (_watermark_process($filepath, $watermark, $location)) {
287          drupal_set_message(t('Watermark applied to image: %file', array('%file' => $label)));          drupal_set_message(t('Watermark applied to image: %file', array('%file' => $sizes[$size]['label'])));
288        }        }
289        else {        else {
290          drupal_set_message(t('Error adding watermark.'), 'error');          drupal_set_message(t('Error adding watermark.'), 'error');
# Line 301  function _watermark_apply($node) { Line 296  function _watermark_apply($node) {
296    
297  function _watermark_process($image_path, $watermark_path, $location) {  function _watermark_process($image_path, $watermark_path, $location) {
298    // init return status    // init return status
299    $status = false;    $status = FALSE;
300    
301    $scaling = variable_get(WATERMARK_SCALING, false);    $scaling = variable_get(WATERMARK_SCALING, FALSE);
302    
303    if (!$wm_info = _watermark_make_image($watermark_path)) {    if (!$wm_info = _watermark_make_image($watermark_path)) {
304      return false;      return FALSE;
305    }    }
306    $wm = $wm_info['handle'];    $wm = $wm_info['handle'];
307    
308    if (!$im_info = _watermark_make_image($image_path)) {    if (!$im_info = _watermark_make_image($image_path)) {
309      return false;      return FALSE;
310    }    }
311    $im = $im_info['handle'];    $im = $im_info['handle'];
312    
# Line 322  function _watermark_process($image_path, Line 317  function _watermark_process($image_path,
317      imagedestroy($im);      imagedestroy($im);
318      $im = $new_im;      $im = $new_im;
319    }    }
320    imagealphablending($im, true);    imagealphablending($im, TRUE);
321    
322    $im_x = $im_info['width'];    $im_x = $im_info['width'];
323    $im_y = $im_info['height'];    $im_y = $im_info['height'];
# Line 418  function _watermark_process($image_path, Line 413  function _watermark_process($image_path,
413      watchdog(WATERMARK_WATCHDOG, $msg, WATCHDOG_ERROR);      watchdog(WATERMARK_WATCHDOG, $msg, WATCHDOG_ERROR);
414    }    }
415    else {    else {
416      $status = true;      $status = TRUE;
417    }    }
418    
419    imagedestroy($im);    imagedestroy($im);
# Line 431  function _watermark_make_image($file) { Line 426  function _watermark_make_image($file) {
426    
427    if (!file_exists($file)) {    if (!file_exists($file)) {
428      drupal_set_message(t('Image file %file not found.', array('%file' => $file ) ), 'error');      drupal_set_message(t('Image file %file not found.', array('%file' => $file ) ), 'error');
429      return false;      return FALSE;
430    }    }
431    
432    $type = exif_imagetype($file);    $type = exif_imagetype($file);
# Line 452  function _watermark_make_image($file) { Line 447  function _watermark_make_image($file) {
447      default:      default:
448        // Unsupported type        // Unsupported type
449        drupal_set_message(t('Image file %file is an unsupported format type=%type.', array( '%file' => $file, '%type' => $type ) ), 'error');        drupal_set_message(t('Image file %file is an unsupported format type=%type.', array( '%file' => $file, '%type' => $type ) ), 'error');
450        return false;        return FALSE;
451    }    }
452    
453    $function = 'imagecreatefrom'. $type;    $function = 'imagecreatefrom'. $type;
# Line 460  function _watermark_make_image($file) { Line 455  function _watermark_make_image($file) {
455    
456    if (!$handle) {    if (!$handle) {
457      drupal_set_message(t('Failed to create handle for image file %file via function %function.', array( '%file' => $file, '%function' => $function )), 'error');      drupal_set_message(t('Failed to create handle for image file %file via function %function.', array( '%file' => $file, '%function' => $function )), 'error');
458      return false;      return FALSE;
459    }    }
460    
461    // create image info    // create image info
# Line 501  function _watermark_get_gallery_vid() { Line 496  function _watermark_get_gallery_vid() {
496    // code from image gallery module    // code from image gallery module
497    
498    $vid = variable_get('image_gallery_nav_vocabulary', '');    $vid = variable_get('image_gallery_nav_vocabulary', '');
499    if (empty($vid) || !taxonomy_get_vocabulary($vid)) {    if (empty($vid) || !taxonomy_vocabulary_load($vid)) {
500      // Check to see if an image gallery vocabulary exists      // Check to see if an image gallery vocabulary exists
501      $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='image_gallery'"));      $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='image_gallery'"));
502      if (!$vid) {      if (!$vid) {
503    
504        // change: we don't want to create any vocabulary here        // change: we don't want to create any vocabulary here
505        return false;        return FALSE;
506      }      }
507      variable_set('image_gallery_nav_vocabulary', $vid);      variable_set('image_gallery_nav_vocabulary', $vid);
508    }    }
# Line 530  function _watermark_is_node_excluded($no Line 525  function _watermark_is_node_excluded($no
525          foreach ($tids as $tid => $status) {          foreach ($tids as $tid => $status) {
526            if ($status!=0 && in_array($tid, $excluded_tids)) {            if ($status!=0 && in_array($tid, $excluded_tids)) {
527              // at least one term matches excluded terms              // at least one term matches excluded terms
528              return true;              return TRUE;
529            }            }
530          }          }
531        }        }
532        // it's not a multiple select vocabulary        // it's not a multiple select vocabulary
533        else if (!empty($tids) && $vid == $gallery_vid && in_array($tids, $excluded_tids)) {        else if (!empty($tids) && $vid == $gallery_vid && in_array($tids, $excluded_tids)) {
534          // excluded term found          // excluded term found
535          return true;          return TRUE;
536        }        }
537      }      }
538    }    }
539    return false;    return FALSE;
540  }  }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

  ViewVC Help
Powered by ViewVC 1.1.2