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 | |
---|
22 | final class SrcImage |
---|
23 | { |
---|
24 | const IS_ORIGINAL = 0x01; |
---|
25 | const IS_MIMETYPE = 0x02; |
---|
26 | |
---|
27 | public $id; |
---|
28 | public $rel_path; |
---|
29 | |
---|
30 | private $size=null; |
---|
31 | private $flags=0; |
---|
32 | |
---|
33 | function __construct($infos) |
---|
34 | { |
---|
35 | global $conf; |
---|
36 | |
---|
37 | $this->id = $infos['id']; |
---|
38 | $ext = get_extension($infos['path']); |
---|
39 | if (in_array($ext, $conf['picture_ext'])) |
---|
40 | { |
---|
41 | $this->rel_path = $infos['path']; |
---|
42 | $this->flags |= self::IS_ORIGINAL; |
---|
43 | } |
---|
44 | elseif (!empty($infos['representative_ext'])) |
---|
45 | { |
---|
46 | $this->rel_path = original_to_representative($infos['path'], $infos['representative_ext']); |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | $ext = strtolower($ext); |
---|
51 | $this->rel_path = trigger_event('get_mimetype_location', get_themeconf('mime_icon_dir').$ext.'.png', $ext ); |
---|
52 | $this->flags |= self::IS_MIMETYPE; |
---|
53 | $this->size = @getimagesize(PHPWG_ROOT_PATH.$this->rel_path); |
---|
54 | } |
---|
55 | |
---|
56 | if (!$this->size && isset($infos['width']) && isset($infos['height'])) |
---|
57 | { |
---|
58 | $this->size = array($infos['width'], $infos['height']); |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | function is_original() |
---|
63 | { |
---|
64 | return $this->flags & self::IS_ORIGINAL; |
---|
65 | } |
---|
66 | |
---|
67 | function is_mimetype() |
---|
68 | { |
---|
69 | return $this->flags & self::IS_MIMETYPE; |
---|
70 | } |
---|
71 | |
---|
72 | function get_path() |
---|
73 | { |
---|
74 | return PHPWG_ROOT_PATH.$this->rel_path; |
---|
75 | } |
---|
76 | |
---|
77 | function get_url() |
---|
78 | { |
---|
79 | return embellish_url(get_root_url().$this->rel_path); |
---|
80 | } |
---|
81 | |
---|
82 | function has_size() |
---|
83 | { |
---|
84 | return $this->size != null; |
---|
85 | } |
---|
86 | |
---|
87 | function get_size() |
---|
88 | { |
---|
89 | if ($this->size == null) |
---|
90 | not_implemented(); // get size from file |
---|
91 | return $this->size; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | final class DerivativeImage |
---|
98 | { |
---|
99 | public $src_image; |
---|
100 | |
---|
101 | private $params; |
---|
102 | private $rel_path, $rel_url; |
---|
103 | |
---|
104 | function __construct($type, $src_image) |
---|
105 | { |
---|
106 | $this->src_image = $src_image; |
---|
107 | if (is_string($type)) |
---|
108 | { |
---|
109 | $this->params = ImageStdParams::get_by_type($type); |
---|
110 | } |
---|
111 | else |
---|
112 | { |
---|
113 | $this->params = $type; |
---|
114 | } |
---|
115 | |
---|
116 | self::build($src_image, $this->params, $this->rel_path, $this->rel_url); |
---|
117 | } |
---|
118 | |
---|
119 | static function thumb_url($infos) |
---|
120 | { |
---|
121 | return self::url(IMG_THUMB, $infos); |
---|
122 | } |
---|
123 | |
---|
124 | static function url($type, $infos) |
---|
125 | { |
---|
126 | $src_image = is_object($infos) ? $infos : new SrcImage($infos); |
---|
127 | $params = is_string($type) ? ImageStdParams::get_by_type($type) : $type; |
---|
128 | self::build($src_image, $params, $rel_path, $rel_url); |
---|
129 | if ($params == null) |
---|
130 | { |
---|
131 | return $src_image->get_url(); |
---|
132 | } |
---|
133 | return embellish_url( |
---|
134 | trigger_event('get_derivative_url', |
---|
135 | get_root_url().$rel_url, |
---|
136 | $params, $src_image, $rel_url |
---|
137 | ) ); |
---|
138 | } |
---|
139 | |
---|
140 | static function get_all($src_image) |
---|
141 | { |
---|
142 | $ret = array(); |
---|
143 | foreach (ImageStdParams::get_defined_type_map() as $type => $params) |
---|
144 | { |
---|
145 | $derivative = new DerivativeImage($params, $src_image); |
---|
146 | $ret[$type] = $derivative; |
---|
147 | } |
---|
148 | foreach (ImageStdParams::get_undefined_type_map() as $type => $type2) |
---|
149 | { |
---|
150 | $ret[$type] = $ret[$type2]; |
---|
151 | } |
---|
152 | |
---|
153 | return $ret; |
---|
154 | } |
---|
155 | |
---|
156 | private static function build($src, &$params, &$rel_path, &$rel_url) |
---|
157 | { |
---|
158 | if ( $src->has_size() && $params->is_identity( $src->get_size() ) ) |
---|
159 | { |
---|
160 | // todo - what if we have a watermark maybe return a smaller size? |
---|
161 | $params = null; |
---|
162 | $rel_path = $rel_url = $src->rel_path; |
---|
163 | return; |
---|
164 | } |
---|
165 | |
---|
166 | $tokens=array(); |
---|
167 | $tokens[] = substr($params->type,0,2); |
---|
168 | |
---|
169 | if ($params->type==IMG_CUSTOM) |
---|
170 | { |
---|
171 | $params->add_url_tokens($tokens); |
---|
172 | } |
---|
173 | |
---|
174 | $loc = $src->rel_path; |
---|
175 | if (substr_compare($loc, './', 0, 2)==0) |
---|
176 | { |
---|
177 | $loc = substr($loc, 2); |
---|
178 | } |
---|
179 | elseif (substr_compare($loc, '../', 0, 3)==0) |
---|
180 | { |
---|
181 | $loc = substr($loc, 3); |
---|
182 | } |
---|
183 | $loc = substr_replace($loc, '-'.implode('_', $tokens), strrpos($loc, '.'), 0 ); |
---|
184 | |
---|
185 | $rel_path = PWG_DERIVATIVE_DIR.$loc; |
---|
186 | |
---|
187 | global $conf; |
---|
188 | $url_style=$conf['derivative_url_style']; |
---|
189 | if (!$url_style) |
---|
190 | { |
---|
191 | $mtime = @filemtime(PHPWG_ROOT_PATH.$rel_path); |
---|
192 | if ($mtime===false or $mtime < $params->last_mod_time) |
---|
193 | { |
---|
194 | $url_style = 2; |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | $url_style = 1; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | if ($url_style == 2) |
---|
203 | { |
---|
204 | $rel_url = 'i'; |
---|
205 | if ($conf['php_extension_in_urls']) $rel_url .= '.php'; |
---|
206 | if ($conf['question_mark_in_urls']) $rel_url .= '?'; |
---|
207 | $rel_url .= '/'.$loc; |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | $rel_url = $rel_path; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | function get_path() |
---|
216 | { |
---|
217 | return PHPWG_ROOT_PATH.$this->rel_path; |
---|
218 | } |
---|
219 | |
---|
220 | function get_url() |
---|
221 | { |
---|
222 | if ($this->params == null) |
---|
223 | { |
---|
224 | return $this->src_image->get_url(); |
---|
225 | } |
---|
226 | return embellish_url( |
---|
227 | trigger_event('get_derivative_url', |
---|
228 | get_root_url().$this->rel_url, |
---|
229 | $this->params, $this->src_image, $this->rel_url |
---|
230 | ) ); |
---|
231 | } |
---|
232 | |
---|
233 | function same_as_source() |
---|
234 | { |
---|
235 | return $this->params == null; |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | function get_type() |
---|
240 | { |
---|
241 | if ($this->params == null) |
---|
242 | return 'Original'; |
---|
243 | return $this->params->type; |
---|
244 | } |
---|
245 | |
---|
246 | /* returns the size of the derivative image*/ |
---|
247 | function get_size() |
---|
248 | { |
---|
249 | if ($this->params == null) |
---|
250 | { |
---|
251 | return $this->src_image->get_size(); |
---|
252 | } |
---|
253 | return $this->params->compute_final_size($this->src_image->get_size()); |
---|
254 | } |
---|
255 | |
---|
256 | function get_size_htm() |
---|
257 | { |
---|
258 | $size = $this->get_size(); |
---|
259 | if ($size) |
---|
260 | { |
---|
261 | return 'width="'.$size[0].'" height="'.$size[1].'"'; |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | function get_size_hr() |
---|
266 | { |
---|
267 | $size = $this->get_size(); |
---|
268 | if ($size) |
---|
269 | { |
---|
270 | return $size[0].' x '.$size[1]; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | function get_scaled_size($maxw, $maxh) |
---|
275 | { |
---|
276 | $size = $this->get_size(); |
---|
277 | if ($size) |
---|
278 | { |
---|
279 | $ratio_w = $size[0] / $maxw; |
---|
280 | $ratio_h = $size[1] / $maxh; |
---|
281 | if ($ratio_w>1 || $ratio_h>1) |
---|
282 | { |
---|
283 | if ($ratio_w > $ratio_h) |
---|
284 | { |
---|
285 | $size[0] = $maxw; |
---|
286 | $size[1] = floor($size[1] / $ratio_w); |
---|
287 | } |
---|
288 | else |
---|
289 | { |
---|
290 | $size[0] = floor($size[0] / $ratio_h); |
---|
291 | $size[1] = $maxh; |
---|
292 | } |
---|
293 | } |
---|
294 | } |
---|
295 | return $size; |
---|
296 | } |
---|
297 | |
---|
298 | function get_scaled_size_htm($maxw=9999, $maxh=9999) |
---|
299 | { |
---|
300 | $size = $this->get_scaled_size($maxw, $maxh); |
---|
301 | if ($size) |
---|
302 | { |
---|
303 | return 'width="'.$size[0].'" height="'.$size[1].'"'; |
---|
304 | } |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | ?> |
---|