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

Last change on this file since 13736 was 13736, checked in by rvelices, 9 years ago
  • small js fixes on index/picture
  • i.php does not use derivatives.dat file anymore (obsolete file)
  • better selection of derivatives in picture.php
  • larger sharpen real range in image.class.php
File size: 5.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
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
22define('IMG_SQUARE', 'square');
23define('IMG_THUMB', 'thumb');
24define('IMG_XXSMALL', '2small');
25define('IMG_XSMALL', 'xsmall');
26define('IMG_SMALL', 'small');
27define('IMG_MEDIUM', 'medium');
28define('IMG_LARGE', 'large');
29define('IMG_XLARGE', 'xlarge');
30define('IMG_XXLARGE', 'xxlarge');
31define('IMG_CUSTOM', 'custom');
32
33final class WatermarkParams
34{
35  public $file = '';
36  public $min_size = array(500,500);
37  public $xpos = 50;
38  public $ypos = 50;
39  public $xrepeat = 0;
40  public $opacity = 100;
41}
42
43
44final class ImageStdParams
45{
46  private static $all_types = array(
47    IMG_SQUARE,IMG_THUMB,IMG_XXSMALL,IMG_XSMALL,IMG_SMALL,IMG_MEDIUM,IMG_LARGE,IMG_XLARGE,IMG_XXLARGE
48    );
49  private static $all_type_map = array();
50  private static $type_map = array();
51  private static $undefined_type_map = array();
52  private static $watermark;
53  public static $custom = array();
54
55  static function get_all_types()
56  {
57    return self::$all_types;
58  }
59
60  static function get_all_type_map()
61  {
62    return self::$all_type_map;
63  }
64
65  static function get_defined_type_map()
66  {
67    return self::$type_map;
68  }
69
70  static function get_undefined_type_map()
71  {
72    return self::$undefined_type_map;
73  }
74
75  static function get_by_type($type)
76  {
77    return self::$all_type_map[$type];
78  }
79
80  static function get_custom($w, $h, $crop=0, $minw=null, $minh=null)
81  {
82    $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) );
83    self::apply_global($params);
84
85    $key = array();
86    $params->add_url_tokens($key);
87    $key = implode('_',$key);
88    if ( @self::$custom[$key] < time() - 24*3600)
89    {
90      self::$custom[$key] = time();
91      self::save();
92    }
93    return $params;
94  }
95
96  static function get_watermark()
97  {
98    return self::$watermark;
99  }
100
101  static function load_from_db()
102  {
103    global $conf;
104    $arr = @unserialize($conf['derivatives']);
105    if (false!==$arr)
106    {
107      self::$type_map = $arr['d'];
108      self::$watermark = @$arr['w'];
109      if (!self::$watermark) self::$watermark = new WatermarkParams();
110      self::$custom = @$arr['c'];
111      if (!self::$custom) self::$custom = array();
112    }
113    else
114    {
115      self::make_default();
116    }
117    self::build_maps();
118  }
119
120  static function set_watermark($watermark)
121  {
122    self::$watermark = $watermark;
123  }
124
125  static function set_and_save($map)
126  {
127    self::$type_map = $map;
128    self::save();
129    self::build_maps();
130  }
131
132  static function save()
133  {
134    global $conf;
135
136    $ser = serialize( array(
137      'd' => self::$type_map,
138      'w' => self::$watermark,
139      'c' => self::$custom,
140      ) );
141    conf_update_param('derivatives', addslashes($ser) );
142  }
143
144  private static function make_default()
145  {
146    self::$watermark = new WatermarkParams();
147    self::$type_map[IMG_SQUARE] = new DerivativeParams( SizingParams::square(120,120) );
148    self::$type_map[IMG_THUMB] = new DerivativeParams( SizingParams::classic(144,144) );
149    self::$type_map[IMG_XXSMALL] = new DerivativeParams( SizingParams::classic(240,240) );
150    self::$type_map[IMG_XSMALL] = new DerivativeParams( SizingParams::classic(432,324) );
151    self::$type_map[IMG_SMALL] = new DerivativeParams( SizingParams::classic(576,432) );
152    self::$type_map[IMG_MEDIUM] = new DerivativeParams( SizingParams::classic(792,594) );
153    self::$type_map[IMG_LARGE] = new DerivativeParams( SizingParams::classic(1008,756) );
154    self::$type_map[IMG_XLARGE] = new DerivativeParams( SizingParams::classic(1224,918) );
155    self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1656,1242) );
156  }
157
158  static function apply_global($params)
159  {
160    if (!empty(self::$watermark->file) &&
161        (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0]
162        && self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ) )
163    {
164      $params->use_watermark = true;
165    }
166  }
167
168  private static function build_maps()
169  {
170    foreach (self::$type_map as $type=>$params)
171    {
172      $params->type = $type;
173      self::apply_global($params);
174    }
175    self::$all_type_map = self::$type_map;
176
177    for ($i=0; $i<count(self::$all_types); $i++)
178    {
179      $tocheck = self::$all_types[$i];
180      if (!isset(self::$type_map[$tocheck]))
181      {
182        for ($j=$i-1; $j>=0; $j--)
183        {
184          $target = self::$all_types[$j];
185          if (isset(self::$type_map[$target]))
186          {
187            self::$all_type_map[$tocheck] = self::$type_map[$target];
188            self::$undefined_type_map[$tocheck] = $target;
189            break;
190          }
191        }
192      }
193    }
194  }
195
196}
197
198?>
Note: See TracBrowser for help on using the repository browser.