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

Last change on this file since 14581 was 14581, checked in by plg, 12 years ago

use watermark if minX OR minY (not AND)

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