source: trunk/admin/include/configuration_watermark_process.inc.php @ 14546

Last change on this file since 14546 was 14546, checked in by plg, 9 years ago

feature 2626: ability to upload a new watermark

File size: 5.7 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'] = 'PNG';
39  }
40  else
41  {
42    $upload_dir = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks';
43
44    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
45    prepare_directory($upload_dir);
46
47    $new_name = get_filename_wo_extension($_FILES['watermarkImage']['name']).'.png';
48    $file_path = $upload_dir.'/'.$new_name; 
49 
50    move_uploaded_file($_FILES['watermarkImage']['tmp_name'], $file_path);
51   
52    $pwatermark['file'] = substr($file_path, strlen(PHPWG_ROOT_PATH));
53  }
54}
55
56// step 1 - sanitize HTML input
57switch ($pwatermark['position'])
58{
59  case 'topleft':
60  {
61    $pwatermark['xpos'] = 0;
62    $pwatermark['ypos'] = 0;
63    break;
64  }
65  case 'topright':
66  {
67    $pwatermark['xpos'] = 100;
68    $pwatermark['ypos'] = 0;
69    break;
70  }
71  case 'middle':
72  {
73    $pwatermark['xpos'] = 50;
74    $pwatermark['ypos'] = 50;
75    break;
76  }
77  case 'bottomleft':
78  {
79    $pwatermark['xpos'] = 0;
80    $pwatermark['ypos'] = 100;
81    break;
82  }
83  case 'bottomright':
84  {
85    $pwatermark['xpos'] = 100;
86    $pwatermark['ypos'] = 100;
87    break;
88  }
89}
90
91// step 2 - check validity
92$v = intval($pwatermark['xpos']);
93if ($v < 0 or $v > 100)
94{
95  $errors['watermark']['xpos'] = '[0..100]';
96}
97
98$v = intval($pwatermark['ypos']);
99if ($v < 0 or $v > 100)
100{
101  $errors['watermark']['ypos'] = '[0..100]';
102}
103
104$v = intval($pwatermark['opacity']);
105if ($v <= 0 or $v > 100)
106{
107  $errors['watermark']['opacity'] = '(0..100]';
108}
109
110// step 3 - save data
111if (count($errors) == 0)
112{
113  $watermark = new WatermarkParams();
114  $watermark->file = $pwatermark['file'];
115  $watermark->xpos = intval($pwatermark['xpos']);
116  $watermark->ypos = intval($pwatermark['ypos']);
117  $watermark->xrepeat = intval($pwatermark['xrepeat']);
118  $watermark->opacity = intval($pwatermark['opacity']);
119  $watermark->min_size = array(intval($pwatermark['minw']),intval($pwatermark['minh']));
120
121  $old_watermark = ImageStdParams::get_watermark();
122  $watermark_changed =
123    $watermark->file != $old_watermark->file
124    || $watermark->xpos != $old_watermark->xpos
125    || $watermark->ypos != $old_watermark->ypos
126    || $watermark->xrepeat != $old_watermark->xrepeat
127    || $watermark->opacity != $old_watermark->opacity;
128
129  // do we have to regenerate the derivatives?
130  $old_enabled = ImageStdParams::get_defined_type_map();
131  // $disabled = @unserialize( @$conf['disabled_derivatives'] );
132  // if ($disabled===false)
133  // {
134  //   $disabled = array();
135  // }
136
137  // save the new watermark configuration
138  ImageStdParams::set_watermark($watermark);
139
140  $new_enabled = ImageStdParams::get_defined_type_map();
141 
142  $changed_types = array();
143
144  foreach(ImageStdParams::get_all_types() as $type)
145  {
146    if (isset($old_enabled[$type]))
147    {
148      $old_params = $old_enabled[$type];
149      // echo '<pre>old '.$type."\n"; print_r($old_params); echo '</pre>';
150     
151      $new_params = $new_enabled[$type];
152      ImageStdParams::apply_global($new_params);
153      // echo '<pre>new '.$type."\n"; print_r($old_params); echo '</pre>';
154     
155      $same = true;
156         
157      if ($new_params->use_watermark != $old_params->use_watermark
158          or $new_params->use_watermark and $watermark_changed)
159      {
160        $same = false;
161      }
162
163      if (!$same)
164      {
165        $new_params->last_mod_time = time();
166        $changed_types[] = $type;
167      }
168      else
169      {
170        $new_params->last_mod_time = $old_params->last_mod_time;
171      }
172      $new_enabled[$type] = $new_params;
173    }
174  }
175
176  $enabled_by = array(); // keys ordered by all types
177  foreach(ImageStdParams::get_all_types() as $type)
178  {
179    if (isset($new_enabled[$type]))
180    {
181      $enabled_by[$type] = $new_enabled[$type];
182    }
183  }
184  ImageStdParams::set_and_save($enabled_by);
185
186  if (count($changed_types))
187  {
188    clear_derivative_cache($changed_types);
189  }
190}
191else
192{
193  $template->assign('watermark', $pwatermark);
194  $template->assign('ferrors', $errors);
195}
196?>
Note: See TracBrowser for help on using the repository browser.