source: trunk/admin/include/functions_upload.inc.php @ 13843

Last change on this file since 13843 was 13843, checked in by plg, 12 years ago

feature 2604: support rotation on derivatives

File size: 13.9 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
24include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
25include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
26
27// add default event handler for image and thumbnail resize
28add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7);
29add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 9);
30
31function get_upload_form_config()
32{
33  // default configuration for upload
34  $upload_form_config = array(
35    'original_resize' => array(
36      'default' => false,
37      'can_be_null' => false,
38      ),
39
40    'original_resize_maxwidth' => array(
41      'default' => 2000,
42      'min' => 500,
43      'max' => 20000,
44      'pattern' => '/^\d+$/',
45      'can_be_null' => false,
46      'error_message' => l10n('The original maximum width must be a number between %d and %d'),
47      ),
48
49    'original_resize_maxheight' => array(
50      'default' => 2000,
51      'min' => 300,
52      'max' => 20000,
53      'pattern' => '/^\d+$/',
54      'can_be_null' => false,
55      'error_message' => l10n('The original maximum height must be a number between %d and %d'),
56      ),
57
58    'original_resize_quality' => array(
59      'default' => 95,
60      'min' => 50,
61      'max' => 98,
62      'pattern' => '/^\d+$/',
63      'can_be_null' => false,
64      'error_message' => l10n('The original image quality must be a number between %d and %d'),
65      ),
66    );
67
68  return $upload_form_config;
69}
70
71function save_upload_form_config($data, &$errors=array())
72{
73  if (!is_array($data) or empty($data))
74  {
75    return false;
76  }
77
78  $upload_form_config = get_upload_form_config();
79  $updates = array();
80
81  foreach ($data as $field => $value)
82  {
83    if (!isset($upload_form_config[$field]))
84    {
85      continue;
86    }
87    if (is_bool($upload_form_config[$field]['default']))
88    {
89      if (isset($value))
90      {
91        $value = true;
92      }
93      else
94      {
95        $value = false;
96      }
97
98      $updates[] = array(
99        'param' => $field,
100        'value' => boolean_to_string($value)
101        );
102    }
103    elseif ($upload_form_config[$field]['can_be_null'] and empty($value))
104    {
105      $updates[] = array(
106        'param' => $field,
107        'value' => 'false'
108        );
109    }
110    else
111    {
112      $min = $upload_form_config[$field]['min'];
113      $max = $upload_form_config[$field]['max'];
114      $pattern = $upload_form_config[$field]['pattern'];
115
116      if (preg_match($pattern, $value) and $value >= $min and $value <= $max)
117      {
118         $updates[] = array(
119          'param' => $field,
120          'value' => $value
121          );
122      }
123      else
124      {
125        array_push(
126          $errors,
127          sprintf(
128            $upload_form_config[$field]['error_message'],
129            $min,
130            $max
131            )
132          );
133      }
134    }
135  }
136
137  if (count($errors) == 0)
138  {
139    mass_updates(
140      CONFIG_TABLE,
141      array(
142        'primary' => array('param'),
143        'update' => array('value')
144        ),
145      $updates
146      );
147    return true;
148  }
149
150  return false;
151}
152
153function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null, $original_md5sum=null)
154{
155  // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
156  //
157  // 2) keep/resize original
158  //
159  // 3) register in database
160
161  // TODO
162  // * check md5sum (already exists?)
163
164  global $conf, $user;
165
166  if (isset($original_md5sum))
167  {
168    $md5sum = $original_md5sum;
169  }
170  else
171  {
172    $md5sum = md5_file($source_filepath);
173  }
174
175  $file_path = null;
176
177  if (isset($image_id))
178  {
179    // this photo already exists, we update it
180    $query = '
181SELECT
182    path
183  FROM '.IMAGES_TABLE.'
184  WHERE id = '.$image_id.'
185;';
186    $result = pwg_query($query);
187    while ($row = pwg_db_fetch_assoc($result))
188    {
189      $file_path = $row['path'];
190    }
191
192    if (!isset($file_path))
193    {
194      die('['.__FUNCTION__.'] this photo does not exist in the database');
195    }
196
197    // delete all physical files related to the photo (thumbnail, web site, HD)
198    delete_element_files(array($image_id));
199  }
200  else
201  {
202    // this photo is new
203
204    // current date
205    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
206    list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
207
208    // upload directory hierarchy
209    $upload_dir = sprintf(
210      PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
211      $year,
212      $month,
213      $day
214      );
215
216    // compute file path
217    $date_string = preg_replace('/[^\d]/', '', $dbnow);
218    $random_string = substr($md5sum, 0, 8);
219    $filename_wo_ext = $date_string.'-'.$random_string;
220    $file_path = $upload_dir.'/'.$filename_wo_ext.'.';
221
222    list($width, $height, $type) = getimagesize($source_filepath);
223    if (IMAGETYPE_PNG == $type)
224    {
225      $file_path.= 'png';
226    }
227    elseif (IMAGETYPE_GIF == $type)
228    {
229      $file_path.= 'gif';
230    }
231    else
232    {
233      $file_path.= 'jpg';
234    }
235
236    prepare_directory($upload_dir);
237  }
238
239  if (is_uploaded_file($source_filepath))
240  {
241    move_uploaded_file($source_filepath, $file_path);
242  }
243  else
244  {
245    rename($source_filepath, $file_path);
246  }
247
248  if (pwg_image::get_library() != 'gd')
249  {
250    if ($conf['original_resize'])
251    {
252      $need_resize = need_resize($file_path, $conf['original_resize_maxwidth'], $conf['original_resize_maxheight']);
253
254      if ($need_resize)
255      {
256        $img = new pwg_image($file_path);
257
258        $img->pwg_resize(
259          $file_path,
260          $conf['original_resize_maxwidth'],
261          $conf['original_resize_maxheight'],
262          $conf['original_resize_quality'],
263          $conf['upload_form_automatic_rotation'],
264          false
265          );
266
267        $img->destroy();
268      }
269    }
270  }
271
272  // we need to save the rotation angle in the database to compute
273  // width/height of "multisizes"
274  $rotation_angle = pwg_image::get_rotation_angle($file_path);
275  $rotation = pwg_image::get_rotation_code_from_angle($rotation_angle);
276 
277  $file_infos = pwg_image_infos($file_path);
278
279  if (isset($image_id))
280  {
281    $update = array(
282      'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
283      'filesize' => $file_infos['filesize'],
284      'width' => $file_infos['width'],
285      'height' => $file_infos['height'],
286      'md5sum' => $md5sum,
287      'added_by' => $user['id'],
288      'rotation' => $rotation,
289      );
290
291    if (isset($level))
292    {
293      $update['level'] = $level;
294    }
295
296    single_update(
297      IMAGES_TABLE,
298      $update,
299      array('id' => $image_id)
300      );
301  }
302  else
303  {
304    // database registration
305    $file = pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path));
306    $insert = array(
307      'file' => $file,
308      'name' => get_name_from_file($file),
309      'date_available' => $dbnow,
310      'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
311      'filesize' => $file_infos['filesize'],
312      'width' => $file_infos['width'],
313      'height' => $file_infos['height'],
314      'md5sum' => $md5sum,
315      'added_by' => $user['id'],
316      'rotation' => $rotation,
317      );
318
319    if (isset($level))
320    {
321      $insert['level'] = $level;
322    }
323
324    single_insert(IMAGES_TABLE, $insert);
325
326    $image_id = pwg_db_insert_id(IMAGES_TABLE);
327  }
328
329  if (isset($categories) and count($categories) > 0)
330  {
331    associate_images_to_categories(
332      array($image_id),
333      $categories
334      );
335  }
336
337  // update metadata from the uploaded file (exif/iptc)
338  if ($conf['use_exif'] and !function_exists('read_exif_data'))
339  {
340    $conf['use_exif'] = false;
341  }
342  sync_metadata(array($image_id));
343
344  invalidate_user_cache();
345
346  // cache thumbnail
347  $query = '
348SELECT
349    id,
350    path
351  FROM '.IMAGES_TABLE.'
352  WHERE id = '.$image_id.'
353;';
354  $image_infos = pwg_db_fetch_assoc(pwg_query($query));
355
356  set_make_full_url();
357  // in case we are on uploadify.php, we have to replace the false path
358  $thumb_url = preg_replace('#admin/include/i#', 'i', DerivativeImage::thumb_url($image_infos));
359  unset_make_full_url();
360 
361  fetchRemote($thumb_url, $dest);
362 
363
364  return $image_id;
365}
366
367function prepare_directory($directory)
368{
369  if (!is_dir($directory)) {
370    if (substr(PHP_OS, 0, 3) == 'WIN')
371    {
372      $directory = str_replace('/', DIRECTORY_SEPARATOR, $directory);
373    }
374    umask(0000);
375    $recursive = true;
376    if (!@mkdir($directory, 0777, $recursive))
377    {
378      die('[prepare_directory] cannot create directory "'.$directory.'"');
379    }
380  }
381
382  if (!is_writable($directory))
383  {
384    // last chance to make the directory writable
385    @chmod($directory, 0777);
386
387    if (!is_writable($directory))
388    {
389      die('[prepare_directory] directory "'.$directory.'" has no write access');
390    }
391  }
392
393  secure_directory($directory);
394}
395
396function need_resize($image_filepath, $max_width, $max_height)
397{
398  // TODO : the resize check should take the orientation into account. If a
399  // rotation must be applied to the resized photo, then we should test
400  // invert width and height.
401  list($width, $height) = getimagesize($image_filepath);
402
403  if ($width > $max_width or $height > $max_height)
404  {
405    return true;
406  }
407
408  return false;
409}
410
411function pwg_image_infos($path)
412{
413  list($width, $height) = getimagesize($path);
414  $filesize = floor(filesize($path)/1024);
415
416  return array(
417    'width'  => $width,
418    'height' => $height,
419    'filesize' => $filesize,
420    );
421}
422
423function is_valid_image_extension($extension)
424{
425  return in_array(strtolower($extension), array('jpg', 'jpeg', 'png', 'gif'));
426}
427
428function file_upload_error_message($error_code)
429{
430  switch ($error_code) {
431    case UPLOAD_ERR_INI_SIZE:
432      return sprintf(
433        l10n('The uploaded file exceeds the upload_max_filesize directive in php.ini: %sB'),
434        get_ini_size('upload_max_filesize', false)
435        );
436    case UPLOAD_ERR_FORM_SIZE:
437      return l10n('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
438    case UPLOAD_ERR_PARTIAL:
439      return l10n('The uploaded file was only partially uploaded');
440    case UPLOAD_ERR_NO_FILE:
441      return l10n('No file was uploaded');
442    case UPLOAD_ERR_NO_TMP_DIR:
443      return l10n('Missing a temporary folder');
444    case UPLOAD_ERR_CANT_WRITE:
445      return l10n('Failed to write file to disk');
446    case UPLOAD_ERR_EXTENSION:
447      return l10n('File upload stopped by extension');
448    default:
449      return l10n('Unknown upload error');
450  }
451}
452
453function get_ini_size($ini_key, $in_bytes=true)
454{
455  $size = ini_get($ini_key);
456
457  if ($in_bytes)
458  {
459    $size = convert_shorthand_notation_to_bytes($size);
460  }
461
462  return $size;
463}
464
465function convert_shorthand_notation_to_bytes($value)
466{
467  $suffix = substr($value, -1);
468  $multiply_by = null;
469
470  if ('K' == $suffix)
471  {
472    $multiply_by = 1024;
473  }
474  else if ('M' == $suffix)
475  {
476    $multiply_by = 1024*1024;
477  }
478  else if ('G' == $suffix)
479  {
480    $multiply_by = 1024*1024*1024;
481  }
482
483  if (isset($multiply_by))
484  {
485    $value = substr($value, 0, -1);
486    $value*= $multiply_by;
487  }
488
489  return $value;
490}
491
492function add_upload_error($upload_id, $error_message)
493{
494  if (!isset($_SESSION['uploads_error']))
495  {
496    $_SESSION['uploads_error'] = array();
497  }
498  if (!isset($_SESSION['uploads_error'][$upload_id]))
499  {
500    $_SESSION['uploads_error'][$upload_id] = array();
501  }
502
503  array_push($_SESSION['uploads_error'][$upload_id], $error_message);
504}
505
506function ready_for_upload_message()
507{
508  global $conf;
509
510  $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']);
511
512  if (!is_dir($conf['upload_dir']))
513  {
514    if (!is_writable(dirname($conf['upload_dir'])))
515    {
516      return sprintf(
517        l10n('Create the "%s" directory at the root of your Piwigo installation'),
518        $relative_dir
519        );
520    }
521  }
522  else
523  {
524    if (!is_writable($conf['upload_dir']))
525    {
526      @chmod($conf['upload_dir'], 0777);
527
528      if (!is_writable($conf['upload_dir']))
529      {
530        return sprintf(
531          l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'),
532          $relative_dir
533          );
534      }
535    }
536  }
537
538  return null;
539}
540
541function file_path_for_type($file_path, $type='thumb')
542{
543  // resolve the $file_path depending on the $type
544  if ('thumb' == $type) {
545    $file_path = get_thumbnail_location(
546      array(
547        'path' => $file_path,
548        'tn_ext' => 'jpg',
549        )
550      );
551  }
552
553  if ('high' == $type) {
554    @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
555    $file_path = get_high_location(
556      array(
557        'path' => $file_path,
558        'has_high' => 'true'
559        )
560      );
561  }
562
563  return $file_path;
564}
565
566?>
Note: See TracBrowser for help on using the repository browser.