From 95a78ca0d3412ffa1d54e13c9caab7bc8b6b0e13 Mon Sep 17 00:00:00 2001 From: rvelices Date: Sun, 1 Jan 2012 21:10:43 +0000 Subject: feature 2541 multisize - admin GUI for choosing derivative parameters + persistence git-svn-id: http://piwigo.org/svn/trunk@12820 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin.php | 1 + admin/derivatives.php | 235 ++++++++++++++++++++++++++ admin/include/functions.php | 24 ++- admin/themes/default/template/admin.tpl | 1 + admin/themes/default/template/derivatives.tpl | 79 +++++++++ i.php | 58 ++++--- include/derivative_params.inc.php | 9 +- include/derivative_std_params.inc.php | 62 +++++-- language/en_UK/common.lang.php | 9 +- 9 files changed, 432 insertions(+), 46 deletions(-) create mode 100644 admin/derivatives.php create mode 100644 admin/themes/default/template/derivatives.tpl diff --git a/admin.php b/admin.php index 382ec76cb..411dcb9e2 100644 --- a/admin.php +++ b/admin.php @@ -165,6 +165,7 @@ $template->assign( 'U_MAINTENANCE'=> $link_start.'maintenance', 'U_NOTIFICATION_BY_MAIL'=> $link_start.'notification_by_mail', 'U_CONFIG_GENERAL'=> $link_start.'configuration', + 'U_CONFIG_DERIVATIVES'=> $link_start.'derivatives', 'U_CONFIG_DISPLAY'=> $conf_link.'default', 'U_CONFIG_EXTENTS'=> $link_start.'extend_for_templates', 'U_CONFIG_MENUBAR'=> $link_start.'menubar', diff --git a/admin/derivatives.php b/admin/derivatives.php new file mode 100644 index 000000000..fbf1ccffb --- /dev/null +++ b/admin/derivatives.php @@ -0,0 +1,235 @@ + &$pderivative) + { + if ($pderivative['must_square'] = ($type==IMG_SQUARE ? true : false)) + { + $pderivative['h'] = $pderivative['w']; + $pderivative['minh'] = $pderivative['minw'] = $pderivative['w']; + $pderivative['crop'] = 100; + } + $pderivative['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; + $pderivative['enabled'] = isset($pderivative['enabled']) || $pderivative['must_enable'] ? true : false; + } + unset($pderivative); + + // step 2 - check validity + $prev_w = $prev_h = 0; + foreach(ImageStdParams::get_all_types() as $type) + { + $pderivative = $pderivatives[$type]; + if (!$pderivative['enabled']) + continue; + + $v = intval($pderivative['w']); + if ($v<=0 || $v<=$prev_w) + { + $errors[$type]['w'] = '>'.$prev_w; + } + $v = intval($pderivative['h']); + if ($v<=0 || $v<=$prev_h) + { + $errors[$type]['h'] = '>'.$prev_h; + } + $v = intval($pderivative['crop']); + if ($v<0 || $v>100) + { + $errors[$type]['crop'] = '[0..100]'; + } + + if ($v!=0) + { + $v = intval($pderivative['minw']); + if ($v<0 || $v>intval($pderivative['w'])) + { + $errors[$type]['minw'] = '[0..'.intval($pderivative['w']).']'; + } + $v = intval($pderivative['minh']); + if ($v<0 || $v>intval($pderivative['h'])) + { + $errors[$type]['minh'] = '[0..'.intval($pderivative['h']).']'; + } + } + + if (count($errors)==0) + { + $prev_w = intval($pderivative['w']); + $prev_h = intval($pderivative['h']); + } + } + // step 3 - save data + if (count($errors)==0) + { + $enabled = ImageStdParams::get_defined_type_map(); + $disabled = @unserialize( @$conf['disabled_derivatives'] ); + if ($disabled===false) + { + $disabled = array(); + } + $changed_types = array(); + + foreach(ImageStdParams::get_all_types() as $type) + { + $pderivative = $pderivatives[$type]; + + if ($pderivative['enabled']) + { + $new_params = new DerivativeParams( + new SizingParams( + array($pderivative['w'],$pderivative['h']), + round($pderivative['crop'] / 100, 2), + array($pderivative['minw'],$pderivative['minh']) + ) + ); + if (isset($enabled[$type])) + { + $old_params = $enabled[$type]; + $same = true; + if ( !size_equals($old_params->sizing->ideal_size, $new_params->sizing->ideal_size) + or $old_params->sizing->max_crop != $new_params->sizing->max_crop) + { + $same = false; + } + + if ( $same && $new_params->sizing->max_crop != 0 + && !size_equals($old_params->sizing->min_size, $new_params->sizing->min_size) ) + { + $same = false; + } + + if (!$same) + { + $new_params->last_mod_time = time(); + $changed_types[] = $type; + } + else + { + $new_params->last_mod_time = $old_params->last_mod_time; + } + $enabled[$type] = $new_params; + } + else + {// now enabled, before was disabled + $enabled[$type] = $new_params; + unset($disabled[$type]); + } + } + else + {// disabled + if (isset($enabled[$type])) + {// now disabled, before was enabled + $disabled[$type] = $enabled[$type]; + unset($enabled[$type]); + } + } + } + + $enabled_by = array(); // keys ordered by all types + foreach(ImageStdParams::get_all_types() as $type) + { + if (isset($enabled[$type])) + { + $enabled_by[$type] = $enabled[$type]; + } + } + ImageStdParams::set_and_save($enabled_by); + if (count($disabled)==0) + { + $query='DELETE FROM '.CONFIG_TABLE.' WHERE param = \'disabled_derivatives\''; + pwg_query($query); + } + else + { + conf_update_param('disabled_derivatives', addslashes(serialize($disabled)) ); + } + $conf['disabled_derivatives']=serialize($disabled); + + if (count($changed_types)) + { + clear_derivative_cache($changed_types); + } + } + else + { + $template->assign('derivatives', $pderivatives); + $template->assign('ferrors', $errors); + } +} + +if (count($errors)==0) +{ + $enabled = ImageStdParams::get_defined_type_map(); + $disabled = @unserialize( @$conf['disabled_derivatives'] ); + if ($disabled===false) + { + $disabled = array(); + } + + $tpl_vars = array(); + foreach(ImageStdParams::get_all_types() as $type) + { + $tpl_var = array(); + + $tpl_var['must_square'] = ($type==IMG_SQUARE ? true : false); + $tpl_var['must_enable'] = ($type==IMG_SQUARE || $type==IMG_THUMB)? true : false; + + if ($params=@$enabled[$type]) + { + $tpl_var['enabled']=true; + } + else + { + $tpl_var['enabled']=false; + $params=@$disabled[$type]; + } + + if ($params) + { + list($tpl_var['w'],$tpl_var['h']) = $params->sizing->ideal_size; + if ( ($tpl_var['crop'] = round(100*$params->sizing->max_crop)) > 0) + { + list($tpl_var['minw'],$tpl_var['minh']) = $params->sizing->min_size; + } + else + { + $tpl_var['minw'] = $tpl_var['minh'] = ""; + } + } + $tpl_vars[$type]=$tpl_var; + } + $template->assign('derivatives', $tpl_vars); +} + +$template->set_filename('derivatives', 'derivatives.tpl'); +$template->assign_var_from_handle('ADMIN_CONTENT', 'derivatives'); +?> \ No newline at end of file diff --git a/admin/include/functions.php b/admin/include/functions.php index 85ee0f4f2..0669c8d6d 100644 --- a/admin/include/functions.php +++ b/admin/include/functions.php @@ -2054,6 +2054,7 @@ function get_active_menu($menu_page) return 4; case 'configuration': + case 'derivatives': case 'extend_for_templates': case 'menubar': case 'themes': @@ -2247,23 +2248,34 @@ SELECT return array_from_query($query, 'user_id'); } -function clear_derivative_cache($type='all') +/** delete all derivative files for one or several types */ +function clear_derivative_cache($types='all') { $pattern='#.*-'; - if ($type == 'all') + if ($types == 'all') + { + $types = ImageStdParams::get_all_types(); + $types[] = IMG_CUSTOM; + } + elseif (!is_array($types)) + { + $types = array($types); + } + + if (count($types)>1) { $type_urls = array(); - foreach(ImageStdParams::get_all_types() as $dtype) + foreach($types as $dtype) { $type_urls[] = derivative_to_url($dtype); } - $type_urls[] = derivative_to_url(IMG_CUSTOM); $pattern .= '(' . implode('|',$type_urls) . ')'; } else { - $pattern .= derivative_to_url($type); + $pattern .= derivative_to_url($types[0]); } + $pattern.='(_[a-zA-Z0-9]+)*\.[a-zA-Z0-9]{3,4}$#'; if ($contents = opendir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR)) { @@ -2320,7 +2332,7 @@ function clear_derivative_cache_rec($path, $pattern) unlink($path.'/index.htm'); } clearstatcache(); - rmdir($path); + @rmdir($path); } return $rmdir; } diff --git a/admin/themes/default/template/admin.tpl b/admin/themes/default/template/admin.tpl index 320e11c7c..c04812090 100644 --- a/admin/themes/default/template/admin.tpl +++ b/admin/themes/default/template/admin.tpl @@ -84,6 +84,7 @@ jQuery(document).ready(function(){ldelim}