1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2013 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 | define('IMG_SQUARE', 'square'); |
---|
23 | define('IMG_THUMB', 'thumb'); |
---|
24 | define('IMG_XXSMALL', '2small'); |
---|
25 | define('IMG_XSMALL', 'xsmall'); |
---|
26 | define('IMG_SMALL', 'small'); |
---|
27 | define('IMG_MEDIUM', 'medium'); |
---|
28 | define('IMG_LARGE', 'large'); |
---|
29 | define('IMG_XLARGE', 'xlarge'); |
---|
30 | define('IMG_XXLARGE', 'xxlarge'); |
---|
31 | define('IMG_CUSTOM', 'custom'); |
---|
32 | |
---|
33 | final 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 | |
---|
44 | final 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 | public static $quality=95; |
---|
55 | |
---|
56 | static function get_all_types() |
---|
57 | { |
---|
58 | return self::$all_types; |
---|
59 | } |
---|
60 | |
---|
61 | static function get_all_type_map() |
---|
62 | { |
---|
63 | return self::$all_type_map; |
---|
64 | } |
---|
65 | |
---|
66 | static function get_defined_type_map() |
---|
67 | { |
---|
68 | return self::$type_map; |
---|
69 | } |
---|
70 | |
---|
71 | static function get_undefined_type_map() |
---|
72 | { |
---|
73 | return self::$undefined_type_map; |
---|
74 | } |
---|
75 | |
---|
76 | static function get_by_type($type) |
---|
77 | { |
---|
78 | return self::$all_type_map[$type]; |
---|
79 | } |
---|
80 | |
---|
81 | static function get_custom($w, $h, $crop=0, $minw=null, $minh=null) |
---|
82 | { |
---|
83 | $params = new DerivativeParams( new SizingParams( array($w,$h), $crop, array($minw,$minh)) ); |
---|
84 | self::apply_global($params); |
---|
85 | |
---|
86 | $key = array(); |
---|
87 | $params->add_url_tokens($key); |
---|
88 | $key = implode('_',$key); |
---|
89 | if ( @self::$custom[$key] < time() - 24*3600) |
---|
90 | { |
---|
91 | self::$custom[$key] = time(); |
---|
92 | self::save(); |
---|
93 | } |
---|
94 | return $params; |
---|
95 | } |
---|
96 | |
---|
97 | static function get_watermark() |
---|
98 | { |
---|
99 | return self::$watermark; |
---|
100 | } |
---|
101 | |
---|
102 | static function load_from_db() |
---|
103 | { |
---|
104 | global $conf; |
---|
105 | $arr = @unserialize($conf['derivatives']); |
---|
106 | if (false!==$arr) |
---|
107 | { |
---|
108 | self::$type_map = $arr['d']; |
---|
109 | self::$watermark = @$arr['w']; |
---|
110 | if (!self::$watermark) self::$watermark = new WatermarkParams(); |
---|
111 | self::$custom = @$arr['c']; |
---|
112 | if (!self::$custom) self::$custom = array(); |
---|
113 | if (isset($arr['q'])) self::$quality = $arr['q']; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | self::$watermark = new WatermarkParams(); |
---|
118 | self::$type_map = self::get_default_sizes(); |
---|
119 | self::save(); |
---|
120 | } |
---|
121 | self::build_maps(); |
---|
122 | } |
---|
123 | |
---|
124 | static function set_watermark($watermark) |
---|
125 | { |
---|
126 | self::$watermark = $watermark; |
---|
127 | } |
---|
128 | |
---|
129 | static function set_and_save($map) |
---|
130 | { |
---|
131 | self::$type_map = $map; |
---|
132 | self::save(); |
---|
133 | self::build_maps(); |
---|
134 | } |
---|
135 | |
---|
136 | static function save() |
---|
137 | { |
---|
138 | global $conf; |
---|
139 | |
---|
140 | $ser = serialize( array( |
---|
141 | 'd' => self::$type_map, |
---|
142 | 'q' => self::$quality, |
---|
143 | 'w' => self::$watermark, |
---|
144 | 'c' => self::$custom, |
---|
145 | ) ); |
---|
146 | conf_update_param('derivatives', addslashes($ser) ); |
---|
147 | } |
---|
148 | |
---|
149 | static function get_default_sizes() |
---|
150 | { |
---|
151 | $arr = array( |
---|
152 | IMG_SQUARE => new DerivativeParams( SizingParams::square(120,120) ), |
---|
153 | IMG_THUMB => new DerivativeParams( SizingParams::classic(144,144) ), |
---|
154 | IMG_XXSMALL => new DerivativeParams( SizingParams::classic(240,240) ), |
---|
155 | IMG_XSMALL => new DerivativeParams( SizingParams::classic(432,324) ), |
---|
156 | IMG_SMALL => new DerivativeParams( SizingParams::classic(576,432) ), |
---|
157 | IMG_MEDIUM => new DerivativeParams( SizingParams::classic(792,594) ), |
---|
158 | IMG_LARGE => new DerivativeParams( SizingParams::classic(1008,756) ), |
---|
159 | IMG_XLARGE => new DerivativeParams( SizingParams::classic(1224,918) ), |
---|
160 | IMG_XXLARGE => new DerivativeParams( SizingParams::classic(1656,1242) ), |
---|
161 | ); |
---|
162 | foreach($arr as $params) |
---|
163 | { |
---|
164 | $params->last_mod_time = time(); |
---|
165 | } |
---|
166 | return $arr; |
---|
167 | } |
---|
168 | |
---|
169 | static function apply_global($params) |
---|
170 | { |
---|
171 | $params->use_watermark = !empty(self::$watermark->file) && |
---|
172 | (self::$watermark->min_size[0]<=$params->sizing->ideal_size[0] |
---|
173 | or self::$watermark->min_size[1]<=$params->sizing->ideal_size[1] ); |
---|
174 | } |
---|
175 | |
---|
176 | private static function build_maps() |
---|
177 | { |
---|
178 | foreach (self::$type_map as $type=>$params) |
---|
179 | { |
---|
180 | $params->type = $type; |
---|
181 | self::apply_global($params); |
---|
182 | } |
---|
183 | self::$all_type_map = self::$type_map; |
---|
184 | |
---|
185 | for ($i=0; $i<count(self::$all_types); $i++) |
---|
186 | { |
---|
187 | $tocheck = self::$all_types[$i]; |
---|
188 | if (!isset(self::$type_map[$tocheck])) |
---|
189 | { |
---|
190 | for ($j=$i-1; $j>=0; $j--) |
---|
191 | { |
---|
192 | $target = self::$all_types[$j]; |
---|
193 | if (isset(self::$type_map[$target])) |
---|
194 | { |
---|
195 | self::$all_type_map[$tocheck] = self::$type_map[$target]; |
---|
196 | self::$undefined_type_map[$tocheck] = $target; |
---|
197 | break; |
---|
198 | } |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | } |
---|
205 | |
---|
206 | ?> |
---|