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

derivatives - can display several sizes on picture page

File:
1 edited

Legend:

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