feature 2548 multisize - custom sizes restricted to those requested by theme/plugin

code refacto

git-svn-id: http://piwigo.org/svn/trunk@13021 68402e56-0260-453c-a942-63ccdbb3a9ee
This commit is contained in:
rvelices
2012-02-02 20:59:41 +00:00
parent a68826cbb3
commit 18036a70fc
9 changed files with 142 additions and 112 deletions
+14 -50
View File
@@ -33,21 +33,20 @@ function size_to_url($s)
return $s[0].'x'.$s[1];
}
function url_to_size($s)
{
$pos = strpos($s, 'x');
if ($pos===false)
{
return array((int)$s, (int)$s);
}
return array((int)substr($s,0,$pos), (int)substr($s,$pos+1));
}
function size_equals($s1, $s2)
{
return ($s1[0]==$s2[0] && $s1[1]==$s2[1]);
}
function char_to_fraction($c)
{
return (ord($c) - ord('a'))/25;
}
function fraction_to_char($f)
{
return ord('a') + round($f*25);
}
/** small utility to manipulate a 'rectangle'*/
final class ImageRect
@@ -79,8 +78,8 @@ final class ImageRect
if (!empty($coi))
{
$coil = floor($this->r * (ord($coi[0]) - ord('a'))/25);
$coir = ceil($this->r * (ord($coi[2]) - ord('a'))/25);
$coil = floor($this->r * char_to_fraction($coi[0]));
$coir = ceil($this->r * char_to_fraction($coi[2]));
$availableL = $coil > $this->l ? $coil - $this->l : 0;
$availableR = $coir < $this->r ? $this->r - $coir : 0;
if ($availableL + $availableR <= $pixels)
@@ -115,8 +114,8 @@ final class ImageRect
if (!empty($coi))
{
$coit = floor($this->b * (ord($coi[1]) - ord('a'))/25);
$coib = ceil($this->b * (ord($coi[3]) - ord('a'))/25);
$coit = floor($this->b * char_to_fraction($coi[1]));
$coib = ceil($this->b * char_to_fraction($coi[3]));
$availableT = $coit > $this->t ? $coit - $this->t : 0;
$availableB = $coib < $this->b ? $this->b - $coib : 0;
if ($availableT + $availableB <= $pixels)
@@ -179,39 +178,11 @@ final class SizingParams
else
{
$tokens[] = size_to_url($this->ideal_size);
$tokens[] = sprintf('%02x', round(100*$this->max_crop) );
$tokens[] = fraction_to_char($this->max_crop);
$tokens[] = size_to_url($this->min_size);
}
}
static function from_url_tokens($tokens)
{
if (count($tokens)<1)
throw new Exception('Empty array while parsing Sizing');
$token = array_shift($tokens);
if ($token[0]=='s')
{
return new SizingParams( url_to_size( substr($token,1) ) );
}
if ($token[0]=='e')
{
$s = url_to_size( substr($token,1) );
return new SizingParams($s, 1, $s);
}
$ideal_size = url_to_size( $token );
if (count($tokens)<2)
throw new Exception('Sizing arr');
$token = array_shift($tokens);
$crop = hexdec($token) / 100;
$token = array_shift($tokens);
$min_size = url_to_size( $token );
return new SizingParams($ideal_size, $crop, $min_size);
}
function compute($in_size, $coi, &$crop_rect, &$scale_size)
{
$destCrop = new ImageRect($in_size);
@@ -302,13 +273,6 @@ final class DerivativeParams
$this->sizing->add_url_tokens($tokens);
}
static function from_url_tokens($tokens)
{
$sizing = SizingParams::from_url_tokens($tokens);
$ret = new DerivativeParams($sizing);
return $ret;
}
function compute_final_size($in_size, $coi)
{
$this->sizing->compute( $in_size, $coi, $crop_rect, $scale_size );
+28 -3
View File
@@ -46,6 +46,7 @@ final class ImageStdParams
private static $type_map = array();
private static $undefined_type_map = array();
private static $watermark;
public static $custom = array();
static function get_all_types()
{
@@ -71,6 +72,22 @@ final class ImageStdParams
{
return self::$all_type_map[$type];
}
static function get_custom($w, $h, $crop=0, $minw=null, $minh=null)
{
$params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) );
self::apply_global($params);
$key = array();
$params->add_url_tokens($key);
$key = implode('_',$key);
if ( @self::$custom[$key] < time() - 24*3600)
{
self::$custom[$key] = time();
self::save();
}
return $params;
}
static function get_watermark()
{
@@ -103,6 +120,8 @@ final class ImageStdParams
self::$type_map = $arr['d'];
self::$watermark = @$arr['w'];
if (!self::$watermark) self::$watermark = new WatermarkParams();
self::$custom = @$arr['c'];
if (!self::$custom) self::$custom = array();
}
else
{
@@ -118,16 +137,22 @@ final class ImageStdParams
static function set_and_save($map)
{
global $conf;
self::$type_map = $map;
self::save();
self::build_maps();
}
static function save()
{
global $conf;
$ser = serialize( array(
'd' => self::$type_map,
'w' => self::$watermark,
'c' => self::$custom,
) );
conf_update_param('derivatives', addslashes($ser) );
file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat', $ser);
self::build_maps();
}
private static function make_default()
@@ -142,7 +167,7 @@ final class ImageStdParams
self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1200,900) );
}
public static function apply_global($params)
static function apply_global($params)
{
if (!empty(self::$watermark->file) &&
(self::$watermark->min_size[0]<=$params->sizing->ideal_size[0]
+4 -18
View File
@@ -126,26 +126,12 @@ SELECT
if ($row['nb_comments'] > 0)
{
// comments order (get, session, conf)
if (!empty($_GET['comments_order']))
if (!empty($_GET['comments_order']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
{
if (in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
{
$comments_order = $_GET['comments_order'];
pwg_set_session_var('comments_order', $comments_order);
}
else
{
$comments_order = $conf['comments_order'];
}
}
else if (pwg_get_session_var('comments_order') !== null)
{
$comments_order = pwg_get_session_var('comments_order');
}
else
{
$comments_order = $conf['comments_order'];
pwg_set_session_var('comments_order', $_GET['comments_order']);
}
$comments_order = pwg_get_session_var('comments_order', $conf['comments_order']);
$template->assign(array(
'COMMENTS_ORDER_URL' => duplicate_picture_url().'&amp;comments_order='.($comments_order == 'ASC' ? 'DESC' : 'ASC'),
'COMMENTS_ORDER_TITLE' => $comments_order == 'ASC' ? l10n('ascending') : l10n('descending'),
+14 -12
View File
@@ -559,31 +559,33 @@ class Template {
!empty($params['width']) or fatal_error('define_derviative missing width');
!empty($params['height']) or fatal_error('define_derviative missing height');
$derivative = new DerivativeParams( SizingParams::classic( intval($params['width']), intval($params['height'])) );
$w = intval($params['width']);
$h = intval($params['height']);
$crop = 0;
$minw=null;
$minh=null;
if (isset($params['crop']))
{
if (is_bool($params['crop']))
{
$derivative->sizing->max_crop = $params['crop'] ? 1:0;
$crop = $params['crop'] ? 1:0;
}
else
{
$derivative->sizing->max_crop = round($params['crop']/100, 2);
$crop = round($params['crop']/100, 2);
}
if ($derivative->sizing->max_crop)
if ($crop)
{
$minw = empty($params['min_width']) ? $derivative->max_width() : intval($params['min_width']);
$minw <= $derivative->max_width() or fatal_error('define_derviative invalid min_width');
$minh = empty($params['min_height']) ? $derivative->max_height() : intval($params['min_height']);
$minh <= $derivative->max_height() or fatal_error('define_derviative invalid min_height');
$derivative->sizing->min_size = array($minw, $minh);
$minw = empty($params['min_width']) ? $w : intval($params['min_width']);
$minw <= $w or fatal_error('define_derviative invalid min_width');
$minh = empty($params['min_height']) ? $h : intval($params['min_height']);
$minh <= $h or fatal_error('define_derviative invalid min_height');
}
}
ImageStdParams::apply_global($derivative);
$smarty->assign( $params['name'], $derivative);
$smarty->assign( $params['name'], ImageStdParams::get_custom($w, $h, $crop, $minw, $minh) );
}
/**