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

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

multisize - added the coi (still to affine the admin ui + language)
multisize - derivatives can be revuild from a larger derviative instead of the original

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