Ignore:
Timestamp:
Feb 11, 2011, 11:57:23 PM (13 years ago)
Author:
plg
Message:

feature 2189 added: ability to update a photo (the JPEG/PNG file) with
pwg.images.addSimple, simply by adding an $image_id as argument.

File:
1 edited

Legend:

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

    r9022 r9191  
    169169}
    170170
    171 function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
     171function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null, $image_id=null)
    172172{
    173173  // Here is the plan
     
    186186 
    187187  global $conf, $user;
    188  
    189   // current date
    190   list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
    191   list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
    192  
    193   // upload directory hierarchy
    194   $upload_dir = sprintf(
    195     PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
    196     $year,
    197     $month,
    198     $day
    199     );
    200 
    201   // compute file path
     188
    202189  $md5sum = md5_file($source_filepath);
    203   $date_string = preg_replace('/[^\d]/', '', $dbnow);
    204   $random_string = substr($md5sum, 0, 8);
    205   $filename_wo_ext = $date_string.'-'.$random_string;
    206   $file_path = $upload_dir.'/'.$filename_wo_ext.'.';
    207 
    208   list($width, $height, $type) = getimagesize($source_filepath);
    209   if (IMAGETYPE_PNG == $type)
    210   {
    211     $file_path.= 'png';
     190  $file_path = null;
     191 
     192  if (isset($image_id))
     193  {
     194    // we are performing an update
     195    $query = '
     196SELECT
     197    path
     198  FROM '.IMAGES_TABLE.'
     199  WHERE id = '.$image_id.'
     200;';
     201    $result = pwg_query($query);
     202    while ($row = pwg_db_fetch_assoc($result))
     203    {
     204      $file_path = $row['path'];
     205    }
     206   
     207    if (!isset($file_path))
     208    {
     209      die('['.__FUNCTION__.'] this photo does not exist in the database');
     210    }
     211
     212    // delete all physical files related to the photo (thumbnail, web site, HD)
     213    delete_element_files(array($image_id));
    212214  }
    213215  else
    214216  {
    215     $file_path.= 'jpg';
    216   }
    217 
    218   prepare_directory($upload_dir);
     217    // this photo is new
     218   
     219    // current date
     220    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
     221    list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
     222 
     223    // upload directory hierarchy
     224    $upload_dir = sprintf(
     225      PHPWG_ROOT_PATH.$conf['upload_dir'].'/%s/%s/%s',
     226      $year,
     227      $month,
     228      $day
     229      );
     230
     231    // compute file path
     232    $date_string = preg_replace('/[^\d]/', '', $dbnow);
     233    $random_string = substr($md5sum, 0, 8);
     234    $filename_wo_ext = $date_string.'-'.$random_string;
     235    $file_path = $upload_dir.'/'.$filename_wo_ext.'.';
     236
     237    list($width, $height, $type) = getimagesize($source_filepath);
     238    if (IMAGETYPE_PNG == $type)
     239    {
     240      $file_path.= 'png';
     241    }
     242    else
     243    {
     244      $file_path.= 'jpg';
     245    }
     246
     247    prepare_directory($upload_dir);
     248  }
     249
    219250  if (is_uploaded_file($source_filepath))
    220251  {
     
    297328  $thumb_infos = pwg_image_infos($thumb_path);
    298329
    299   // database registration
    300   $insert = array(
    301     'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
    302     'date_available' => $dbnow,
    303     'tn_ext' => 'jpg',
    304     'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
    305     'filesize' => $file_infos['filesize'],
    306     'width' => $file_infos['width'],
    307     'height' => $file_infos['height'],
    308     'md5sum' => $md5sum,
    309     'added_by' => $user['id'],
    310     );
    311 
    312   if (isset($high_infos))
    313   {
    314     $insert['has_high'] = 'true';
    315     $insert['high_filesize'] = $high_infos['filesize'];
    316   }
    317 
    318   if (isset($level))
    319   {
    320     $insert['level'] = $level;
    321   }
    322  
    323   mass_inserts(
    324     IMAGES_TABLE,
    325     array_keys($insert),
    326     array($insert)
    327     );
    328  
    329   $image_id = pwg_db_insert_id(IMAGES_TABLE);
     330  if (isset($image_id))
     331  {
     332    $update = array(
     333      'id' => $image_id,
     334      'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
     335      'filesize' => $file_infos['filesize'],
     336      'width' => $file_infos['width'],
     337      'height' => $file_infos['height'],
     338      'md5sum' => $md5sum,
     339      'added_by' => $user['id'],
     340      );
     341   
     342    if (isset($high_infos))
     343    {
     344      $update['has_high'] = 'true';
     345      $update['high_filesize'] = $high_infos['filesize'];
     346    }
     347    else
     348    {
     349      $update['has_high'] = 'false';
     350      $update['high_filesize'] = null;
     351    }
     352
     353    if (isset($level))
     354    {
     355      $update['level'] = $level;
     356    }
     357
     358    mass_updates(
     359      IMAGES_TABLE,
     360      array(
     361        'primary' => array('id'),
     362        'update' => array_keys($update)
     363        ),
     364      array($update)
     365      );
     366  }
     367  else
     368  {
     369    // database registration
     370    $insert = array(
     371      'file' => pwg_db_real_escape_string(isset($original_filename) ? $original_filename : basename($file_path)),
     372      'date_available' => $dbnow,
     373      'tn_ext' => 'jpg',
     374      'path' => preg_replace('#^'.preg_quote(PHPWG_ROOT_PATH).'#', '', $file_path),
     375      'filesize' => $file_infos['filesize'],
     376      'width' => $file_infos['width'],
     377      'height' => $file_infos['height'],
     378      'md5sum' => $md5sum,
     379      'added_by' => $user['id'],
     380      );
     381
     382    if (isset($high_infos))
     383    {
     384      $insert['has_high'] = 'true';
     385      $insert['high_filesize'] = $high_infos['filesize'];
     386    }
     387
     388    if (isset($level))
     389    {
     390      $insert['level'] = $level;
     391    }
     392 
     393    mass_inserts(
     394      IMAGES_TABLE,
     395      array_keys($insert),
     396      array($insert)
     397      );
     398 
     399    $image_id = pwg_db_insert_id(IMAGES_TABLE);
     400  }
    330401
    331402  if (isset($categories) and count($categories) > 0)
     
    343414  }
    344415  update_metadata(array($image_id=>$file_path));
    345  
     416
    346417  invalidate_user_cache();
    347418
Note: See TracChangeset for help on using the changeset viewer.