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

Revision 13683, 6.5 KB checked in by plg, 8 years ago (diff)

feature 2598: add sizes XXS and XS

new label for sizes

On picture.php, the current size is "checked" and javascript refreshed when switched (with jQuery)

jQuery loaded by default on header.tpl (already loaded by thumbnails.tpl)

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 load_from_file()
121  {
122    global $conf;
123    $arr = @unserialize(@file_get_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat'));
124    if (false!==$arr)
125    {
126      self::$type_map = $arr['d'];
127      self::$watermark = @$arr['w'];
128      if (!self::$watermark) self::$watermark = new WatermarkParams();
129      self::$custom = @$arr['c'];
130      if (!self::$custom) self::$custom = array();
131    }
132    else
133    {
134      self::make_default();
135    }
136    self::build_maps();
137  }
138
139  static function set_watermark($watermark)
140  {
141    self::$watermark = $watermark;
142  }
143
144  static function set_and_save($map)
145  {
146    self::$type_map = $map;
147    self::save();
148    self::build_maps();
149  }
150
151  static function save()
152  {
153    global $conf;
154
155    $ser = serialize( array(
156      'd' => self::$type_map,
157      'w' => self::$watermark,
158      'c' => self::$custom,
159      ) );
160    conf_update_param('derivatives', addslashes($ser) );
161    file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat', $ser);
162  }
163
164  private static function make_default()
165  {
166    self::$watermark = new WatermarkParams();
167    self::$type_map[IMG_SQUARE] = new DerivativeParams( SizingParams::square(120,120) );
168    self::$type_map[IMG_THUMB] = new DerivativeParams( SizingParams::classic(144,144) );
169    self::$type_map[IMG_XXSMALL] = new DerivativeParams( SizingParams::classic(240,240) );
170    self::$type_map[IMG_XSMALL] = new DerivativeParams( SizingParams::classic(432,324) );
171    self::$type_map[IMG_SMALL] = new DerivativeParams( SizingParams::classic(576,432) );
172    self::$type_map[IMG_MEDIUM] = new DerivativeParams( SizingParams::classic(792,594) );
173    self::$type_map[IMG_LARGE] = new DerivativeParams( SizingParams::classic(1008,756) );
174    self::$type_map[IMG_XLARGE] = new DerivativeParams( SizingParams::classic(1224,918) );
175    self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1656,1242) );
176  }
177
178  static function apply_global($params)
179  {
180    if (!empty(self::$watermark->file) &&
181        (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0]
182        && self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ) )
183    {
184      $params->use_watermark = true;
185    }
186  }
187
188  private static function build_maps()
189  {
190    foreach (self::$type_map as $type=>$params)
191    {
192      $params->type = $type;
193      self::apply_global($params);
194    }
195    self::$all_type_map = self::$type_map;
196
197    for ($i=0; $i<count(self::$all_types); $i++)
198    {
199      $tocheck = self::$all_types[$i];
200      if (!isset(self::$type_map[$tocheck]))
201      {
202        for ($j=$i-1; $j>=0; $j--)
203        {
204          $target = self::$all_types[$j];
205          if (isset(self::$type_map[$target]))
206          {
207            self::$all_type_map[$tocheck] = self::$type_map[$target];
208            self::$undefined_type_map[$tocheck] = $target;
209            break;
210          }
211        }
212      }
213    }
214  }
215
216}
217
218?>
Note: See TracBrowser for help on using the repository browser.