1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | functions_metadata.php | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | application : PhpWebGallery <http://phpwebgallery.net> | |
---|
6 | // | branch : BSF (Best So Far) | |
---|
7 | // +-----------------------------------------------------------------------+ |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2004-08-21 12:52:43 +0000 (Sat, 21 Aug 2004) $ |
---|
10 | // | last modifier : $Author: z0rglub $ |
---|
11 | // | revision : $Revision: 486 $ |
---|
12 | // +-----------------------------------------------------------------------+ |
---|
13 | // | This program is free software; you can redistribute it and/or modify | |
---|
14 | // | it under the terms of the GNU General Public License as published by | |
---|
15 | // | the Free Software Foundation | |
---|
16 | // | | |
---|
17 | // | This program is distributed in the hope that it will be useful, but | |
---|
18 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
19 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
20 | // | General Public License for more details. | |
---|
21 | // | | |
---|
22 | // | You should have received a copy of the GNU General Public License | |
---|
23 | // | along with this program; if not, write to the Free Software | |
---|
24 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
25 | // | USA. | |
---|
26 | // +-----------------------------------------------------------------------+ |
---|
27 | |
---|
28 | /** |
---|
29 | * returns informations from IPTC metadata, mapping is done at the beginning |
---|
30 | * of the function |
---|
31 | * |
---|
32 | * @param string $filename |
---|
33 | * @return array |
---|
34 | */ |
---|
35 | function get_iptc_data($filename) |
---|
36 | { |
---|
37 | global $getimagesize_time; |
---|
38 | |
---|
39 | $map = array( |
---|
40 | 'keywords' => '2#025', |
---|
41 | 'date_creation' => '2#055', |
---|
42 | 'author' => '2#122', |
---|
43 | 'name' => '2#085', |
---|
44 | 'comment' => '2#120' |
---|
45 | ); |
---|
46 | $datefields = array('date_creation', 'date_available'); |
---|
47 | |
---|
48 | $result = array(); |
---|
49 | |
---|
50 | // Read IPTC data |
---|
51 | $iptc = array(); |
---|
52 | |
---|
53 | $start = get_moment(); |
---|
54 | getimagesize($filename, &$imginfo); |
---|
55 | $getimagesize_time+= get_moment() - $start; |
---|
56 | |
---|
57 | if (is_array($imginfo) and isset($imginfo['APP13'])) |
---|
58 | { |
---|
59 | $iptc = iptcparse($imginfo['APP13']); |
---|
60 | if (is_array($iptc)) |
---|
61 | { |
---|
62 | $rmap = array_flip($map); |
---|
63 | foreach (array_keys($rmap) as $iptc_key) |
---|
64 | { |
---|
65 | if (isset($iptc[$iptc_key][0]) and $value = $iptc[$iptc_key][0]) |
---|
66 | { |
---|
67 | // strip leading zeros (weird Kodak Scanner software) |
---|
68 | while ($value[0] == chr(0)) |
---|
69 | { |
---|
70 | $value = substr($value, 1); |
---|
71 | } |
---|
72 | // remove binary nulls |
---|
73 | $value = str_replace(chr(0x00), ' ', $value); |
---|
74 | |
---|
75 | foreach (array_keys($map, $iptc_key) as $pwg_key) |
---|
76 | { |
---|
77 | if (in_array($pwg_key, $datefields)) |
---|
78 | { |
---|
79 | if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) |
---|
80 | { |
---|
81 | $value = $matches[1].'-'.$matches[2].'-'.$matches[3]; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | continue; |
---|
86 | } |
---|
87 | } |
---|
88 | $result[$pwg_key] = $value; |
---|
89 | } |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | return $result; |
---|
95 | } |
---|
96 | |
---|
97 | function update_metadata($files) |
---|
98 | { |
---|
99 | global $conf; |
---|
100 | |
---|
101 | // $conf['use_iptc'] = true; |
---|
102 | // $conf['use_exif'] = true; |
---|
103 | |
---|
104 | $inserts = array(); |
---|
105 | |
---|
106 | foreach ($files as $id => $file) |
---|
107 | { |
---|
108 | $insert = array(); |
---|
109 | $insert['id'] = $id; |
---|
110 | $insert['filesize'] = floor(filesize($file)/1024); |
---|
111 | |
---|
112 | if ($image_size = @getimagesize($file)) |
---|
113 | { |
---|
114 | $insert['width'] = $image_size[0]; |
---|
115 | $insert['height'] = $image_size[1]; |
---|
116 | } |
---|
117 | |
---|
118 | if ($conf['use_exif']) |
---|
119 | { |
---|
120 | if ($exif = @read_exif_data($file)) |
---|
121 | { |
---|
122 | if (isset($exif['DateTime'])) |
---|
123 | { |
---|
124 | preg_match('/^(\d{4}).(\d{2}).(\d{2})/' |
---|
125 | ,$exif['DateTime'] |
---|
126 | ,$matches); |
---|
127 | $insert['date_creation'] = |
---|
128 | "'".$matches[1].'-'.$matches[2].'-'.$matches[3]."'"; |
---|
129 | } |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | if ($conf['use_iptc']) |
---|
134 | { |
---|
135 | $iptc = get_iptc_data($file); |
---|
136 | foreach (array_keys($iptc) as $key) |
---|
137 | { |
---|
138 | $insert[$key] = "'".addslashes($iptc[$key])."'"; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | $insert['date_metadata_update'] = CURRENT_DATE; |
---|
143 | |
---|
144 | array_push($inserts, $insert); |
---|
145 | } |
---|
146 | |
---|
147 | if (count($inserts) > 0) |
---|
148 | { |
---|
149 | $dbfields = array( |
---|
150 | 'id','filesize','width','height','name','author','comment' |
---|
151 | ,'date_creation','keywords','date_metadata_update' |
---|
152 | ); |
---|
153 | |
---|
154 | // depending on the MySQL version, we use the multi table update or N |
---|
155 | // update queries |
---|
156 | $query = 'SELECT VERSION() AS version;'; |
---|
157 | $row = mysql_fetch_array(mysql_query($query)); |
---|
158 | if (version_compare($row['version'],'4.0.4') < 0) |
---|
159 | { |
---|
160 | // MySQL is prior to version 4.0.4, multi table update feature is not |
---|
161 | // available |
---|
162 | echo 'MySQL is prior to version 4.0.4, multi table update feature is not available<br />'; |
---|
163 | foreach ($inserts as $insert) |
---|
164 | { |
---|
165 | $query = ' |
---|
166 | UPDATE '.IMAGES_TABLE.' |
---|
167 | SET '; |
---|
168 | foreach (array_diff(array_keys($insert),array('id')) as $num => $key) |
---|
169 | { |
---|
170 | if ($num > 1) |
---|
171 | { |
---|
172 | $query.= ', '; |
---|
173 | } |
---|
174 | $query.= $key.' = '.$insert[$key]; |
---|
175 | } |
---|
176 | $query.= ' |
---|
177 | WHERE id = '.$insert['id'].' |
---|
178 | ;'; |
---|
179 | // echo '<pre>'.$query.'</pre>'; |
---|
180 | mysql_query($query); |
---|
181 | } |
---|
182 | } |
---|
183 | else |
---|
184 | { |
---|
185 | // creation of the temporary table |
---|
186 | $query = ' |
---|
187 | DESCRIBE '.IMAGES_TABLE.' |
---|
188 | ;'; |
---|
189 | $result = mysql_query($query); |
---|
190 | $columns = array(); |
---|
191 | while ($row = mysql_fetch_array($result)) |
---|
192 | { |
---|
193 | if (in_array($row['Field'], $dbfields)) |
---|
194 | { |
---|
195 | $column = $row['Field']; |
---|
196 | $column.= ' '.$row['Type']; |
---|
197 | if (!isset($row['Null']) or $row['Null'] == '') |
---|
198 | { |
---|
199 | $column.= ' NOT NULL'; |
---|
200 | } |
---|
201 | if (isset($row['Default'])) |
---|
202 | { |
---|
203 | $column.= " default '".$row['Default']."'"; |
---|
204 | } |
---|
205 | array_push($columns, $column); |
---|
206 | } |
---|
207 | } |
---|
208 | $query = ' |
---|
209 | CREATE TEMPORARY TABLE '.IMAGE_METADATA_TABLE.' |
---|
210 | ( |
---|
211 | '.implode(",\n", $columns).', |
---|
212 | PRIMARY KEY (id) |
---|
213 | ) |
---|
214 | ;'; |
---|
215 | // echo '<pre>'.$query.'</pre>'; |
---|
216 | mysql_query($query); |
---|
217 | // inserts all found pictures |
---|
218 | $query = ' |
---|
219 | INSERT INTO '.IMAGE_METADATA_TABLE.' |
---|
220 | ('.implode(',', $dbfields).') |
---|
221 | VALUES |
---|
222 | '; |
---|
223 | foreach ($inserts as $insert_id => $insert) |
---|
224 | { |
---|
225 | $query.= ' |
---|
226 | '; |
---|
227 | if ($insert_id > 0) |
---|
228 | { |
---|
229 | $query.= ','; |
---|
230 | } |
---|
231 | $query.= '('; |
---|
232 | foreach ($dbfields as $field_id => $dbfield) |
---|
233 | { |
---|
234 | if ($field_id > 0) |
---|
235 | { |
---|
236 | $query.= ','; |
---|
237 | } |
---|
238 | |
---|
239 | if (!isset($insert[$dbfield]) or $insert[$dbfield] == '') |
---|
240 | { |
---|
241 | $query.= 'NULL'; |
---|
242 | } |
---|
243 | else |
---|
244 | { |
---|
245 | $query.= $insert[$dbfield]; |
---|
246 | } |
---|
247 | } |
---|
248 | $query.=')'; |
---|
249 | } |
---|
250 | $query.= ' |
---|
251 | ;'; |
---|
252 | // echo '<pre>'.$query.'</pre>'; |
---|
253 | mysql_query($query); |
---|
254 | // update of images table by joining with temporary table |
---|
255 | $query = ' |
---|
256 | UPDATE '.IMAGES_TABLE.' AS images, '.IMAGE_METADATA_TABLE.' as metadata |
---|
257 | SET '.implode("\n , ", |
---|
258 | array_map( |
---|
259 | create_function('$s', 'return "images.$s = metadata.$s";') |
---|
260 | , array_diff($dbfields, array('id')))).' |
---|
261 | WHERE images.id = metadata.id |
---|
262 | ;'; |
---|
263 | echo '<pre>'.$query.'</pre>'; |
---|
264 | mysql_query($query); |
---|
265 | } |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | /** |
---|
270 | * returns an array associating element id (images.id) with its complete |
---|
271 | * path in the filesystem |
---|
272 | * |
---|
273 | * @param int id_uppercat |
---|
274 | * @param boolean recursive ? |
---|
275 | * @param boolean only newly added files ? |
---|
276 | * @return array |
---|
277 | */ |
---|
278 | function get_filelist($category_id = '', $recursive = false, $only_new = false) |
---|
279 | { |
---|
280 | $files = array(); |
---|
281 | |
---|
282 | $query = ' |
---|
283 | SELECT id, dir |
---|
284 | FROM '.CATEGORIES_TABLE.' |
---|
285 | ;'; |
---|
286 | $result = mysql_query($query); |
---|
287 | $cat_dirs = array(); |
---|
288 | while ($row = mysql_fetch_array($result)) |
---|
289 | { |
---|
290 | $cat_dirs[$row['id']] = $row['dir']; |
---|
291 | } |
---|
292 | |
---|
293 | // filling $uppercats_array : to each category id the uppercats list is |
---|
294 | // associated |
---|
295 | $uppercats_array = array(); |
---|
296 | |
---|
297 | $query = ' |
---|
298 | SELECT id, uppercats |
---|
299 | FROM '.CATEGORIES_TABLE; |
---|
300 | if (is_numeric($category_id)) |
---|
301 | { |
---|
302 | if ($recursive) |
---|
303 | { |
---|
304 | $query.= ' |
---|
305 | WHERE uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\' |
---|
306 | '; |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | $query.= ' |
---|
311 | WHERE id = '.$category_id.' |
---|
312 | '; |
---|
313 | } |
---|
314 | } |
---|
315 | $query.= ' |
---|
316 | ;'; |
---|
317 | $result = mysql_query($query); |
---|
318 | while ($row = mysql_fetch_array($result)) |
---|
319 | { |
---|
320 | $uppercats_array[$row['id']] = $row['uppercats']; |
---|
321 | } |
---|
322 | |
---|
323 | $query = ' |
---|
324 | SELECT galleries_url |
---|
325 | FROM '.SITES_TABLE.' |
---|
326 | WHERE id = 1 |
---|
327 | '; |
---|
328 | $row = mysql_fetch_array(mysql_query($query)); |
---|
329 | $basedir = $row['galleries_url']; |
---|
330 | |
---|
331 | // filling $cat_fulldirs |
---|
332 | $cat_fulldirs = array(); |
---|
333 | foreach ($uppercats_array as $cat_id => $uppercats) |
---|
334 | { |
---|
335 | $uppercats = str_replace(',', '/', $uppercats); |
---|
336 | $cat_fulldirs[$cat_id] = $basedir.preg_replace('/(\d+)/e', |
---|
337 | "\$cat_dirs['$1']", |
---|
338 | $uppercats); |
---|
339 | } |
---|
340 | |
---|
341 | $query = ' |
---|
342 | SELECT id, file, storage_category_id |
---|
343 | FROM '.IMAGES_TABLE.' |
---|
344 | WHERE storage_category_id IN ('.implode(',' |
---|
345 | ,array_keys($uppercats_array)).')'; |
---|
346 | if ($only_new) |
---|
347 | { |
---|
348 | $query.= ' |
---|
349 | AND date_metadata_update IS NULL |
---|
350 | '; |
---|
351 | } |
---|
352 | $query.= ' |
---|
353 | ;'; |
---|
354 | $result = mysql_query($query); |
---|
355 | while ($row = mysql_fetch_array($result)) |
---|
356 | { |
---|
357 | $files[$row['id']] |
---|
358 | = $cat_fulldirs[$row['storage_category_id']].'/'.$row['file']; |
---|
359 | } |
---|
360 | |
---|
361 | return $files; |
---|
362 | } |
---|
363 | ?> |
---|