Changeset 8249


Ignore:
Timestamp:
Dec 23, 2010, 11:22:51 AM (10 years ago)
Author:
plg
Message:

feature 2083 added: implement method pwg.images.addSimple in core

makes admin/include/function_upload.inc.php not dependant from include/ws_functions.inc.php (moves functions file_path_for_type and ready_for_upload_message)

cleaner method to initialize the upload settings

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions_upload.inc.php

    r8227 r8249  
    11<?php
    2 // TODO
    3 // * check md5sum (already exists?)
    4 
    5 include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
    6 include_once(PHPWG_ROOT_PATH.'include/ws_functions.inc.php');
     2// +-----------------------------------------------------------------------+
     3// | Piwigo - a PHP based picture gallery                                  |
     4// +-----------------------------------------------------------------------+
     5// | Copyright(C) 2008-2010 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
    724include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    8 
    9 // Here is the plan
    10 //
    11 // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
    12 //
    13 // 2) if taller than max_height or wider than max_width, move to pwg_high
    14 //    + web sized creation
    15 //
    16 // 3) thumbnail creation from web sized
    17 //
    18 // 4) register in database
    1925
    2026// add default event handler for image and thumbnail resize
     
    2228add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7);
    2329
     30function get_upload_form_config()
     31{
     32  // default configuration for upload
     33  $upload_form_config = array(
     34    'websize_resize' => array(
     35      'default' => true,
     36      'can_be_null' => false,
     37      ),
     38   
     39    'websize_maxwidth' => array(
     40      'default' => 800,
     41      'min' => 100,
     42      'max' => 1600,
     43      'pattern' => '/^\d+$/',
     44      'can_be_null' => true,
     45      'error_message' => l10n('The websize maximum width must be a number between %d and %d'),
     46      ),
     47 
     48    'websize_maxheight' => array(
     49      'default' => 600,
     50      'min' => 100,
     51      'max' => 1200,
     52      'pattern' => '/^\d+$/',
     53      'can_be_null' => true,
     54      'error_message' => l10n('The websize maximum height must be a number between %d and %d'),
     55      ),
     56 
     57    'websize_quality' => array(
     58      'default' => 95,
     59      'min' => 50,
     60      'max' => 100,
     61      'pattern' => '/^\d+$/',
     62      'can_be_null' => false,
     63      'error_message' => l10n('The websize image quality must be a number between %d and %d'),
     64      ),
     65 
     66    'thumb_maxwidth' => array(
     67      'default' => 128,
     68      'min' => 50,
     69      'max' => 300,
     70      'pattern' => '/^\d+$/',
     71      'can_be_null' => false,
     72      'error_message' => l10n('The thumbnail maximum width must be a number between %d and %d'),
     73      ),
     74 
     75    'thumb_maxheight' => array(
     76      'default' => 96,
     77      'min' => 50,
     78      'max' => 300,
     79      'pattern' => '/^\d+$/',
     80      'can_be_null' => false,
     81      'error_message' => l10n('The thumbnail maximum height must be a number between %d and %d'),
     82      ),
     83 
     84    'thumb_quality' => array(
     85      'default' => 95,
     86      'min' => 50,
     87      'max' => 100,
     88      'pattern' => '/^\d+$/',
     89      'can_be_null' => false,
     90      'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'),
     91      ),
     92 
     93    'hd_keep' => array(
     94      'default' => true,
     95      'can_be_null' => false,
     96      ),
     97 
     98    'hd_resize' => array(
     99      'default' => false,
     100      'can_be_null' => false,
     101      ),
     102 
     103    'hd_maxwidth' => array(
     104      'default' => 2000,
     105      'min' => 500,
     106      'max' => 20000,
     107      'pattern' => '/^\d+$/',
     108      'can_be_null' => false,
     109      'error_message' => l10n('The high definition maximum width must be a number between %d and %d'),
     110      ),
     111 
     112    'hd_maxheight' => array(
     113      'default' => 2000,
     114      'min' => 500,
     115      'max' => 20000,
     116      'pattern' => '/^\d+$/',
     117      'can_be_null' => false,
     118      'error_message' => l10n('The high definition maximum height must be a number between %d and %d'),
     119      ),
     120 
     121    'hd_quality' => array(
     122      'default' => 95,
     123      'min' => 50,
     124      'max' => 100,
     125      'pattern' => '/^\d+$/',
     126      'can_be_null' => false,
     127      'error_message' => l10n('The high definition image quality must be a number between %d and %d'),
     128      ),
     129    );
     130
     131  return $upload_form_config;
     132}
     133
     134/*
     135 * automatic fill of configuration parameters
     136 */
     137function prepare_upload_configuration()
     138{
     139  global $conf;
     140
     141  $inserts = array();
     142 
     143  foreach (get_upload_form_config() as $param_shortname => $param)
     144  {
     145    $param_name = 'upload_form_'.$param_shortname;
     146 
     147    if (!isset($conf[$param_name]))
     148    {
     149      $param_value = boolean_to_string($param['default']);
     150     
     151      array_push(
     152        $inserts,
     153        array(
     154          'param' => $param_name,
     155          'value' => $param_value,
     156          )
     157        );
     158
     159      $conf[$param_name] = $param_value;
     160      if (is_bool($param['default']))
     161      {
     162        $conf[$param_name] = get_boolean($param_value);
     163      }
     164    }
     165  }
     166 
     167  if (count($inserts) > 0)
     168  {
     169    mass_inserts(
     170      CONFIG_TABLE,
     171      array_keys($inserts[0]),
     172      $inserts
     173      );
     174  }
     175}
     176
    24177function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
    25178{
     179  // Here is the plan
     180  //
     181  // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
     182  //
     183  // 2) if taller than max_height or wider than max_width, move to pwg_high
     184  //    + web sized creation
     185  //
     186  // 3) thumbnail creation from web sized
     187  //
     188  // 4) register in database
     189 
     190  // TODO
     191  // * check md5sum (already exists?)
     192 
    26193  global $conf;
    27 
     194 
    28195  // current date
    29196  list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
     
    90257      if ($conf['upload_form_hd_keep'])
    91258      {
    92         $need_resize = need_resize($high_path, $conf['upload_form_hd_maxwidth'], $conf['upload_form_hd_maxheight']);
     259        if ($conf['upload_form_hd_resize'])
     260        {
     261          $need_resize = need_resize($high_path, $conf['upload_form_hd_maxwidth'], $conf['upload_form_hd_maxheight']);
    93262       
    94         if ($conf['upload_form_hd_resize'] and $need_resize)
    95         {
    96           pwg_image_resize(
    97             false,
    98             $high_path,
    99             $high_path,
    100             $conf['upload_form_hd_maxwidth'],
    101             $conf['upload_form_hd_maxheight'],
    102             $conf['upload_form_hd_quality'],
    103             false
    104             );
    105           $high_infos = pwg_image_infos($high_path);
     263          if ($need_resize)
     264          {
     265            pwg_image_resize(
     266              false,
     267              $high_path,
     268              $high_path,
     269              $conf['upload_form_hd_maxwidth'],
     270              $conf['upload_form_hd_maxheight'],
     271              $conf['upload_form_hd_quality'],
     272              false
     273              );
     274            $high_infos = pwg_image_infos($high_path);
     275          }
    106276        }
    107277      }
     
    558728  return false;
    559729}
     730
     731function ready_for_upload_message()
     732{
     733  global $conf;
     734
     735  $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']);
     736
     737  if (!is_dir($conf['upload_dir']))
     738  {
     739    if (!is_writable(dirname($conf['upload_dir'])))
     740    {
     741      return sprintf(
     742        l10n('Create the "%s" directory at the root of your Piwigo installation'),
     743        $relative_dir
     744        );
     745    }
     746  }
     747  else
     748  {
     749    if (!is_writable($conf['upload_dir']))
     750    {
     751      @chmod($conf['upload_dir'], 0777);
     752     
     753      if (!is_writable($conf['upload_dir']))
     754      {
     755        return sprintf(
     756          l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'),
     757          $relative_dir
     758          );
     759      }
     760    }
     761  }
     762
     763  return null;
     764}
     765
     766function file_path_for_type($file_path, $type='thumb')
     767{
     768  // resolve the $file_path depending on the $type
     769  if ('thumb' == $type) {
     770    $file_path = get_thumbnail_location(
     771      array(
     772        'path' => $file_path,
     773        'tn_ext' => 'jpg',
     774        )
     775      );
     776  }
     777
     778  if ('high' == $type) {
     779    @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
     780    $file_path = get_high_location(
     781      array(
     782        'path' => $file_path,
     783        'has_high' => 'true'
     784        )
     785      );
     786  }
     787
     788  return $file_path;
     789}
    560790?>
  • trunk/admin/photos_add.php

    r8227 r8249  
    4444// +-----------------------------------------------------------------------+
    4545
    46 // automatic fill of configuration parameters
    47 $upload_form_config = array(
    48   'websize_resize' => array(
    49     'default' => true,
    50     'can_be_null' => false,
    51     ),
    52  
    53   'websize_maxwidth' => array(
    54     'default' => 800,
    55     'min' => 100,
    56     'max' => 1600,
    57     'pattern' => '/^\d+$/',
    58     'can_be_null' => true,
    59     'error_message' => l10n('The websize maximum width must be a number between %d and %d'),
    60     ),
    61  
    62   'websize_maxheight' => array(
    63     'default' => 600,
    64     'min' => 100,
    65     'max' => 1200,
    66     'pattern' => '/^\d+$/',
    67     'can_be_null' => true,
    68     'error_message' => l10n('The websize maximum height must be a number between %d and %d'),
    69     ),
    70  
    71   'websize_quality' => array(
    72     'default' => 95,
    73     'min' => 50,
    74     'max' => 100,
    75     'pattern' => '/^\d+$/',
    76     'can_be_null' => false,
    77     'error_message' => l10n('The websize image quality must be a number between %d and %d'),
    78     ),
    79  
    80   'thumb_maxwidth' => array(
    81     'default' => 128,
    82     'min' => 50,
    83     'max' => 300,
    84     'pattern' => '/^\d+$/',
    85     'can_be_null' => false,
    86     'error_message' => l10n('The thumbnail maximum width must be a number between %d and %d'),
    87     ),
    88  
    89   'thumb_maxheight' => array(
    90     'default' => 96,
    91     'min' => 50,
    92     'max' => 300,
    93     'pattern' => '/^\d+$/',
    94     'can_be_null' => false,
    95     'error_message' => l10n('The thumbnail maximum height must be a number between %d and %d'),
    96     ),
    97  
    98   'thumb_quality' => array(
    99     'default' => 95,
    100     'min' => 50,
    101     'max' => 100,
    102     'pattern' => '/^\d+$/',
    103     'can_be_null' => false,
    104     'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'),
    105     ),
    106  
    107   'hd_keep' => array(
    108     'default' => true,
    109     'can_be_null' => false,
    110     ),
    111  
    112   'hd_resize' => array(
    113     'default' => false,
    114     'can_be_null' => false,
    115     ),
    116  
    117   'hd_maxwidth' => array(
    118     'default' => 2000,
    119     'min' => 500,
    120     'max' => 20000,
    121     'pattern' => '/^\d+$/',
    122     'can_be_null' => false,
    123     'error_message' => l10n('The high definition maximum width must be a number between %d and %d'),
    124     ),
    125  
    126   'hd_maxheight' => array(
    127     'default' => 2000,
    128     'min' => 500,
    129     'max' => 20000,
    130     'pattern' => '/^\d+$/',
    131     'can_be_null' => false,
    132     'error_message' => l10n('The high definition maximum height must be a number between %d and %d'),
    133     ),
    134  
    135   'hd_quality' => array(
    136     'default' => 95,
    137     'min' => 50,
    138     'max' => 100,
    139     'pattern' => '/^\d+$/',
    140     'can_be_null' => false,
    141     'error_message' => l10n('The high definition image quality must be a number between %d and %d'),
    142     ),
    143   );
     46prepare_upload_configuration();
    14447
    145 $inserts = array();
    146 
    147 foreach ($upload_form_config as $param_shortname => $param)
    148 {
    149   $param_name = 'upload_form_'.$param_shortname;
    150  
    151   if (!isset($conf[$param_name]))
    152   {
    153     $param_value = boolean_to_string($param['default']);
    154    
    155     array_push(
    156       $inserts,
    157       array(
    158         'param' => $param_name,
    159         'value' => $param_value,
    160         )
    161       );
    162     $conf[$param_name] = $param_value;
    163   }
    164 }
    165 
    166 if (count($inserts) > 0)
    167 {
    168   mass_inserts(
    169     CONFIG_TABLE,
    170     array_keys($inserts[0]),
    171     $inserts
    172     );
    173 }
     48$upload_form_config = get_upload_form_config();
    17449
    17550// +-----------------------------------------------------------------------+
     
    239114  );
    240115
    241 // $template->append(
    242 //   'head_elements',
    243 //   '<link rel="stylesheet" type="text/css" href="'.UPLOAD_FORM_PATH.'upload.css">'."\n"
    244 //   );
    245 
    246116// +-----------------------------------------------------------------------+
    247117// |                             Load the tab                              |
  • trunk/include/ws_functions.inc.php

    r8126 r8249  
    10771077function add_file($file_path, $type, $original_sum, $file_sum)
    10781078{
     1079  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     1080 
    10791081  $file_path = file_path_for_type($file_path, $type);
    10801082
     
    13361338 
    13371339  invalidate_user_cache();
     1340}
     1341
     1342function ws_images_addSimple($params, &$service)
     1343{
     1344  global $conf;
     1345  if (!is_admin() || is_adviser() )
     1346  {
     1347    return new PwgError(401, 'Access denied');
     1348  }
     1349
     1350  if (!$service->isPost())
     1351  {
     1352    return new PwgError(405, "This method requires HTTP POST");
     1353  }
     1354
     1355  // category
     1356  $params['category'] = (int)$params['category'];
     1357  if ($params['category'] <= 0)
     1358  {
     1359    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id");
     1360  }
     1361
     1362  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
     1363  prepare_upload_configuration();
     1364
     1365  $image_id = add_uploaded_file(
     1366    $_FILES['image']['tmp_name'],
     1367    $_FILES['image']['name'],
     1368    array($params['category']),
     1369    8
     1370    );
     1371
     1372  $info_columns = array(
     1373    'name',
     1374    'author',
     1375    'comment',
     1376    'level',
     1377    'date_creation',
     1378    );
     1379
     1380  foreach ($info_columns as $key)
     1381  {
     1382    if (isset($params[$key]))
     1383    {
     1384      $update[$key] = $params[$key];
     1385    }
     1386  }
     1387
     1388  if (count(array_keys($update)) > 0)
     1389  {
     1390    $update['id'] = $image_id;
     1391
     1392    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1393    mass_updates(
     1394      IMAGES_TABLE,
     1395      array(
     1396        'primary' => array('id'),
     1397        'update'  => array_diff(array_keys($update), array('id'))
     1398        ),
     1399      array($update)
     1400      );
     1401  }
     1402
     1403
     1404  if (isset($params['tags']) and !empty($params['tags']))
     1405  {
     1406    $tag_ids = array();
     1407    $tag_names = explode(',', $params['tags']);
     1408    foreach ($tag_names as $tag_name)
     1409    {
     1410      $tag_id = tag_id_from_tag_name($tag_name);
     1411      array_push($tag_ids, $tag_id);
     1412    }
     1413
     1414    add_tags($tag_ids, array($image_id));
     1415  }
     1416
     1417  $query = '
     1418SELECT id, name, permalink
     1419  FROM '.CATEGORIES_TABLE.'
     1420  WHERE id = '.$params['category'].'
     1421;';
     1422  $result = pwg_query($query);
     1423  $category = pwg_db_fetch_assoc($result);
     1424
     1425  return array(
     1426    'image_id' => $image_id,
     1427    'url' => make_picture_url(
     1428      array(
     1429        'image_id' => $image_id,
     1430        'section' => 'categories',
     1431        'category' => $category
     1432        )
     1433      ),
     1434    );
    13381435}
    13391436
     
    17451842
    17461843    if (isset($params[$param_name.'_sum'])) {
     1844      include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    17471845      $type_path = file_path_for_type($path, $type);
    17481846      if (!is_file($type_path)) {
     
    17611859
    17621860  return $ret;
    1763 }
    1764 
    1765 function file_path_for_type($file_path, $type='thumb')
    1766 {
    1767   // resolve the $file_path depending on the $type
    1768   if ('thumb' == $type) {
    1769     $file_path = get_thumbnail_location(
    1770       array(
    1771         'path' => $file_path,
    1772         'tn_ext' => 'jpg',
    1773         )
    1774       );
    1775   }
    1776 
    1777   if ('high' == $type) {
    1778     @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
    1779     $file_path = get_high_location(
    1780       array(
    1781         'path' => $file_path,
    1782         'has_high' => 'true'
    1783         )
    1784       );
    1785   }
    1786 
    1787   return $file_path;
    17881861}
    17891862
     
    21532226  }
    21542227
     2228  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    21552229  $ret['message'] = ready_for_upload_message();
    21562230  $ret['ready_for_upload'] = true;
     
    21632237  return $ret;
    21642238}
    2165 
    2166 function ready_for_upload_message()
    2167 {
    2168   global $conf;
    2169 
    2170   $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']);
    2171 
    2172   if (!is_dir($conf['upload_dir']))
    2173   {
    2174     if (!is_writable(dirname($conf['upload_dir'])))
    2175     {
    2176       return sprintf(
    2177         l10n('Create the "%s" directory at the root of your Piwigo installation'),
    2178         $relative_dir
    2179         );
    2180     }
    2181   }
    2182   else
    2183   {
    2184     if (!is_writable($conf['upload_dir']))
    2185     {
    2186       @chmod($conf['upload_dir'], 0777);
    2187      
    2188       if (!is_writable($conf['upload_dir']))
    2189       {
    2190         return sprintf(
    2191           l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'),
    2192           $relative_dir
    2193           );
    2194       }
    2195     }
    2196   }
    2197 
    2198   return null;
    2199 }
    22002239?>
  • trunk/ws.php

    r6363 r8249  
    222222
    223223  $service->addMethod(
     224    'pwg.images.addSimple',
     225    'ws_images_addSimple',
     226    array(
     227      'category' => array('default' => null),
     228      'name' => array('default' => null),
     229      'author' => array('default' => null),
     230      'comment' => array('default' => null),
     231      'level' => array(
     232        'default' => 0,
     233        'maxValue' => $conf['available_permission_levels']
     234        ),
     235      'tags' => array('default' => null),
     236      ),
     237    'POST method only.<br>Use the <b>image</b> field for uploading file.<br>Set the form encoding to "form-data"<br><b>category</b> is the numeric identifier of the destination category.'
     238    );
     239 
     240  $service->addMethod(
    224241    'pwg.categories.getAdminList',
    225242    'ws_categories_getAdminList',
Note: See TracChangeset for help on using the changeset viewer.