Changeset 12775


Ignore:
Timestamp:
Dec 21, 2011, 2:51:44 PM (9 years ago)
Author:
rvelices
Message:

derivatives - can display several sizes on picture page

Location:
extensions/derivatives
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • extensions/derivatives/i.php

    r12770 r12775  
    105105
    106106  $deriv = explode('_', $deriv);
    107   foreach (ImageStdParams::get_all_types() as $type)
     107  foreach (ImageStdParams::get_defined_type_map() as $type => $params)
    108108  {
    109109    if (substr($type,0,2) == $deriv[0])
    110110    {
    111111      $page['derivative_type'] = $type;
    112       $page['derivative_params'] = ImageStdParams::get_by_type($type);
     112      $page['derivative_params'] = $params;
    113113      break;
    114114    }
  • extensions/derivatives/include/derivative.inc.php

    r12770 r12775  
    44{
    55  public $rel_path;
     6 
    67  public $coi=null;
    78  private $size=null;
    8  
     9
    910  function __construct($infos)
    1011  {
     
    2728      $this->rel_path = get_themeconf('mime_icon_dir').strtolower($ext).'.png';
    2829    }
    29    
     30
    3031    $this->coi = @$infos['coi'];
    3132    if (isset($infos['width']) && isset($infos['height']))
     
    3435    }
    3536  }
    36  
     37
     38  function has_size()
     39  {
     40    return $this->size != null;
     41  }
     42
    3743  function get_size()
    3844  {
     
    4349}
    4450
     51
     52
    4553final class DerivativeImage
    4654{
     55  const SAME_AS_SRC = 0x10;
     56
    4757  public $src_image;
     58
     59  private $requested_type;
     60
     61  private $flags = 0;
    4862  private $params;
    4963  private $rel_path, $rel_url;
    50  
    51   function __construct($type, $infos)
    52   {
    53     $this->src_image = new SrcImage($infos);
    54     self::build($type, $src_image, $this->rel_path, $this->rel_url, $this->params);
    55   }
    56  
     64
     65  function __construct($type, $src_image)
     66  {
     67    $this->src_image = $src_image;
     68    if (is_string($type))
     69    {
     70      $this->requested_type = $type;
     71      $this->params = ImageStdParams::get_by_type($type);
     72    }
     73    else
     74    {
     75      $this->requested_type = IMG_CUSTOM;
     76      $this->params = $type;
     77    }
     78
     79    self::build($src_image, $this->params, $this->rel_path, $this->rel_url, $this->flags);
     80  }
     81
    5782  static function thumb_url($infos)
    5883  {
    5984    $src_image = new SrcImage($infos);
    60     self::build(IMG_THUMB, $src_image, $rel_path, $rel_url);
    61     //die($rel_url);
     85    self::build($src_image, ImageStdParams::get_by_type(IMG_THUMB), $rel_path, $rel_url);
    6286    return get_root_url().$rel_url;
    6387  }
     
    6690  {
    6791    $src_image = new SrcImage($infos);
    68     self::build($type, $src_image, $rel_path, $rel_url);
     92    $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type;
     93    self::build($src_image, $params, $rel_path, $rel_url);
    6994    return get_root_url().$rel_url;
    7095  }
    71  
    72   private static function build($type, $src, &$rel_path, &$rel_url, &$params = null)
    73   {
    74     if (is_string($type))
    75     {
    76       $params = ImageStdParams::get_by_type($type);
    77     }
    78     else
    79     {
    80       $type = IMG_CUSTOM;
     96
     97  static function get_all($infos)
     98  {
     99    $src_image = new SrcImage($infos);
     100    $ret = array();
     101    foreach (ImageStdParams::get_defined_type_map() as $type => $params)
     102    {
     103      $derivative = new DerivativeImage($params, $src_image);
     104      $ret[$type] = $derivative;
     105    }
     106    foreach (ImageStdParams::get_undefined_type_map() as $type => $type2)
     107    {
     108      $ret[$type] = $ret[$type2];
    81109    }
    82110   
    83     // todo check if we should not downgrade to a different type because
    84     // original file is too small ...
    85     // we are using a mime type icon ...
    86     // etc...
    87    
     111    return $ret;
     112  }
     113
     114  private static function build($src, &$params, &$rel_path, &$rel_url, &$flags = null)
     115  {
     116    if ( $src->has_size() && $params->is_identity( $src->get_size() ) )
     117    {
     118      // todo - what if we have a watermark maybe return a smaller size?
     119      $flags |= self::SAME_AS_SRC;
     120      $params = null;
     121      $rel_path = $rel_url = $src->rel_path;
     122      return;
     123    }
     124
    88125    $tokens=array();
    89     $tokens[] = substr($type,0,2);
    90    
     126    $tokens[] = substr($params->type,0,2);
     127
    91128    if (!empty($src->coi))
    92129    {
    93130      $tokens[] = 'ci'.$src->coi;
    94131    }
    95    
    96     if ($type==IMG_CUSTOM)
     132
     133    if ($params->type==IMG_CUSTOM)
    97134    {
    98135      $params->add_url_tokens($tokens);
    99136    }
    100    
     137
    101138    $loc = $src->rel_path;
    102139    if (substr_compare($loc, '../', 0, 3)==0)
     
    107144
    108145    $rel_path = PWG_DERIVATIVE_DIR.$loc;
    109    
     146
    110147    global $conf;
    111148    $url_style=$conf['derivative_url_style'];
     
    122159      }
    123160    }
    124    
     161
    125162    if ($url_style == 2)
    126163    {
     
    136173  }
    137174
     175  function get_url()
     176  {
     177    return get_root_url().$this->rel_url;
     178  }
     179
     180  function same_as_source()
     181  {
     182    return $this->flags & self::SAME_AS_SRC;
     183  }
     184
     185
    138186  /* returns the size of the derivative image*/
    139187  function get_size()
    140188  {
     189    if ($this->flags & self::SAME_AS_SRC)
     190    {
     191      return $this->src_image->get_size();
     192    }
    141193    return $this->params->compute_final_size($this->src_image->get_size(), $this->src_image->coi);
    142194  }
     195
     196  function get_size_htm()
     197  {
     198    $size = $this->get_size();
     199    if ($size)
     200    {
     201      return 'width="'.$size[0].'" height="'.$size[1].'"';
     202    }
     203  }
     204
     205  function get_size_hr()
     206  {
     207    $size = $this->get_size();
     208    if ($size)
     209    {
     210      return $size[0].' x '.$size[1];
     211    }
     212  }
     213
    143214}
    144215
  • extensions/derivatives/include/derivative_params.inc.php

    r12770 r12775  
    3030{
    3131  public $l,$t,$r,$b;
    32  
     32
    3333  function __construct($l)
    3434  {
     
    3737    $this->b = $l[1];
    3838  }
    39  
     39
    4040  function width()
    4141  {
    4242    return $this->r - $this->l;
    4343  }
    44  
     44
    4545  function height()
    4646  {
     
    5353      return;
    5454    $tlcrop = floor($pixels/2);
    55    
     55
    5656    if (!empty($coi))
    5757    {
     
    8383    $this->r -= $pixels - $tlcrop;
    8484  }
    85  
     85
    8686  function crop_v($pixels, $coi, $force)
    8787  {
     
    8989      return;
    9090    $tlcrop = floor($pixels/2);
    91    
     91
    9292    if (!empty($coi))
    9393    {
     
    132132    $this->min_size = $min_size;
    133133  }
    134  
     134
    135135  static function classic($w, $h)
    136136  {
     
    142142    return new SizingParams( array($w,$w), 1, array($w,$w) );
    143143  }
    144  
     144
    145145  function add_url_tokens(&$tokens)
    146146  {
    147       if (!$this->could_crop())
     147      if ($this->max_crop == 0)
    148148      {
    149149        $tokens[] = 's'.size_to_url($this->ideal_size);
     
    160160      }
    161161  }
    162  
     162
    163163  static function from_url_tokens($tokens)
    164164  {
     
    175175      return new SizingParams($s, 1, $s);
    176176    }
    177    
     177
    178178    $ideal_size = url_to_size( $token );
    179179    if (count($tokens)<2)
    180180      throw new Exception('Sizing arr');
    181      
     181
    182182    $token = array_shift($tokens);
    183183    $crop = sscanf('%02x' , $token) / 100;
    184    
     184
    185185    $token = array_shift($tokens);
    186186    $min_size = url_to_size( $token );
    187187    return new SizingParams($ideal_size, $crop, $min_size);
    188188  }
    189  
    190   function could_crop()
    191   {
    192     return $this->max_crop > 0;
    193   }
     189
    194190
    195191  function compute($in_size, $coi, &$crop_rect, &$scale_size)
    196192  {
    197193    $destCrop = new ImageRect($in_size);
    198    
     194
    199195    if ($this->max_crop > 0)
    200196    {
     
    225221      }
    226222    }
    227    
     223
    228224    $scale_size = array($destCrop->width(), $destCrop->height());
    229    
     225
    230226    $ratio_w = $destCrop->width() / $this->ideal_size[0];
    231227    $ratio_h = $destCrop->height() / $this->ideal_size[1];
     
    247243      $scale_size = null;
    248244    }
    249    
     245
    250246    $crop_rect = null;
    251247    if ($destCrop->width()!=$in_size[0] || $destCrop->height()!=$in_size[1] )
     
    261257final class ImageParams
    262258{
     259  public $type = IMG_CUSTOM;
    263260  public $sizing;
    264  
     261
    265262  function __construct($sizing)
    266263  {
    267264    $this->sizing = $sizing;
    268265  }
    269  
     266
    270267  function add_url_tokens(&$tokens)
    271268  {
    272269    $this->sizing->add_url_tokens($tokens);
    273270  }
    274  
     271
    275272  static function from_url_tokens($tokens)
    276273  {
     
    279276    return $ret;
    280277  }
    281  
     278
    282279  function compute_final_size($in_size, $coi)
    283280  {
     
    285282    return $scale_size != null ? $scale_size : $in_size;
    286283  }
     284
     285  function is_identity($in_size)
     286  {
     287    if ($in_size[0] > $this->sizing->ideal_size[0] or
     288        $in_size[1] > $this->sizing->ideal_size[1] )
     289    {
     290      return false;
     291    }
     292    return true;
     293  }
    287294}
    288295?>
  • extensions/derivatives/include/derivative_std_params.inc.php

    r12770 r12775  
    11<?php
    22
    3 define('IMG_TINY', 'tiny');
     3define('IMG_SQUARE', 'square');
    44define('IMG_THUMB', 'thumb');
    55define('IMG_SMALL', 'small');
     
    1212final class ImageStdParams
    1313{
    14   private static $all_types = array();
     14  private static $all_types = array(IMG_SQUARE,IMG_THUMB,IMG_SMALL,IMG_MEDIUM,IMG_LARGE,IMG_XLARGE,IMG_XXLARGE);
     15  private static $all_type_map = array();
    1516  private static $type_map = array();
     17  private static $undefined_type_map = array();
    1618 
    1719  static function get_all_types()
     
    2022  }
    2123 
     24  static function get_all_type_map()
     25  {
     26    return self::$all_type_map;
     27  }
     28
     29  static function get_defined_type_map()
     30  {
     31    return self::$type_map;
     32  }
     33
     34  static function get_undefined_type_map()
     35  {
     36    return self::$undefined_type_map;
     37  }
     38 
    2239  static function get_by_type($type)
    2340  {
    24     return self::$type_map[$type];
     41    return self::$all_type_map[$type];
    2542  }
    2643 
     
    2845  {
    2946    self::make_default();
     47    self::build_maps();
    3048  }
    3149
     
    3351  {
    3452    self::make_default();
     53    self::build_maps();
    3554  }
    3655
     
    3857  {
    3958    //todo
    40     self::$type_map[IMG_TINY] = new ImageParams( SizingParams::square(100,100) );
     59    self::$type_map[IMG_SQUARE] = new ImageParams( SizingParams::square(100,100) );
    4160    self::$type_map[IMG_THUMB] = new ImageParams( SizingParams::classic(144,144) );
    4261    self::$type_map[IMG_SMALL] = new ImageParams( SizingParams::classic(240,240) );
    4362    self::$type_map[IMG_MEDIUM] = new ImageParams( SizingParams::classic(432,432) );
    4463    self::$type_map[IMG_LARGE] = new ImageParams( SizingParams::classic(864,648) );
    45     self::$all_types = array_keys(self::$type_map);
     64    self::$type_map[IMG_XLARGE] = new ImageParams( SizingParams::classic(1200,900) );
    4665  }
     66 
     67  private static function build_maps()
     68  {
     69    foreach (self::$type_map as $type=>$params)
     70    {
     71      $params->type = $type;
     72    }
     73    self::$all_type_map = self::$type_map;
     74
     75    for ($i=0; $i<count(self::$all_types); $i++)
     76    {
     77      $tocheck = self::$all_types[$i];
     78      if (!isset(self::$type_map[$tocheck]))
     79      {
     80        for ($j=$i-1; $j>=0; $j--)
     81        {
     82          $target = self::$all_types[$j];
     83          if (isset(self::$type_map[$target]))
     84          {
     85            self::$all_type_map[$tocheck] = self::$type_map[$target];
     86            self::$undefined_type_map[$tocheck] = $target;
     87            break;
     88          }
     89        }
     90      }
     91    }
     92  }
     93 
    4794}
    4895
  • extensions/derivatives/main.inc.php

    r12770 r12775  
    2424}
    2525
     26
     27add_event_handler(
     28  'render_element_content',
     29  'dyn_render_picture_content',
     30  EVENT_HANDLER_PRIORITY_NEUTRAL-1,
     31  2
     32  );
     33
     34function dyn_render_picture_content($content, $element_info)
     35{
     36  if ( !empty($content) )
     37  {// someone hooked us - so we skip;
     38    return $content;
     39  }
     40
     41  $all_derivatives = DerivativeImage::get_all($element_info);
     42  //var_export($all_derivatives);
     43  $selected_derivative = $all_derivatives[IMG_LARGE];
     44
     45  $available_derivatives = array();
     46  $added = array();
     47  foreach($all_derivatives as $type => $derivative)
     48  {
     49    $url = $derivative->get_url();
     50    if (isset($added[$url]))
     51      continue;
     52    $added[$url] = 1;
     53    $available_derivatives[] = $type;
     54  }
     55
     56
     57  global $user, $page, $template;
     58
     59  $template->set_filenames(
     60    array('default_content'=> dirname(__FILE__).'/picture_content.tpl')
     61    );
     62
     63  $template->append('current', array(
     64      'all_derivatives' => $all_derivatives,
     65      'selected_derivative' => $selected_derivative,
     66      'available_derivative_types' => $available_derivatives,
     67    ), true);
     68
     69  $template->assign( array(
     70    'ALT_IMG' => $element_info['file'] ));
     71
     72  return $template->parse( 'default_content', true);;
     73}
    2674?>
Note: See TracChangeset for help on using the changeset viewer.