source: branches/2.2/admin/include/functions_metadata.php @ 11747

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

bug 2356 fixed: if the EXIF date can't be parsed, we don't use it to fill the photo date

  • Property svn:eol-style set to LF
File size: 7.7 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      else
101      {
102        unset($exif[$pwg_key]);
103        continue;
104      }
105    }
106    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
107  }
108
109  return $exif;
110}
111
112function update_metadata($files)
113{
114  global $conf;
115
116  if (!defined('CURRENT_DATE'))
117  {
118    define('CURRENT_DATE', date('Y-m-d'));
119  }
120
121  $datas = array();
122  $tags_of = array();
123  $has_high_images = array();
124
125  $image_ids = array();
126  foreach ($files as $id => $file)
127  {
128    array_push($image_ids, $id);
129  }
130
131  $query = '
132SELECT id
133  FROM '.IMAGES_TABLE.'
134  WHERE has_high = \'true\'
135    AND id IN (
136'.wordwrap(implode(', ', $image_ids), 80, "\n").'
137)
138;';
139
140  $has_high_images = array_from_query($query, 'id');
141
142  foreach ($files as $id => $file)
143  {
144    $data = array();
145    $data['id'] = $id;
146    $data['filesize'] = floor(filesize($file)/1024);
147
148    if ($image_size = @getimagesize($file))
149    {
150      $data['width'] = $image_size[0];
151      $data['height'] = $image_size[1];
152    }
153
154    if (in_array($id, $has_high_images))
155    {
156      $high_file = dirname($file).'/pwg_high/'.basename($file);
157
158      $data['high_filesize'] = floor(filesize($high_file)/1024);
159    }
160
161    if ($conf['use_exif'])
162    {
163      $exif = get_sync_exif_data($file);
164      if (count($exif) == 0 and isset($data['high_filesize']))
165      {
166        $exif = get_sync_exif_data($high_file);
167      }
168      $data = array_merge($data, $exif);
169    }
170
171    if ($conf['use_iptc'])
172    {
173      $iptc = get_sync_iptc_data($file);
174      if (count($iptc) == 0 and isset($data['high_filesize']))
175      {
176        $iptc = get_sync_iptc_data($high_file);
177      }
178      $data = array_merge($data, $iptc);
179     
180      if (count($iptc) > 0)
181      {
182        foreach (array_keys($iptc) as $key)
183        {
184          if ($key == 'keywords' or $key == 'tags')
185          {
186            if (!isset($tags_of[$id]))
187            {
188              $tags_of[$id] = array();
189            }
190
191            foreach (explode(',', $iptc[$key]) as $tag_name)
192            {
193              array_push(
194                $tags_of[$id],
195                tag_id_from_tag_name($tag_name)
196                );
197            }
198          }
199        }
200      }
201    }
202
203    $data['date_metadata_update'] = CURRENT_DATE;
204
205    array_push($datas, $data);
206  }
207
208  if (count($datas) > 0)
209  {
210    $update_fields =
211      array(
212        'filesize',
213        'width',
214        'height',
215        'high_filesize',
216        'date_metadata_update'
217        );
218
219    if ($conf['use_exif'])
220    {
221      $update_fields =
222        array_merge(
223          $update_fields,
224          array_keys($conf['use_exif_mapping'])
225          );
226    }
227
228    if ($conf['use_iptc'])
229    {
230      $update_fields =
231        array_merge(
232          $update_fields,
233          array_diff(
234            array_keys($conf['use_iptc_mapping']),
235            array('tags', 'keywords')
236            )
237          );
238    }
239
240    mass_updates(
241      IMAGES_TABLE,
242      array(
243        'primary' => array('id'),
244        'update'  => array_unique($update_fields)
245        ),
246      $datas,
247      MASS_UPDATES_SKIP_EMPTY
248      );
249  }
250
251  set_tags_of($tags_of);
252}
253
254/**
255 * returns an array associating element id (images.id) with its complete
256 * path in the filesystem
257 *
258 * @param int id_uppercat
259 * @param int site_id
260 * @param boolean recursive ?
261 * @param boolean only newly added files ?
262 * @return array
263 */
264function get_filelist($category_id = '', $site_id=1, $recursive = false,
265                      $only_new = false)
266{
267  // filling $cat_ids : all categories required
268  $cat_ids = array();
269
270  $query = '
271SELECT id
272  FROM '.CATEGORIES_TABLE.'
273  WHERE site_id = '.$site_id.'
274    AND dir IS NOT NULL';
275  if (is_numeric($category_id))
276  {
277    if ($recursive)
278    {
279      $query.= '
280    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
281';
282    }
283    else
284    {
285      $query.= '
286    AND id = '.$category_id.'
287';
288    }
289  }
290  $query.= '
291;';
292  $result = pwg_query($query);
293  while ($row = pwg_db_fetch_assoc($result))
294  {
295    array_push($cat_ids, $row['id']);
296  }
297
298  if (count($cat_ids) == 0)
299  {
300    return array();
301  }
302
303  $files = array();
304
305  $query = '
306SELECT id, path
307  FROM '.IMAGES_TABLE.'
308  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
309  if ($only_new)
310  {
311    $query.= '
312    AND date_metadata_update IS NULL
313';
314  }
315  $query.= '
316;';
317  $result = pwg_query($query);
318  while ($row = pwg_db_fetch_assoc($result))
319  {
320    $files[$row['id']] = $row['path'];
321  }
322
323  return $files;
324}
325?>
Note: See TracBrowser for help on using the repository browser.