source: trunk/include/derivative_std_params.inc.php @ 30939

Last change on this file since 30939 was 26461, checked in by mistic100, 11 years ago

Update headers to 2014. Happy new year!!

File size: 7.3 KB
RevLine 
[12796]1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
[12796]6// +-----------------------------------------------------------------------+
7// | This program is free software; you can redistribute it and/or modify  |
8// | it under the terms of the GNU General Public License as published by  |
9// | the Free Software Foundation                                          |
10// |                                                                       |
11// | This program is distributed in the hope that it will be useful, but   |
12// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
13// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
14// | General Public License for more details.                              |
15// |                                                                       |
16// | You should have received a copy of the GNU General Public License     |
17// | along with this program; if not, write to the Free Software           |
18// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
19// | USA.                                                                  |
20// +-----------------------------------------------------------------------+
21
[25754]22/**
23 * @package Derivatives
24 */
25
26
[12796]27define('IMG_SQUARE', 'square');
28define('IMG_THUMB', 'thumb');
[13683]29define('IMG_XXSMALL', '2small');
30define('IMG_XSMALL', 'xsmall');
[12796]31define('IMG_SMALL', 'small');
32define('IMG_MEDIUM', 'medium');
33define('IMG_LARGE', 'large');
34define('IMG_XLARGE', 'xlarge');
35define('IMG_XXLARGE', 'xxlarge');
36define('IMG_CUSTOM', 'custom');
37
[25754]38
39/**
40 * Container for watermark configuration.
41 */
[12851]42final class WatermarkParams
43{
[25754]44  /** @var string */
[12851]45  public $file = '';
[25754]46  /** @var int[] */
[12851]47  public $min_size = array(500,500);
[25754]48  /** @var int */
[12851]49  public $xpos = 50;
[25754]50  /** @var int */
[12851]51  public $ypos = 50;
[25754]52  /** @var int */
[12851]53  public $xrepeat = 0;
[25754]54  /** @var int */
[12851]55  public $opacity = 100;
56}
57
58
[25754]59/**
60 * Container for standard derivatives parameters.
61 */
[12796]62final class ImageStdParams
63{
[25754]64  /** @var string[] */
[13683]65  private static $all_types = array(
[25754]66    IMG_SQUARE, IMG_THUMB, IMG_XXSMALL, IMG_XSMALL, IMG_SMALL,
67    IMG_MEDIUM, IMG_LARGE, IMG_XLARGE, IMG_XXLARGE
[13683]68    );
[25754]69  /** @var DerivativeParams[] */
[12796]70  private static $all_type_map = array();
[25754]71  /** @var DerivativeParams[] */
[12796]72  private static $type_map = array();
[25754]73  /** @var DerivativeParams[] */
[12796]74  private static $undefined_type_map = array();
[25754]75  /** @var WatermarkParams */
[12851]76  private static $watermark;
[25754]77  /** @var array */
[13021]78  public static $custom = array();
[25754]79  /** @var int */
[14649]80  public static $quality=95;
[12820]81
[25754]82  /**
83   * @return string[]
84   */
[12796]85  static function get_all_types()
86  {
87    return self::$all_types;
88  }
[12820]89
[25754]90  /**
91   * @return DerivativeParams[]
92   */
[12796]93  static function get_all_type_map()
94  {
95    return self::$all_type_map;
96  }
97
[25754]98  /**
99   * @return DerivativeParams[]
100   */
[12796]101  static function get_defined_type_map()
102  {
103    return self::$type_map;
104  }
105
[25754]106  /**
107   * @return DerivativeParams[]
108   */
[12796]109  static function get_undefined_type_map()
110  {
111    return self::$undefined_type_map;
112  }
[12820]113
[25754]114  /**
115   * @return DerivativeParams
116   */
[12796]117  static function get_by_type($type)
118  {
119    return self::$all_type_map[$type];
120  }
[13038]121
[25754]122  /**
123   * @param int $w
124   * @param int $h
125   * @param float $crop
126   * @param int $minw
127   * @param int $minh
128   * @return DerivativeParams
129   */
[13021]130  static function get_custom($w, $h, $crop=0, $minw=null, $minh=null)
131  {
132    $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) );
133    self::apply_global($params);
[12820]134
[13021]135    $key = array();
136    $params->add_url_tokens($key);
137    $key = implode('_',$key);
138    if ( @self::$custom[$key] < time() - 24*3600)
139    {
140      self::$custom[$key] = time();
141      self::save();
142    }
143    return $params;
144  }
145
[25754]146  /**
147   * @return WatermarkParams
148   */
[12851]149  static function get_watermark()
150  {
151    return self::$watermark;
152  }
153
[25754]154  /**
155   * Loads derivative configuration from database or initializes it.
156   */
[12796]157  static function load_from_db()
158  {
[12820]159    global $conf;
160    $arr = @unserialize($conf['derivatives']);
161    if (false!==$arr)
162    {
163      self::$type_map = $arr['d'];
[12851]164      self::$watermark = @$arr['w'];
165      if (!self::$watermark) self::$watermark = new WatermarkParams();
[13038]166      self::$custom = @$arr['c'];
167      if (!self::$custom) self::$custom = array();
[14649]168      if (isset($arr['q'])) self::$quality = $arr['q'];
[12820]169    }
170    else
171    {
[14228]172      self::$watermark = new WatermarkParams();
173      self::$type_map = self::get_default_sizes();
[14649]174      self::save();
[12820]175    }
[12796]176    self::build_maps();
177  }
178
[25754]179  /**
180   * @param WatermarkParams $watermark
181   */
[12851]182  static function set_watermark($watermark)
183  {
184    self::$watermark = $watermark;
185  }
[13038]186
[25754]187  /**
188   * @see ImageStdParams::save()
189   *
190   * @param DerivativeParams[] $map
191   */
[12820]192  static function set_and_save($map)
193  {
194    self::$type_map = $map;
[13021]195    self::save();
196    self::build_maps();
197  }
[12820]198
[25754]199  /**
200   * Saves the configuration in database.
201   */
[13021]202  static function save()
203  {
204    global $conf;
205
[12820]206    $ser = serialize( array(
[12851]207      'd' => self::$type_map,
[14649]208      'q' => self::$quality,
[12851]209      'w' => self::$watermark,
[13021]210      'c' => self::$custom,
[12820]211      ) );
212    conf_update_param('derivatives', addslashes($ser) );
213  }
214
[25754]215  /**
216   * @return DerivativeParams[]
217   */
[14228]218  static function get_default_sizes()
[12796]219  {
[14228]220    $arr = array(
221      IMG_SQUARE => new DerivativeParams( SizingParams::square(120,120) ),
222      IMG_THUMB => new DerivativeParams( SizingParams::classic(144,144) ),
223      IMG_XXSMALL => new DerivativeParams( SizingParams::classic(240,240) ),
224      IMG_XSMALL => new DerivativeParams( SizingParams::classic(432,324) ),
225      IMG_SMALL => new DerivativeParams( SizingParams::classic(576,432) ),
226      IMG_MEDIUM => new DerivativeParams( SizingParams::classic(792,594) ),
227      IMG_LARGE => new DerivativeParams( SizingParams::classic(1008,756) ),
228      IMG_XLARGE => new DerivativeParams( SizingParams::classic(1224,918) ),
229      IMG_XXLARGE => new DerivativeParams( SizingParams::classic(1656,1242) ),
230    );
[25754]231    $now = time();
[14228]232    foreach($arr as $params)
233    {
[25754]234      $params->last_mod_time = $now;
[14228]235    }
236    return $arr;
[12796]237  }
[12820]238
[25754]239  /**
240   * Compute 'apply_watermark'
241   *
242   * @param DerivativeParams $params
243   */
[13021]244  static function apply_global($params)
[12851]245  {
[14550]246    $params->use_watermark = !empty(self::$watermark->file) &&
[12851]247        (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0]
[14581]248        or self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] );
[12851]249  }
[13038]250
[25754]251  /**
252   * Build 'type_map', 'all_type_map' and 'undefined_type_map'.
253   */
[12796]254  private static function build_maps()
255  {
256    foreach (self::$type_map as $type=>$params)
257    {
258      $params->type = $type;
[12851]259      self::apply_global($params);
[12796]260    }
261    self::$all_type_map = self::$type_map;
262
263    for ($i=0; $i<count(self::$all_types); $i++)
264    {
265      $tocheck = self::$all_types[$i];
266      if (!isset(self::$type_map[$tocheck]))
267      {
268        for ($j=$i-1; $j>=0; $j--)
269        {
270          $target = self::$all_types[$j];
271          if (isset(self::$type_map[$target]))
272          {
273            self::$all_type_map[$tocheck] = self::$type_map[$target];
274            self::$undefined_type_map[$tocheck] = $target;
275            break;
276          }
277        }
278      }
279    }
280  }
281}
282
283?>
Note: See TracBrowser for help on using the repository browser.