Changeset 12865


Ignore:
Timestamp:
Jan 8, 2012, 8:25:52 PM (9 years ago)
Author:
rvelices
Message:

feature 2548 multisize - added a page to build missing derivatives

  • browser driven, chained ws calls to retrieve urls, visual feedback of progress through slideshow
Location:
trunk
Files:
3 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/include/functions.php

    r12855 r12865  
    201201    $files[] = get_element_path($row);
    202202
    203    
    204203    if (!empty($row['representative_ext']))
    205204    {
    206       $pi = pathinfo($row['path']);
    207       $file_wo_ext = get_filename_wo_extension($pi['basename']);
    208205      $files[] = original_to_representative( $files[0], $row['representative_ext']);
    209206    }
  • trunk/admin/include/image.class.php

    r12851 r12865  
    705705                imagecopy($cut, $ioverlay, 0, 0, 0, 0, $ow, $oh);
    706706                imagecopymerge($this->image, $cut, $x, $y, 0, 0, $ow, $oh, $opacity);
     707    imagedestroy($cut);
    707708    return true;
    708709  }
  • trunk/admin/themes/default/template/derivatives.tpl

    r12851 r12865  
    2323{/literal}{/html_head}
    2424
     25<p><a href="admin.php?page=derivatives_build">Build missing derivatives</a></p>
    2526<form method="post" id="derviativesForm">
    2627<fieldset>
  • trunk/i.php

    r12851 r12865  
    109109  {
    110110    $req = $_SERVER["QUERY_STRING"];
     111    if ($pos=strpos($req, '&'))
     112    {
     113      $req = substr($req, 0, $pos);
     114    }
    111115    /*foreach (array_keys($_GET) as $keynum => $key)
    112116    {
     
    168172    try
    169173    {
    170       $page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
     174      $params = $page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
    171175    }
    172176    catch (Exception $e)
    173177    {
    174178      ierror($e->getMessage(), 400);
     179    }
     180    if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
     181    {
     182      ierror('Invalid size', 400);
     183    }
     184    if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
     185    {
     186      ierror('Invalid crop', 400);
    175187    }
    176188  }
     
    225237
    226238$params = $page['derivative_params'];
    227 if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
    228 {
    229   ierror('Invalid size', 400);
    230 }
    231 if ($params->sizing->max_crop < 0 or $params->sizing->max_crop > 1)
    232 {
    233   ierror('Invalid crop', 400);
    234 }
    235239
    236240$src_mtime = @filemtime($page['src_path']);
     
    251255$expires=false;
    252256$now = time();
    253 if ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
     257if ( isset($_GET['b']) )
     258{
     259  $expires = $now + 100;
     260  header("Cache-control: no-store, max-age=100");
     261}
     262elseif ( $now > (max($src_mtime, $params->last_mod_time) + 24*3600) )
    254263{// somehow arbitrary - if derivative params or src didn't change for the last 24 hours, we send an expire header for several days
    255264  $expires = $now + 10*24*3600;
     
    268277}
    269278
     279if (!mkgetdir(dirname($page['derivative_path'])))
     280{
     281  ierror("dir create error", 500);
     282}
    270283
    271284include_once(PHPWG_ROOT_PATH . 'admin/include/image.class.php');
     
    275288
    276289$image = new pwg_image($page['src_path']);
    277 
    278 if (!mkgetdir(dirname($page['derivative_path'])))
    279 {
    280   ierror("dir create error", 500);
    281 }
    282290
    283291$changes = 0;
     
    326334    {
    327335      // todo
     336      $pad = $wm_size[0] + max(30, round($wm_size[0]/4));
     337      for($i=-$wm->xrepeat; $i<=$wm->xrepeat; $i++)
     338      {
     339        if (!$i) continue;
     340        $x2 = $x + $i * $pad;
     341        if ($x2>=0 && $x2+$wm_size[0]<$d_size[0])
     342          if (!$image->compose($wm_image, $x2, $y, $wm->opacity))
     343            break;
     344      }
    328345    }
    329346  }
  • trunk/include/derivative.inc.php

    r12855 r12865  
    220220  }
    221221
     222  function get_path()
     223  {
     224    return PHPWG_ROOT_PATH.$this->rel_path;
     225  }
     226
    222227  function get_url()
    223228  {
  • trunk/include/ws_functions.inc.php

    r12855 r12865  
    9393    $clauses[] = $tbl_name.'width/'.$tbl_name.'height<='.$params['f_max_ratio'];
    9494  }
    95   if ( $params['f_with_thumbnail'] )
    96   {
    97     $clauses[] = $tbl_name.'tn_ext IS NOT NULL';
     95  if (is_numeric($params['f_max_level']) )
     96  {
     97    $clauses[] = $tbl_name.'level <= '.$params['f_max_level'];
    9898  }
    9999  return $clauses;
     
    148148{
    149149  $ret = array();
    150  
     150
    151151  $src_image = new SrcImage($image_row);
    152152
     
    156156    $ret['element_url'] = get_element_url($image_row);
    157157  }
    158  
     158
    159159  $derivatives = DerivativeImage::get_all($src_image);
    160160  $derivatives_arr = array();
     
    178178    'id','element_url', 'file','width','height','hit','date_available','date_creation'
    179179    );
     180}
     181
     182function ws_getMissingDerivatives($params, &$service)
     183{
     184  if (!is_admin())
     185  {
     186    return new PwgError(403, 'Forbidden');
     187  }
     188
     189  if ( empty($params['types']) )
     190  {
     191    $types = array_keys(ImageStdParams::get_defined_type_map());
     192  }
     193  else
     194  {
     195    $types = array_intersect(array_keys(ImageStdParams::get_defined_type_map()), $params['types']);
     196    if (count($types)==0)
     197    {
     198      return new PwgError(WS_ERR_INVALID_PARAM, "Invalid types");
     199    }
     200  }
     201
     202  if ( ($max_urls = intval($params['max_urls'])) <= 0)
     203  {
     204    return new PwgError(WS_ERR_INVALID_PARAM, "Invalid max_urls");
     205  }
     206
     207  list($max_id, $image_count) = pwg_db_fetch_row( pwg_query('SELECT MAX(id)+1, COUNT(*) FROM '.IMAGES_TABLE) );
     208  $start_id = intval($params['prev_page']);
     209  if ($start_id<=0)
     210  {
     211    $start_id = $max_id;
     212  }
     213
     214  $uid = '&b='.time();
     215  global $conf;
     216  $conf['question_mark_in_urls'] = $conf['php_extension_in_urls'] = true;
     217  $conf['derivative_url_style']=2; //script
     218
     219  $qlimit = min(5000, ceil(max($image_count/500, $max_urls/count($types))));
     220  $where_clauses = ws_std_image_sql_filter( $params, '' );
     221  $where_clauses[] = 'id<start_id';
     222
     223  $query_model = 'SELECT id, path, representative_ext, width, height
     224  FROM '.IMAGES_TABLE.'
     225  WHERE '.implode(' AND ', $where_clauses).'
     226  ORDER BY id DESC
     227  LIMIT '.$qlimit;
     228
     229  $urls=array();
     230  do
     231  {
     232    $result = pwg_query( str_replace('start_id', $start_id, $query_model));
     233    $is_last = pwg_db_num_rows($result) < $qlimit;
     234    while ($row=pwg_db_fetch_assoc($result))
     235    {
     236      $start_id = $row['id'];
     237      $src_image = new SrcImage($row);
     238      if ($src_image->is_mimetype())
     239        continue;
     240      foreach($types as $type)
     241      {
     242        $derivative = new DerivativeImage($type, $src_image);
     243        if ($type != $derivative->get_type())
     244          continue;
     245        if (@filemtime($derivative->get_path())===false)
     246        {
     247          $urls[] = $derivative->get_url().$uid;
     248        }
     249      }
     250      if (count($urls)>=$max_urls && !$is_last)
     251        break;
     252    }
     253    if ($is_last)
     254    {
     255      $start_id = 0;
     256    }
     257  }while (count($urls)<$max_urls && $start_id);
     258
     259  $ret = array();
     260  if ($start_id)
     261  {
     262    $ret['next_page']=$start_id;
     263  }
     264  $ret['urls']=$urls;
     265  return $ret;
    180266}
    181267
     
    515601  $user_representative_updates_for = array();
    516602  // management of the album thumbnail -- stops here
    517  
     603
    518604  $cats = array();
    519605  while ($row = pwg_db_fetch_assoc($result))
     
    553639
    554640    // management of the album thumbnail -- starts here
    555     // 
     641    //
    556642    // on branch 2.3, the algorithm is duplicated from
    557643    // include/category_cats, but we should use a common code for Piwigo 2.4
     
    604690      }
    605691    }
    606    
     692
    607693    if (isset($image_id))
    608694    {
     
    611697        $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
    612698      }
    613    
     699
    614700      $row['representative_picture_id'] = $image_id;
    615701      array_push($image_ids, $image_id);
     
    651737        // * register it at user_representative_picture_id
    652738        // * set it as the representative_picture_id for the category
    653        
     739
    654740        foreach ($categories as &$category)
    655741        {
     
    658744            // searching a random representant among elements in sub-categories
    659745            $image_id = get_random_image_in_category($category);
    660            
     746
    661747            if (isset($image_id) and !in_array($image_id, $image_ids))
    662748            {
    663749              array_push($new_image_ids, $image_id);
    664750            }
    665            
     751
    666752            if ($conf['representative_cache_on_level'])
    667753            {
    668754              $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
    669755            }
    670            
     756
    671757            $category['representative_picture_id'] = $image_id;
    672758          }
     
    675761      }
    676762    }
    677    
     763
    678764    if (count($new_image_ids) > 0)
    679765    {
     
    697783  {
    698784    $updates = array();
    699  
     785
    700786    foreach ($user_representative_updates_for as $user_cat => $image_id)
    701787    {
    702788      list($user_id, $cat_id) = explode('#', $user_cat);
    703    
     789
    704790      array_push(
    705791        $updates,
     
    736822    unset($cat['count_categories']);
    737823  }
    738   unset($cat); 
     824  unset($cat);
    739825  // management of the album thumbnail -- stops here
    740826
     
    15311617  // check dumped thumbnail md5
    15321618  $dumped_md5 = md5_file($file_path);
    1533   if ($dumped_md5 != $file_sum) 
     1619  if ($dumped_md5 != $file_sum)
    15341620  {
    15351621    return new PwgError(500, '[add_file] '.$type.' transfer failed');
     
    16661752  {
    16671753    ws_logfile('[pwg.images.add] resize activated');
    1668    
     1754
    16691755    // temporary file path
    16701756    $type = 'file';
    16711757    $file_path = $conf['upload_dir'].'/buffer/'.$params['original_sum'].'-'.$type;
    1672    
     1758
    16731759    merge_chunks($file_path, $params['original_sum'], $type);
    16741760    chmod($file_path, 0644);
    16751761
    16761762    include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
    1677    
     1763
    16781764    $image_id = add_uploaded_file(
    16791765      $file_path,
     
    17041790    $filename_wo_ext = $date_string.'-'.$random_string;
    17051791    $file_path = $upload_dir.'/'.$filename_wo_ext.'.jpg';
    1706    
     1792
    17071793    // add files
    17081794    $file_infos  = add_file($file_path, 'file',  $params['original_sum'], $params['file_sum']);
    17091795    $thumb_infos = add_file($file_path, 'thumb', $params['original_sum'], $params['thumbnail_sum']);
    1710    
     1796
    17111797    if (isset($params['high_sum']))
    17121798    {
     
    17631849    }
    17641850  }
    1765  
     1851
    17661852  if (count(array_keys($update)) > 0)
    17671853  {
     
    17741860
    17751861  $url_params = array('image_id' => $image_id);
    1776  
     1862
    17771863  // let's add links between the image and the categories
    17781864  if (isset($params['categories']))
     
    17821868    if (preg_match('/^\d+/', $params['categories'], $matches)) {
    17831869      $category_id = $matches[0];
    1784    
     1870
    17851871      $query = '
    17861872SELECT id, name, permalink
     
    17901876      $result = pwg_query($query);
    17911877      $category = pwg_db_fetch_assoc($result);
    1792      
     1878
    17931879      $url_params['section'] = 'categories';
    17941880      $url_params['category'] = $category;
     
    19582044    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid user_id');
    19592045  }
    1960  
     2046
    19612047  $query = '
    19622048DELETE FROM '.RATE_TABLE.'
    19632049  WHERE user_id='.$user_id;
    1964  
     2050
    19652051  if (!empty($params['anonymous_id']))
    19662052  {
    19672053    $query .= ' AND anonymous_id=\''.$params['anonymous_id'].'\'';
    19682054  }
    1969  
     2055
    19702056  $changes = pwg_db_changes(pwg_query($query));
    19712057  if ($changes)
     
    32753361    return new PwgError(403, "image can't be resized");
    32763362  }
    3277  
     3363
    32783364  $hd_path = get_high_path($image);
    32793365
     
    32853371      $hd_dir = dirname($hd_path);
    32863372      prepare_directory($hd_dir);
    3287      
     3373
    32883374      rename($image_path, $hd_path);
    32893375      $hd_infos = pwg_image_infos($hd_path);
  • trunk/themes/default/template/picture_content.tpl

    r12855 r12865  
    3333}
    3434{/literal}{/footer_script}
    35 <a id="derivativeSwitchLink" onclick="toggleDerivativeSwitchBox()" style="cursor:pointer">{$current.selected_derivative->get_type()|@translate}</a>
     35<a id="derivativeSwitchLink" href="javascript:toggleDerivativeSwitchBox()">{$current.selected_derivative->get_type()|@translate}</a>
    3636<div id="derivativeSwitchBox" onclick="toggleDerivativeSwitchBox()" style="display:none">
    3737{foreach from=$current.unique_derivatives item=derivative key=derivative_type}
     
    3939{/foreach}
    4040{if isset($U_ORIGINAL)}
    41 <a  href="javascript:phpWGOpenWindow('{$U_ORIGINAL}','xxx','scrollbars=yes,toolbar=no,status=no,resizable=yes')" title="{'Click on the photo to see it in high definition'|@translate}">{'original'|@translate}</a>
     41<a href="javascript:phpWGOpenWindow('{$U_ORIGINAL}','xxx','scrollbars=yes,toolbar=no,status=no,resizable=yes')" title="{'Click on the photo to see it in high definition'|@translate}">{'original'|@translate}</a>
    4242{/if}
    4343</div>
  • trunk/ws.php

    r12730 r12865  
    7070        'f_min_ratio' => array( 'default'=> null ),
    7171        'f_max_ratio' => array( 'default'=> null ),
    72         'f_with_thumbnail' => array( 'default'=> false ),
     72        'f_max_level' => array( 'default'=> null ),
    7373      ),
    7474      'Returns elements for the corresponding categories.
     
    8686      ),
    8787      'retrieves a list of categories (tree_output option only compatible with json/php output format' );
     88
     89  $service->addMethod('pwg.getMissingDerivatives', 'ws_getMissingDerivatives',
     90      array(
     91        'types' => array( 'default'=>array(), 'flags'=>WS_PARAM_FORCE_ARRAY),
     92        'max_urls' => array( 'default' => 200 ),
     93        'prev_page' => array( 'default'=> null),
     94        'f_min_rate' => array( 'default'=> null ),
     95        'f_max_rate' => array( 'default'=> null ),
     96        'f_min_hit' => array( 'default'=> null ),
     97        'f_max_hit' => array( 'default'=> null ),
     98        'f_min_date_available' => array( 'default'=> null ),
     99        'f_max_date_available' => array( 'default'=> null ),
     100        'f_min_date_created' => array( 'default'=> null ),
     101        'f_max_date_created' => array( 'default'=> null ),
     102        'f_min_ratio' => array( 'default'=> null ),
     103        'f_max_ratio' => array( 'default'=> null ),
     104        'f_max_level' => array( 'default'=> null ),
     105      ),
     106      'retrieves a list of derivatives to build' );
    88107
    89108  $service->addMethod('pwg.images.addComment', 'ws_images_addComment',
     
    130149        'f_min_ratio' => array( 'default'=> null ),
    131150        'f_max_ratio' => array( 'default'=> null ),
    132         'f_with_thumbnail' => array( 'default'=> false ),
     151        'f_max_level' => array( 'default'=> null ),
    133152      ),
    134153      'Returns elements for the corresponding query search.'
     
    193212        'f_min_ratio' => array( 'default'=> null ),
    194213        'f_max_ratio' => array( 'default'=> null ),
    195         'f_with_thumbnail' => array( 'default'=> false ),
     214        'f_max_level' => array( 'default'=> null ),
    196215      ),
    197216      'Returns elements for the corresponding tags. Note that tag_id, tag_url_name, tag_name an be arrays. Fill at least one of them. '
Note: See TracChangeset for help on using the changeset viewer.