source: branches/2.4/admin/include/configuration_watermark_process.inc.php @ 17295

Last change on this file since 17295 was 17295, checked in by rvelices, 12 years ago

feature 2708: in admin, display allowed custom derivatives and ability to delete them

File size: 5.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if( !defined("PHPWG_ROOT_PATH") )
25{
26  die ("Hacking attempt!");
27}
28
29$errors = array();
30$pwatermark = $_POST['w'];
31
32// step 0 - manage upload if any
33if (isset($_FILES['watermarkImage']) and !empty($_FILES['watermarkImage']['tmp_name']))
34{
35  list($width, $height, $type) = getimagesize($_FILES['watermarkImage']['tmp_name']);
36  if (IMAGETYPE_PNG != $type)
37  {
38    $errors['watermarkImage'] = sprintf(
39      l10n('Allowed file types: %s.'),
40      'PNG'
41      );
42  }
43  else
44  {
45    $upload_dir = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks';
46
47    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
48    prepare_directory($upload_dir);
49
50    $new_name = get_filename_wo_extension($_FILES['watermarkImage']['name']).'.png';
51    $file_path = $upload_dir.'/'.$new_name;
52
53    move_uploaded_file($_FILES['watermarkImage']['tmp_name'], $file_path);
54
55    $pwatermark['file'] = substr($file_path, strlen(PHPWG_ROOT_PATH));
56  }
57}
58
59// step 1 - sanitize HTML input
60switch ($pwatermark['position'])
61{
62  case 'topleft':
63  {
64    $pwatermark['xpos'] = 0;
65    $pwatermark['ypos'] = 0;
66    break;
67  }
68  case 'topright':
69  {
70    $pwatermark['xpos'] = 100;
71    $pwatermark['ypos'] = 0;
72    break;
73  }
74  case 'middle':
75  {
76    $pwatermark['xpos'] = 50;
77    $pwatermark['ypos'] = 50;
78    break;
79  }
80  case 'bottomleft':
81  {
82    $pwatermark['xpos'] = 0;
83    $pwatermark['ypos'] = 100;
84    break;
85  }
86  case 'bottomright':
87  {
88    $pwatermark['xpos'] = 100;
89    $pwatermark['ypos'] = 100;
90    break;
91  }
92}
93
94// step 2 - check validity
95$v = intval($pwatermark['xpos']);
96if ($v < 0 or $v > 100)
97{
98  $errors['watermark']['xpos'] = '[0..100]';
99}
100
101$v = intval($pwatermark['ypos']);
102if ($v < 0 or $v > 100)
103{
104  $errors['watermark']['ypos'] = '[0..100]';
105}
106
107$v = intval($pwatermark['opacity']);
108if ($v <= 0 or $v > 100)
109{
110  $errors['watermark']['opacity'] = '(0..100]';
111}
112
113// step 3 - save data
114if (count($errors) == 0)
115{
116  $watermark = new WatermarkParams();
117  $watermark->file = $pwatermark['file'];
118  $watermark->xpos = intval($pwatermark['xpos']);
119  $watermark->ypos = intval($pwatermark['ypos']);
120  $watermark->xrepeat = intval($pwatermark['xrepeat']);
121  $watermark->opacity = intval($pwatermark['opacity']);
122  $watermark->min_size = array(intval($pwatermark['minw']),intval($pwatermark['minh']));
123
124  $old_watermark = ImageStdParams::get_watermark();
125  $watermark_changed =
126    $watermark->file != $old_watermark->file
127    || $watermark->xpos != $old_watermark->xpos
128    || $watermark->ypos != $old_watermark->ypos
129    || $watermark->xrepeat != $old_watermark->xrepeat
130    || $watermark->opacity != $old_watermark->opacity;
131
132  // save the new watermark configuration
133  ImageStdParams::set_watermark($watermark);
134
135  // do we have to regenerate the derivatives (and which types)?
136  $changed_types = array();
137
138  foreach (ImageStdParams::get_defined_type_map() as $type => $params)
139  {
140    $old_use_watermark = $params->use_watermark;
141    ImageStdParams::apply_global($params);
142
143    $changed = $params->use_watermark != $old_use_watermark;
144    if (!$changed and $params->use_watermark)
145    {
146      $changed = $watermark_changed;
147    }
148    if (!$changed and $params->use_watermark)
149    {
150      // if thresholds change and before/after the threshold is lower than the corresponding derivative side -> some derivatives might switch the watermark
151      $changed |= $watermark->min_size[0]!=$old_watermark->min_size[0] and ($watermark->min_size[0]<$params->max_width() or $old_watermark->min_size[0]<$params->max_width());
152      $changed |= $watermark->min_size[1]!=$old_watermark->min_size[1] and ($watermark->min_size[1]<$params->max_height() or $old_watermark->min_size[1]<$params->max_height());
153    }
154
155    if ($changed)
156    {
157      $params->last_mod_time = time();
158      $changed_types[] = $type;
159    }
160  }
161
162  ImageStdParams::save();
163
164  if (count($changed_types))
165  {
166    clear_derivative_cache($changed_types);
167  }
168
169  array_push(
170    $page['infos'],
171    l10n('Your configuration settings are saved')
172    );
173}
174else
175{
176  $template->assign('watermark', $pwatermark);
177  $template->assign('ferrors', $errors);
178}
179?>
Note: See TracBrowser for help on using the repository browser.