1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2014 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 | |
---|
22 | /** |
---|
23 | * @package Derivatives |
---|
24 | */ |
---|
25 | |
---|
26 | |
---|
27 | define('IMG_SQUARE', 'square'); |
---|
28 | define('IMG_THUMB', 'thumb'); |
---|
29 | define('IMG_XXSMALL', '2small'); |
---|
30 | define('IMG_XSMALL', 'xsmall'); |
---|
31 | define('IMG_SMALL', 'small'); |
---|
32 | define('IMG_MEDIUM', 'medium'); |
---|
33 | define('IMG_LARGE', 'large'); |
---|
34 | define('IMG_XLARGE', 'xlarge'); |
---|
35 | define('IMG_XXLARGE', 'xxlarge'); |
---|
36 | define('IMG_CUSTOM', 'custom'); |
---|
37 | |
---|
38 | |
---|
39 | /** |
---|
40 | * Container for watermark configuration. |
---|
41 | */ |
---|
42 | final class WatermarkParams |
---|
43 | { |
---|
44 | /** @var string */ |
---|
45 | public $file = ''; |
---|
46 | /** @var int[] */ |
---|
47 | public $min_size = array(500,500); |
---|
48 | /** @var int */ |
---|
49 | public $xpos = 50; |
---|
50 | /** @var int */ |
---|
51 | public $ypos = 50; |
---|
52 | /** @var int */ |
---|
53 | public $xrepeat = 0; |
---|
54 | /** @var int */ |
---|
55 | public $opacity = 100; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | /** |
---|
60 | * Container for standard derivatives parameters. |
---|
61 | */ |
---|
62 | final class ImageStdParams |
---|
63 | { |
---|
64 | /** @var string[] */ |
---|
65 | private static $all_types = array( |
---|
66 | IMG_SQUARE, IMG_THUMB, IMG_XXSMALL, IMG_XSMALL, IMG_SMALL, |
---|
67 | IMG_MEDIUM, IMG_LARGE, IMG_XLARGE, IMG_XXLARGE |
---|
68 | ); |
---|
69 | /** @var DerivativeParams[] */ |
---|
70 | private static $all_type_map = array(); |
---|
71 | /** @var DerivativeParams[] */ |
---|
72 | private static $type_map = array(); |
---|
73 | /** @var DerivativeParams[] */ |
---|
74 | private static $undefined_type_map = array(); |
---|
75 | /** @var WatermarkParams */ |
---|
76 | private static $watermark; |
---|
77 | /** @var array */ |
---|
78 | public static $custom = array(); |
---|
79 | /** @var int */ |
---|
80 | public static $quality=95; |
---|
81 | |
---|
82 | /** |
---|
83 | * @return string[] |
---|
84 | */ |
---|
85 | static function get_all_types() |
---|
86 | { |
---|
87 | return self::$all_types; |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * @return DerivativeParams[] |
---|
92 | */ |
---|
93 | static function get_all_type_map() |
---|
94 | { |
---|
95 | return self::$all_type_map; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * @return DerivativeParams[] |
---|
100 | */ |
---|
101 | static function get_defined_type_map() |
---|
102 | { |
---|
103 | return self::$type_map; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * @return DerivativeParams[] |
---|
108 | */ |
---|
109 | static function get_undefined_type_map() |
---|
110 | { |
---|
111 | return self::$undefined_type_map; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * @return DerivativeParams |
---|
116 | */ |
---|
117 | static function get_by_type($type) |
---|
118 | { |
---|
119 | return self::$all_type_map[$type]; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * @param int $w |
---|
124 | * @param int $h |
---|
125 | * @param float $crop |
---|
126 | * @param int $minw |
---|
127 | * @param int $minh |
---|
128 | * @return DerivativeParams |
---|
129 | */ |
---|
130 | static function get_custom($w, $h, $crop=0, $minw=null, $minh=null) |
---|
131 | { |
---|
132 | $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) ); |
---|
133 | self::apply_global($params); |
---|
134 | |
---|
135 | $key = array(); |
---|
136 | $params->add_url_tokens($key); |
---|
137 | $key = implode('_',$key); |
---|
138 | if ( @self::$custom[$key] < time() - 24*3600) |
---|
139 | { |
---|
140 | self::$custom[$key] = time(); |
---|
141 | self::save(); |
---|
142 | } |
---|
143 | return $params; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * @return WatermarkParams |
---|
148 | */ |
---|
149 | static function get_watermark() |
---|
150 | { |
---|
151 | return self::$watermark; |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Loads derivative configuration from database or initializes it. |
---|
156 | */ |
---|
157 | static function load_from_db() |
---|
158 | { |
---|
159 | global $conf; |
---|
160 | $arr = @unserialize($conf['derivatives']); |
---|
161 | if (false!==$arr) |
---|
162 | { |
---|
163 | self::$type_map = $arr['d']; |
---|
164 | self::$watermark = @$arr['w']; |
---|
165 | if (!self::$watermark) self::$watermark = new WatermarkParams(); |
---|
166 | self::$custom = @$arr['c']; |
---|
167 | if (!self::$custom) self::$custom = array(); |
---|
168 | if (isset($arr['q'])) self::$quality = $arr['q']; |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | self::$watermark = new WatermarkParams(); |
---|
173 | self::$type_map = self::get_default_sizes(); |
---|
174 | self::save(); |
---|
175 | } |
---|
176 | self::build_maps(); |
---|
177 | } |
---|
178 | |
---|
179 | /** |
---|
180 | * @param WatermarkParams $watermark |
---|
181 | */ |
---|
182 | static function set_watermark($watermark) |
---|
183 | { |
---|
184 | self::$watermark = $watermark; |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * @see ImageStdParams::save() |
---|
189 | * |
---|
190 | * @param DerivativeParams[] $map |
---|
191 | */ |
---|
192 | static function set_and_save($map) |
---|
193 | { |
---|
194 | self::$type_map = $map; |
---|
195 | self::save(); |
---|
196 | self::build_maps(); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Saves the configuration in database. |
---|
201 | */ |
---|
202 | static function save() |
---|
203 | { |
---|
204 | global $conf; |
---|
205 | |
---|
206 | $ser = serialize( array( |
---|
207 | 'd' => self::$type_map, |
---|
208 | 'q' => self::$quality, |
---|
209 | 'w' => self::$watermark, |
---|
210 | 'c' => self::$custom, |
---|
211 | ) ); |
---|
212 | conf_update_param('derivatives', addslashes($ser) ); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * @return DerivativeParams[] |
---|
217 | */ |
---|
218 | static function get_default_sizes() |
---|
219 | { |
---|
220 | $arr = array( |
---|
221 | IMG_SQUARE => new DerivativeParams( SizingParams::square(120,120) ), |
---|
222 | IMG_THUMB => new DerivativeParams( SizingParams::classic(144,144) ), |
---|
223 | IMG_XXSMALL => new DerivativeParams( SizingParams::classic(240,240) ), |
---|
224 | IMG_XSMALL => new DerivativeParams( SizingParams::classic(432,324) ), |
---|
225 | IMG_SMALL => new DerivativeParams( SizingParams::classic(576,432) ), |
---|
226 | IMG_MEDIUM => new DerivativeParams( SizingParams::classic(792,594) ), |
---|
227 | IMG_LARGE => new DerivativeParams( SizingParams::classic(1008,756) ), |
---|
228 | IMG_XLARGE => new DerivativeParams( SizingParams::classic(1224,918) ), |
---|
229 | IMG_XXLARGE => new DerivativeParams( SizingParams::classic(1656,1242) ), |
---|
230 | ); |
---|
231 | $now = time(); |
---|
232 | foreach($arr as $params) |
---|
233 | { |
---|
234 | $params->last_mod_time = $now; |
---|
235 | } |
---|
236 | return $arr; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Compute 'apply_watermark' |
---|
241 | * |
---|
242 | * @param DerivativeParams $params |
---|
243 | */ |
---|
244 | static function apply_global($params) |
---|
245 | { |
---|
246 | $params->use_watermark = !empty(self::$watermark->file) && |
---|
247 | (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0] |
---|
248 | or self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * Build 'type_map', 'all_type_map' and 'undefined_type_map'. |
---|
253 | */ |
---|
254 | private static function build_maps() |
---|
255 | { |
---|
256 | foreach (self::$type_map as $type=>$params) |
---|
257 | { |
---|
258 | $params->type = $type; |
---|
259 | self::apply_global($params); |
---|
260 | } |
---|
261 | self::$all_type_map = self::$type_map; |
---|
262 | |
---|
263 | for ($i=0; $i<count(self::$all_types); $i++) |
---|
264 | { |
---|
265 | $tocheck = self::$all_types[$i]; |
---|
266 | if (!isset(self::$type_map[$tocheck])) |
---|
267 | { |
---|
268 | for ($j=$i-1; $j>=0; $j--) |
---|
269 | { |
---|
270 | $target = self::$all_types[$j]; |
---|
271 | if (isset(self::$type_map[$target])) |
---|
272 | { |
---|
273 | self::$all_type_map[$tocheck] = self::$type_map[$target]; |
---|
274 | self::$undefined_type_map[$tocheck] = $target; |
---|
275 | break; |
---|
276 | } |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | ?> |
---|