Changeset 13021


Ignore:
Timestamp:
Feb 2, 2012, 9:59:41 PM (12 years ago)
Author:
rvelices
Message:

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

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/admin/derivatives.php

    r12922 r13021  
    215215        if (isset($enabled[$type]))
    216216        {// now disabled, before was enabled
     217          $changed_types[] = $type;
    217218          $disabled[$type] = $enabled[$type];
    218219          unset($enabled[$type]);
  • trunk/admin/include/functions.php

    r13013 r13021  
    203203
    204204    $ok = true;
    205     foreach ($files as $path)
    206     {
    207       if (is_file($path) and !unlink($path))
    208       {
    209         $ok = false;
    210         trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
    211         break;
    212       }
    213     }
     205                if (!isset($conf['never_delete_originals']))
     206                {
     207                        foreach ($files as $path)
     208                        {
     209                                if (is_file($path) and !unlink($path))
     210                                {
     211                                        $ok = false;
     212                                        trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING);
     213                                        break;
     214                                }
     215                        }
     216                }
    214217
    215218    if ($ok)
  • trunk/admin/themes/default/template/derivatives_build.tpl

    r12865 r13021  
    4545{footer_script require='jquery.effects.slide'}{literal}
    4646
    47 var loader = new ImageLoader( {onChanged: loaderChanged} )
     47var loader = new ImageLoader( {onChanged: loaderChanged, maxRequests:1 } )
    4848        , pending_next_page = null
    4949        , last_image_show_time = 0
  • trunk/i.php

    r12958 r13021  
    6666function ilog()
    6767{
    68   global $conf, $ilogfh;
     68  global $conf;
    6969  if (!$conf['enable_i_log']) return;
    70   if(!$ilogfh)
    71   {
    72     $dir=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/';
    73     if (!mkgetdir($dir) or ! ($ilogfh=fopen($dir.'i.log', 'a')) )
    74       return;
    75   }
     70
    7671  $line = date("c");
    7772  foreach( func_get_args() as $arg)
     
    8782    }
    8883  }
    89   fwrite($ilogfh, $line."\n");
     84        $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/i.log';
     85  if (false == file_put_contents($file, $line."\n", FILE_APPEND))
     86        {
     87                mkgetdir(dirname($file));
     88        }
    9089}
    9190
     
    125124}
    126125
     126function url_to_size($s)
     127{
     128  $pos = strpos($s, 'x');
     129  if ($pos===false)
     130  {
     131    return array((int)$s, (int)$s);
     132  }
     133  return array((int)substr($s,0,$pos), (int)substr($s,$pos+1));
     134}
     135
     136function parse_custom_params($tokens)
     137{
     138  if (count($tokens)<1)
     139    ierror('Empty array while parsing Sizing', 400);
     140
     141  $crop = 0;
     142  $min_size = null;
     143
     144  $token = array_shift($tokens);
     145  if ($token[0]=='s')
     146  {
     147    $size = url_to_size( substr($token,1) );
     148  }
     149  elseif ($token[0]=='e')
     150  {
     151    $crop = 1;
     152    $size = $min_size = url_to_size( substr($token,1) );
     153  }
     154  else
     155  {
     156    $size = url_to_size( $token );
     157    if (count($tokens)<2)
     158      ierror('Sizing arr', 400);
     159
     160    $token = array_shift($tokens);
     161    $crop = char_to_fraction($token);
     162
     163    $token = array_shift($tokens);
     164    $min_size = url_to_size( $token );
     165  }
     166  return new DerivativeParams( new SizingParams($size, $crop, $min_size) );
     167}
     168
    127169function parse_request()
    128170{
     
    200242  if ($page['derivative_type'] == IMG_CUSTOM)
    201243  {
    202     try
    203     {
    204       $params = $page['derivative_params'] = DerivativeParams::from_url_tokens($deriv);
    205     }
    206     catch (Exception $e)
    207     {
    208       ierror($e->getMessage(), 400);
    209     }
     244    $params = $page['derivative_params'] = parse_custom_params($deriv);
     245
    210246    if ($params->sizing->ideal_size[0] < 20 or $params->sizing->ideal_size[1] < 20)
    211247    {
     
    215251    {
    216252      ierror('Invalid crop', 400);
     253    }
     254    $greatest = ImageStdParams::get_by_type(IMG_XXLARGE);
     255    if ($params->max_width() > $greatest->max_width() || $params->max_height() > $greatest->max_height())
     256    {
     257      ierror('Too big', 403);
     258    }
     259   
     260    $key = array();
     261    $params->add_url_tokens($key);
     262    $key = implode('_', $key);
     263    if (!isset(ImageStdParams::$custom[$key]))
     264    {
     265      ierror('Size not allowed', 403);
    217266    }
    218267  }
  • trunk/include/derivative_params.inc.php

    r12908 r13021  
    3434}
    3535
    36 function url_to_size($s)
    37 {
    38   $pos = strpos($s, 'x');
    39   if ($pos===false)
    40   {
    41     return array((int)$s, (int)$s);
    42   }
    43   return array((int)substr($s,0,$pos), (int)substr($s,$pos+1));
    44 }
    45 
    4636function size_equals($s1, $s2)
    4737{
     
    4939}
    5040
     41function char_to_fraction($c)
     42{
     43        return (ord($c) - ord('a'))/25;
     44}
     45
     46function fraction_to_char($f)
     47{
     48        return ord('a') + round($f*25);
     49}
    5150
    5251/** small utility to manipulate a 'rectangle'*/
     
    8079    if (!empty($coi))
    8180    {
    82       $coil = floor($this->r * (ord($coi[0]) - ord('a'))/25);
    83       $coir = ceil($this->r * (ord($coi[2]) - ord('a'))/25);
     81      $coil = floor($this->r * char_to_fraction($coi[0]));
     82      $coir = ceil($this->r * char_to_fraction($coi[2]));
    8483      $availableL = $coil > $this->l ? $coil - $this->l : 0;
    8584      $availableR = $coir < $this->r ? $this->r - $coir : 0;
     
    116115    if (!empty($coi))
    117116    {
    118       $coit = floor($this->b * (ord($coi[1]) - ord('a'))/25);
    119       $coib = ceil($this->b * (ord($coi[3]) - ord('a'))/25);
     117      $coit = floor($this->b * char_to_fraction($coi[1]));
     118      $coib = ceil($this->b * char_to_fraction($coi[3]));
    120119      $availableT = $coit > $this->t ? $coit - $this->t : 0;
    121120      $availableB = $coib < $this->b ? $this->b - $coib : 0;
     
    180179      {
    181180        $tokens[] = size_to_url($this->ideal_size);
    182         $tokens[] = sprintf('%02x', round(100*$this->max_crop) );
     181        $tokens[] = fraction_to_char($this->max_crop);
    183182        $tokens[] = size_to_url($this->min_size);
    184183      }
    185184  }
    186 
    187   static function from_url_tokens($tokens)
    188   {
    189     if (count($tokens)<1)
    190       throw new Exception('Empty array while parsing Sizing');
    191     $token = array_shift($tokens);
    192     if ($token[0]=='s')
    193     {
    194       return new SizingParams( url_to_size( substr($token,1) ) );
    195     }
    196     if ($token[0]=='e')
    197     {
    198       $s = url_to_size( substr($token,1) );
    199       return new SizingParams($s, 1, $s);
    200     }
    201 
    202     $ideal_size = url_to_size( $token );
    203     if (count($tokens)<2)
    204       throw new Exception('Sizing arr');
    205 
    206     $token = array_shift($tokens);
    207     $crop = hexdec($token) / 100;
    208 
    209     $token = array_shift($tokens);
    210     $min_size = url_to_size( $token );
    211     return new SizingParams($ideal_size, $crop, $min_size);
    212   }
    213 
    214185
    215186  function compute($in_size, $coi, &$crop_rect, &$scale_size)
     
    303274  }
    304275
    305   static function from_url_tokens($tokens)
    306   {
    307     $sizing = SizingParams::from_url_tokens($tokens);
    308     $ret = new DerivativeParams($sizing);
    309     return $ret;
    310   }
    311 
    312276  function compute_final_size($in_size, $coi)
    313277  {
  • trunk/include/derivative_std_params.inc.php

    r12851 r13021  
    4747  private static $undefined_type_map = array();
    4848  private static $watermark;
     49  public static $custom = array();
    4950
    5051  static function get_all_types()
     
    7172  {
    7273    return self::$all_type_map[$type];
     74  }
     75 
     76  static function get_custom($w, $h, $crop=0, $minw=null, $minh=null)
     77  {
     78    $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) );
     79    self::apply_global($params);
     80
     81    $key = array();
     82    $params->add_url_tokens($key);
     83    $key = implode('_',$key);
     84    if ( @self::$custom[$key] < time() - 24*3600)
     85    {
     86      self::$custom[$key] = time();
     87      self::save();
     88    }
     89    return $params;
    7390  }
    7491
     
    104121      self::$watermark = @$arr['w'];
    105122      if (!self::$watermark) self::$watermark = new WatermarkParams();
     123      self::$custom = @$arr['c'];
     124      if (!self::$custom) self::$custom = array();
    106125    }
    107126    else
     
    119138  static function set_and_save($map)
    120139  {
     140    self::$type_map = $map;
     141    self::save();
     142    self::build_maps();
     143  }
     144
     145  static function save()
     146  {
    121147    global $conf;
    122     self::$type_map = $map;
    123148
    124149    $ser = serialize( array(
    125150      'd' => self::$type_map,
    126151      'w' => self::$watermark,
     152      'c' => self::$custom,
    127153      ) );
    128154    conf_update_param('derivatives', addslashes($ser) );
    129155    file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat', $ser);
    130     self::build_maps();
    131156  }
    132157
     
    143168  }
    144169
    145   public static function apply_global($params)
     170  static function apply_global($params)
    146171  {
    147172    if (!empty(self::$watermark->file) &&
  • trunk/include/picture_comment.inc.php

    r12922 r13021  
    127127  {
    128128    // comments order (get, session, conf)
    129     if (!empty($_GET['comments_order']))
     129    if (!empty($_GET['comments_order']) && in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
    130130    {
    131       if (in_array(strtoupper($_GET['comments_order']), array('ASC', 'DESC')))
    132       {
    133         $comments_order = $_GET['comments_order'];
    134         pwg_set_session_var('comments_order', $comments_order);
    135       }
    136       else
    137       {
    138         $comments_order = $conf['comments_order'];
    139       }
     131      pwg_set_session_var('comments_order', $_GET['comments_order']);
    140132    }
    141     else if (pwg_get_session_var('comments_order') !== null)
    142     {
    143       $comments_order = pwg_get_session_var('comments_order');
    144     }
    145     else
    146     {
    147       $comments_order = $conf['comments_order'];
    148     }
     133    $comments_order = pwg_get_session_var('comments_order', $conf['comments_order']);
     134
    149135    $template->assign(array(
    150136      'COMMENTS_ORDER_URL' => duplicate_picture_url().'&amp;comments_order='.($comments_order == 'ASC' ? 'DESC' : 'ASC'),
  • trunk/include/template.class.php

    r12955 r13021  
    560560    !empty($params['height']) or fatal_error('define_derviative missing height');
    561561
    562     $derivative = new DerivativeParams( SizingParams::classic( intval($params['width']), intval($params['height'])) );
     562    $w = intval($params['width']);
     563    $h = intval($params['height']);
     564    $crop = 0;
     565    $minw=null;
     566    $minh=null;
     567   
    563568    if (isset($params['crop']))
    564569    {
    565570      if (is_bool($params['crop']))
    566571      {
    567         $derivative->sizing->max_crop = $params['crop'] ? 1:0;
     572        $crop = $params['crop'] ? 1:0;
    568573      }
    569574      else
    570575      {
    571         $derivative->sizing->max_crop = round($params['crop']/100, 2);
    572       }
    573 
    574       if ($derivative->sizing->max_crop)
    575       {
    576         $minw = empty($params['min_width']) ? $derivative->max_width() : intval($params['min_width']);
    577         $minw <= $derivative->max_width() or fatal_error('define_derviative invalid min_width');
    578         $minh = empty($params['min_height']) ? $derivative->max_height() : intval($params['min_height']);
    579         $minh <= $derivative->max_height() or fatal_error('define_derviative invalid min_height');
    580 
    581         $derivative->sizing->min_size = array($minw, $minh);
    582       }
    583     }
    584 
    585     ImageStdParams::apply_global($derivative);
    586     $smarty->assign( $params['name'], $derivative);
     576        $crop = round($params['crop']/100, 2);
     577      }
     578
     579      if ($crop)
     580      {
     581        $minw = empty($params['min_width']) ? $w : intval($params['min_width']);
     582        $minw <= $w or fatal_error('define_derviative invalid min_width');
     583        $minh = empty($params['min_height']) ? $h : intval($params['min_height']);
     584        $minh <= $h or fatal_error('define_derviative invalid min_height');
     585      }
     586    }
     587
     588    $smarty->assign( $params['name'], ImageStdParams::get_custom($w, $h, $crop, $minw, $minh) );
    587589  }
    588590
  • trunk/themes/default/template/picture.tpl

    r12894 r13021  
    279279        {if $COMMENT_COUNT > 0}
    280280                <h3>{$pwg->l10n_dec('%d comment', '%d comments',$COMMENT_COUNT)}</h3>
    281         {/if}
    282   {if $COMMENT_COUNT > 2}
    283     {'Sort order'|@translate} : <a href="{$COMMENTS_ORDER_URL}#comments">{$COMMENTS_ORDER_TITLE}</a>
    284   {/if}
     281        {if $COMMENT_COUNT > 2}
     282                {'Sort order'|@translate}: <a href="{$COMMENTS_ORDER_URL}#comments" rel="nofollow">{$COMMENTS_ORDER_TITLE}</a>
     283        {/if}
     284        {/if}
    285285        {if !empty($navbar)}{include file='navigation_bar.tpl'|@get_extent:'navbar'}{/if}
    286286
Note: See TracChangeset for help on using the changeset viewer.