source: trunk/admin/include/functions_metadata.php @ 11185

Last change on this file since 11185 was 9073, checked in by plg, 13 years ago

bug 270 fixed: (yes "270", submitted in 2006), time (hour:minute:second) added
in the date_creation field and in the synchronization from EXIF metadata.

  • Property svn:eol-style set to LF
File size: 7.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
25
26
27function get_sync_iptc_data($file)
28{
29  global $conf;
30
31  $map = $conf['use_iptc_mapping'];
32
33  $iptc = get_iptc_data($file, $map);
34
35  foreach ($iptc as $pwg_key => $value)
36  {
37    if (in_array($pwg_key, array('date_creation', 'date_available')))
38    {
39      if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
40      {
41        $year = $matches[1];
42        $month = $matches[2];
43        $day = $matches[3];
44
45        if (!checkdate($month, $day, $year))
46        {
47          // we suppose the year is correct
48          $month = 1;
49          $day = 1;
50        }
51       
52        $iptc[$pwg_key] = $year.'-'.$month.'-'.$day;
53      }
54    }
55  }
56
57  if (isset($iptc['keywords']))
58  {
59    // official keywords separator is the comma
60    $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
61    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
62
63    $iptc['keywords'] = implode(
64      ',',
65      array_unique(
66        explode(
67          ',',
68          $iptc['keywords']
69          )
70        )
71      );
72  }
73
74  foreach ($iptc as $pwg_key => $value)
75  {
76    $iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
77  }
78
79  return $iptc;
80}
81
82function get_sync_exif_data($file)
83{
84  global $conf;
85
86  $exif = get_exif_data($file, $conf['use_exif_mapping']);
87
88  foreach ($exif as $pwg_key => $value)
89  {
90    if (in_array($pwg_key, array('date_creation', 'date_available')))
91    {
92      if (preg_match('/^(\d{4}).(\d{2}).(\d{2}) (\d{2}).(\d{2}).(\d{2})/', $value, $matches))
93      {
94        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3].' '.$matches[4].':'.$matches[5].':'.$matches[6];
95      }
96      elseif (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches))
97      {
98        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
99      }
100    }
101    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
102  }
103
104  return $exif;
105}
106
107function update_metadata($files)
108{
109  global $conf;
110
111  if (!defined('CURRENT_DATE'))
112  {
113    define('CURRENT_DATE', date('Y-m-d'));
114  }
115
116  $datas = array();
117  $tags_of = array();
118  $has_high_images = array();
119
120  $image_ids = array();
121  foreach ($files as $id => $file)
122  {
123    array_push($image_ids, $id);
124  }
125
126  $query = '
127SELECT id
128  FROM '.IMAGES_TABLE.'
129  WHERE has_high = \'true\'
130    AND id IN (
131'.wordwrap(implode(', ', $image_ids), 80, "\n").'
132)
133;';
134
135  $has_high_images = array_from_query($query, 'id');
136
137  foreach ($files as $id => $file)
138  {
139    $data = array();
140    $data['id'] = $id;
141    $data['filesize'] = floor(filesize($file)/1024);
142
143    if ($image_size = @getimagesize($file))
144    {
145      $data['width'] = $image_size[0];
146      $data['height'] = $image_size[1];
147    }
148
149    if (in_array($id, $has_high_images))
150    {
151      $high_file = dirname($file).'/pwg_high/'.basename($file);
152
153      $data['high_filesize'] = floor(filesize($high_file)/1024);
154    }
155
156    if ($conf['use_exif'])
157    {
158      $exif = get_sync_exif_data($file);
159      if (count($exif) == 0 and isset($data['high_filesize']))
160      {
161        $exif = get_sync_exif_data($high_file);
162      }
163      $data = array_merge($data, $exif);
164    }
165
166    if ($conf['use_iptc'])
167    {
168      $iptc = get_sync_iptc_data($file);
169      if (count($iptc) == 0 and isset($data['high_filesize']))
170      {
171        $iptc = get_sync_iptc_data($high_file);
172      }
173      $data = array_merge($data, $iptc);
174     
175      if (count($iptc) > 0)
176      {
177        foreach (array_keys($iptc) as $key)
178        {
179          if ($key == 'keywords' or $key == 'tags')
180          {
181            if (!isset($tags_of[$id]))
182            {
183              $tags_of[$id] = array();
184            }
185
186            foreach (explode(',', $iptc[$key]) as $tag_name)
187            {
188              array_push(
189                $tags_of[$id],
190                tag_id_from_tag_name($tag_name)
191                );
192            }
193          }
194        }
195      }
196    }
197
198    $data['date_metadata_update'] = CURRENT_DATE;
199
200    array_push($datas, $data);
201  }
202
203  if (count($datas) > 0)
204  {
205    $update_fields =
206      array(
207        'filesize',
208        'width',
209        'height',
210        'high_filesize',
211        'date_metadata_update'
212        );
213
214    if ($conf['use_exif'])
215    {
216      $update_fields =
217        array_merge(
218          $update_fields,
219          array_keys($conf['use_exif_mapping'])
220          );
221    }
222
223    if ($conf['use_iptc'])
224    {
225      $update_fields =
226        array_merge(
227          $update_fields,
228          array_diff(
229            array_keys($conf['use_iptc_mapping']),
230            array('tags', 'keywords')
231            )
232          );
233    }
234
235    mass_updates(
236      IMAGES_TABLE,
237      array(
238        'primary' => array('id'),
239        'update'  => array_unique($update_fields)
240        ),
241      $datas,
242      MASS_UPDATES_SKIP_EMPTY
243      );
244  }
245
246  set_tags_of($tags_of);
247}
248
249/**
250 * returns an array associating element id (images.id) with its complete
251 * path in the filesystem
252 *
253 * @param int id_uppercat
254 * @param int site_id
255 * @param boolean recursive ?
256 * @param boolean only newly added files ?
257 * @return array
258 */
259function get_filelist($category_id = '', $site_id=1, $recursive = false,
260                      $only_new = false)
261{
262  // filling $cat_ids : all categories required
263  $cat_ids = array();
264
265  $query = '
266SELECT id
267  FROM '.CATEGORIES_TABLE.'
268  WHERE site_id = '.$site_id.'
269    AND dir IS NOT NULL';
270  if (is_numeric($category_id))
271  {
272    if ($recursive)
273    {
274      $query.= '
275    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
276';
277    }
278    else
279    {
280      $query.= '
281    AND id = '.$category_id.'
282';
283    }
284  }
285  $query.= '
286;';
287  $result = pwg_query($query);
288  while ($row = pwg_db_fetch_assoc($result))
289  {
290    array_push($cat_ids, $row['id']);
291  }
292
293  if (count($cat_ids) == 0)
294  {
295    return array();
296  }
297
298  $files = array();
299
300  $query = '
301SELECT id, path
302  FROM '.IMAGES_TABLE.'
303  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
304  if ($only_new)
305  {
306    $query.= '
307    AND date_metadata_update IS NULL
308';
309  }
310  $query.= '
311;';
312  $result = pwg_query($query);
313  while ($row = pwg_db_fetch_assoc($result))
314  {
315    $files[$row['id']] = $row['path'];
316  }
317
318  return $files;
319}
320?>
Note: See TracBrowser for help on using the repository browser.