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

Last change on this file since 13021 was 13021, checked in by rvelices, 12 years ago

feature 2548 multisize - custom sizes restricted to those requested by theme/plugin
code refacto

File size: 6.1 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_SMALL', 'small');
25define('IMG_MEDIUM', 'medium');
26define('IMG_LARGE', 'large');
27define('IMG_XLARGE', 'xlarge');
28define('IMG_XXLARGE', 'xxlarge');
29define('IMG_CUSTOM', 'custom');
30
31final class WatermarkParams
32{
33  public $file = '';
34  public $min_size = array(500,500);
35  public $xpos = 50;
36  public $ypos = 50;
37  public $xrepeat = 0;
38  public $opacity = 100;
39}
40
41
42final class ImageStdParams
43{
44  private static $all_types = array(IMG_SQUARE,IMG_THUMB,IMG_SMALL,IMG_MEDIUM,IMG_LARGE,IMG_XLARGE,IMG_XXLARGE);
45  private static $all_type_map = array();
46  private static $type_map = array();
47  private static $undefined_type_map = array();
48  private static $watermark;
49  public static $custom = array();
50
51  static function get_all_types()
52  {
53    return self::$all_types;
54  }
55
56  static function get_all_type_map()
57  {
58    return self::$all_type_map;
59  }
60
61  static function get_defined_type_map()
62  {
63    return self::$type_map;
64  }
65
66  static function get_undefined_type_map()
67  {
68    return self::$undefined_type_map;
69  }
70
71  static function get_by_type($type)
72  {
73    return self::$all_type_map[$type];
74  }
75 
76  static function get_custom($w, $h, $crop=0, $minw=null, $minh=null)
77  {
78    $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) );
79    self::apply_global($params);
80
81    $key = array();
82    $params->add_url_tokens($key);
83    $key = implode('_',$key);
84    if ( @self::$custom[$key] < time() - 24*3600)
85    {
86      self::$custom[$key] = time();
87      self::save();
88    }
89    return $params;
90  }
91
92  static function get_watermark()
93  {
94    return self::$watermark;
95  }
96
97  static function load_from_db()
98  {
99    global $conf;
100    $arr = @unserialize($conf['derivatives']);
101    if (false!==$arr)
102    {
103      self::$type_map = $arr['d'];
104      self::$watermark = @$arr['w'];
105      if (!self::$watermark) self::$watermark = new WatermarkParams();
106    }
107    else
108    {
109      self::make_default();
110    }
111    self::build_maps();
112  }
113
114  static function load_from_file()
115  {
116    global $conf;
117    $arr = @unserialize(@file_get_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat'));
118    if (false!==$arr)
119    {
120      self::$type_map = $arr['d'];
121      self::$watermark = @$arr['w'];
122      if (!self::$watermark) self::$watermark = new WatermarkParams();
123      self::$custom = @$arr['c'];
124      if (!self::$custom) self::$custom = array();
125    }
126    else
127    {
128      self::make_default();
129    }
130    self::build_maps();
131  }
132
133  static function set_watermark($watermark)
134  {
135    self::$watermark = $watermark;
136  }
137 
138  static function set_and_save($map)
139  {
140    self::$type_map = $map;
141    self::save();
142    self::build_maps();
143  }
144
145  static function save()
146  {
147    global $conf;
148
149    $ser = serialize( array(
150      'd' => self::$type_map,
151      'w' => self::$watermark,
152      'c' => self::$custom,
153      ) );
154    conf_update_param('derivatives', addslashes($ser) );
155    file_put_contents(PHPWG_ROOT_PATH.$conf['data_location'].'derivatives.dat', $ser);
156  }
157
158  private static function make_default()
159  {
160    self::$watermark = new WatermarkParams();
161    self::$type_map[IMG_SQUARE] = new DerivativeParams( SizingParams::square(100,100) );
162    self::$type_map[IMG_THUMB] = new DerivativeParams( SizingParams::classic(144,144) );
163    self::$type_map[IMG_SMALL] = new DerivativeParams( SizingParams::classic(240,240) );
164    self::$type_map[IMG_MEDIUM] = new DerivativeParams( SizingParams::classic(432,432) );
165    self::$type_map[IMG_LARGE] = new DerivativeParams( SizingParams::classic(648,576) );
166    self::$type_map[IMG_XLARGE] = new DerivativeParams( SizingParams::classic(864,648) );
167    self::$type_map[IMG_XXLARGE] = new DerivativeParams( SizingParams::classic(1200,900) );
168  }
169
170  static function apply_global($params)
171  {
172    if (!empty(self::$watermark->file) &&
173        (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0]
174        && self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ) )
175    {
176      $params->use_watermark = true;
177    }
178  }
179 
180  private static function build_maps()
181  {
182    foreach (self::$type_map as $type=>$params)
183    {
184      $params->type = $type;
185      self::apply_global($params);
186    }
187    self::$all_type_map = self::$type_map;
188
189    for ($i=0; $i<count(self::$all_types); $i++)
190    {
191      $tocheck = self::$all_types[$i];
192      if (!isset(self::$type_map[$tocheck]))
193      {
194        for ($j=$i-1; $j>=0; $j--)
195        {
196          $target = self::$all_types[$j];
197          if (isset(self::$type_map[$target]))
198          {
199            self::$all_type_map[$tocheck] = self::$type_map[$target];
200            self::$undefined_type_map[$tocheck] = $target;
201            break;
202          }
203        }
204      }
205    }
206  }
207
208}
209
210?>
Note: See TracBrowser for help on using the repository browser.