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
RevLine 
[486]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[486]23
[493]24include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
25
[858]26
[493]27function get_sync_iptc_data($file)
[486]28{
[2560]29  global $conf;
[2521]30
[600]31  $map = $conf['use_iptc_mapping'];
[2521]32
[493]33  $iptc = get_iptc_data($file, $map);
34
35  foreach ($iptc as $pwg_key => $value)
[486]36  {
[2560]37    if (in_array($pwg_key, array('date_creation', 'date_available')))
[486]38    {
[614]39      if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
[486]40      {
[7507]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;
[486]53      }
54    }
55  }
[493]56
[562]57  if (isset($iptc['keywords']))
58  {
[1127]59    // official keywords separator is the comma
60    $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
[562]61    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
[1223]62
63    $iptc['keywords'] = implode(
64      ',',
65      array_unique(
66        explode(
67          ',',
68          $iptc['keywords']
69          )
70        )
71      );
[562]72  }
73
[1717]74  foreach ($iptc as $pwg_key => $value)
75  {
76    $iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
77  }
78
[493]79  return $iptc;
[486]80}
81
[858]82function get_sync_exif_data($file)
83{
[2560]84  global $conf;
[858]85
86  $exif = get_exif_data($file, $conf['use_exif_mapping']);
87
88  foreach ($exif as $pwg_key => $value)
89  {
[2560]90    if (in_array($pwg_key, array('date_creation', 'date_available')))
[858]91    {
[9073]92      if (preg_match('/^(\d{4}).(\d{2}).(\d{2}) (\d{2}).(\d{2}).(\d{2})/', $value, $matches))
[858]93      {
[9073]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      {
[858]98        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
99      }
[11747]100      else
101      {
102        unset($exif[$pwg_key]);
103        continue;
104      }
[858]105    }
[1717]106    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
[858]107  }
108
109  return $exif;
110}
111
[486]112function update_metadata($files)
113{
114  global $conf;
[582]115
116  if (!defined('CURRENT_DATE'))
117  {
[624]118    define('CURRENT_DATE', date('Y-m-d'));
[582]119  }
120
[625]121  $datas = array();
[1119]122  $tags_of = array();
[1883]123  $has_high_images = array();
[486]124
[1883]125  $image_ids = array();
[486]126  foreach ($files as $id => $file)
127  {
[1883]128    array_push($image_ids, $id);
129  }
[2521]130
[1883]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
[2521]140  $has_high_images = array_from_query($query, 'id');
[1883]141
142  foreach ($files as $id => $file)
143  {
[625]144    $data = array();
145    $data['id'] = $id;
146    $data['filesize'] = floor(filesize($file)/1024);
[2521]147
[486]148    if ($image_size = @getimagesize($file))
149    {
[625]150      $data['width'] = $image_size[0];
151      $data['height'] = $image_size[1];
[486]152    }
[1883]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    }
[2521]160
[486]161    if ($conf['use_exif'])
162    {
[858]163      $exif = get_sync_exif_data($file);
[4683]164      if (count($exif) == 0 and isset($data['high_filesize']))
165      {
166        $exif = get_sync_exif_data($high_file);
167      }
[4681]168      $data = array_merge($data, $exif);
[486]169    }
170
171    if ($conf['use_iptc'])
172    {
[493]173      $iptc = get_sync_iptc_data($file);
[4683]174      if (count($iptc) == 0 and isset($data['high_filesize']))
175      {
176        $iptc = get_sync_iptc_data($high_file);
177      }
[4681]178      $data = array_merge($data, $iptc);
179     
[493]180      if (count($iptc) > 0)
[486]181      {
[493]182        foreach (array_keys($iptc) as $key)
183        {
[1119]184          if ($key == 'keywords' or $key == 'tags')
185          {
186            if (!isset($tags_of[$id]))
187            {
188              $tags_of[$id] = array();
189            }
[2521]190
[1119]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          }
[493]199        }
[486]200      }
201    }
202
[625]203    $data['date_metadata_update'] = CURRENT_DATE;
[486]204
[625]205    array_push($datas, $data);
[486]206  }
[2521]207
[625]208  if (count($datas) > 0)
[486]209  {
[903]210    $update_fields =
211      array(
212        'filesize',
213        'width',
214        'height',
[1883]215        'high_filesize',
[903]216        'date_metadata_update'
217        );
[2521]218
[702]219    if ($conf['use_exif'])
220    {
[903]221      $update_fields =
222        array_merge(
223          $update_fields,
224          array_keys($conf['use_exif_mapping'])
225          );
[702]226    }
[2521]227
[702]228    if ($conf['use_iptc'])
229    {
[903]230      $update_fields =
231        array_merge(
232          $update_fields,
[1119]233          array_diff(
234            array_keys($conf['use_iptc_mapping']),
235            array('tags', 'keywords')
236            )
[903]237          );
[702]238    }
[858]239
[1127]240    mass_updates(
241      IMAGES_TABLE,
[903]242      array(
243        'primary' => array('id'),
244        'update'  => array_unique($update_fields)
[1127]245        ),
[2521]246      $datas,
247      MASS_UPDATES_SKIP_EMPTY
[1127]248      );
[486]249  }
[1119]250
[1127]251  set_tags_of($tags_of);
[486]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
[1029]259 * @param int site_id
[486]260 * @param boolean recursive ?
261 * @param boolean only newly added files ?
262 * @return array
263 */
[2521]264function get_filelist($category_id = '', $site_id=1, $recursive = false,
[1029]265                      $only_new = false)
[486]266{
[657]267  // filling $cat_ids : all categories required
268  $cat_ids = array();
[2521]269
[486]270  $query = '
[657]271SELECT id
[582]272  FROM '.CATEGORIES_TABLE.'
[1029]273  WHERE site_id = '.$site_id.'
[582]274    AND dir IS NOT NULL';
[486]275  if (is_numeric($category_id))
276  {
277    if ($recursive)
278    {
279      $query.= '
[4367]280    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
[486]281';
282    }
283    else
284    {
285      $query.= '
[582]286    AND id = '.$category_id.'
[486]287';
288    }
289  }
290  $query.= '
291;';
[587]292  $result = pwg_query($query);
[4325]293  while ($row = pwg_db_fetch_assoc($result))
[486]294  {
[657]295    array_push($cat_ids, $row['id']);
[486]296  }
297
[657]298  if (count($cat_ids) == 0)
[590]299  {
300    return array();
301  }
302
[657]303  $files = array();
[486]304
305  $query = '
[657]306SELECT id, path
[486]307  FROM '.IMAGES_TABLE.'
[1121]308  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
[486]309  if ($only_new)
310  {
311    $query.= '
312    AND date_metadata_update IS NULL
313';
314  }
315  $query.= '
316;';
[587]317  $result = pwg_query($query);
[4325]318  while ($row = pwg_db_fetch_assoc($result))
[486]319  {
[657]320    $files[$row['id']] = $row['path'];
[486]321  }
[2521]322
[486]323  return $files;
324}
325?>
Note: See TracBrowser for help on using the repository browser.