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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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  {
Note: See TracChangeset for help on using the changeset viewer.