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

Last change on this file since 9073 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
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      }
100    }
[1717]101    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
[858]102  }
103
104  return $exif;
105}
106
[486]107function update_metadata($files)
108{
109  global $conf;
[582]110
111  if (!defined('CURRENT_DATE'))
112  {
[624]113    define('CURRENT_DATE', date('Y-m-d'));
[582]114  }
115
[625]116  $datas = array();
[1119]117  $tags_of = array();
[1883]118  $has_high_images = array();
[486]119
[1883]120  $image_ids = array();
[486]121  foreach ($files as $id => $file)
122  {
[1883]123    array_push($image_ids, $id);
124  }
[2521]125
[1883]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
[2521]135  $has_high_images = array_from_query($query, 'id');
[1883]136
137  foreach ($files as $id => $file)
138  {
[625]139    $data = array();
140    $data['id'] = $id;
141    $data['filesize'] = floor(filesize($file)/1024);
[2521]142
[486]143    if ($image_size = @getimagesize($file))
144    {
[625]145      $data['width'] = $image_size[0];
146      $data['height'] = $image_size[1];
[486]147    }
[1883]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    }
[2521]155
[486]156    if ($conf['use_exif'])
157    {
[858]158      $exif = get_sync_exif_data($file);
[4683]159      if (count($exif) == 0 and isset($data['high_filesize']))
160      {
161        $exif = get_sync_exif_data($high_file);
162      }
[4681]163      $data = array_merge($data, $exif);
[486]164    }
165
166    if ($conf['use_iptc'])
167    {
[493]168      $iptc = get_sync_iptc_data($file);
[4683]169      if (count($iptc) == 0 and isset($data['high_filesize']))
170      {
171        $iptc = get_sync_iptc_data($high_file);
172      }
[4681]173      $data = array_merge($data, $iptc);
174     
[493]175      if (count($iptc) > 0)
[486]176      {
[493]177        foreach (array_keys($iptc) as $key)
178        {
[1119]179          if ($key == 'keywords' or $key == 'tags')
180          {
181            if (!isset($tags_of[$id]))
182            {
183              $tags_of[$id] = array();
184            }
[2521]185
[1119]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          }
[493]194        }
[486]195      }
196    }
197
[625]198    $data['date_metadata_update'] = CURRENT_DATE;
[486]199
[625]200    array_push($datas, $data);
[486]201  }
[2521]202
[625]203  if (count($datas) > 0)
[486]204  {
[903]205    $update_fields =
206      array(
207        'filesize',
208        'width',
209        'height',
[1883]210        'high_filesize',
[903]211        'date_metadata_update'
212        );
[2521]213
[702]214    if ($conf['use_exif'])
215    {
[903]216      $update_fields =
217        array_merge(
218          $update_fields,
219          array_keys($conf['use_exif_mapping'])
220          );
[702]221    }
[2521]222
[702]223    if ($conf['use_iptc'])
224    {
[903]225      $update_fields =
226        array_merge(
227          $update_fields,
[1119]228          array_diff(
229            array_keys($conf['use_iptc_mapping']),
230            array('tags', 'keywords')
231            )
[903]232          );
[702]233    }
[858]234
[1127]235    mass_updates(
236      IMAGES_TABLE,
[903]237      array(
238        'primary' => array('id'),
239        'update'  => array_unique($update_fields)
[1127]240        ),
[2521]241      $datas,
242      MASS_UPDATES_SKIP_EMPTY
[1127]243      );
[486]244  }
[1119]245
[1127]246  set_tags_of($tags_of);
[486]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
[1029]254 * @param int site_id
[486]255 * @param boolean recursive ?
256 * @param boolean only newly added files ?
257 * @return array
258 */
[2521]259function get_filelist($category_id = '', $site_id=1, $recursive = false,
[1029]260                      $only_new = false)
[486]261{
[657]262  // filling $cat_ids : all categories required
263  $cat_ids = array();
[2521]264
[486]265  $query = '
[657]266SELECT id
[582]267  FROM '.CATEGORIES_TABLE.'
[1029]268  WHERE site_id = '.$site_id.'
[582]269    AND dir IS NOT NULL';
[486]270  if (is_numeric($category_id))
271  {
272    if ($recursive)
273    {
274      $query.= '
[4367]275    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
[486]276';
277    }
278    else
279    {
280      $query.= '
[582]281    AND id = '.$category_id.'
[486]282';
283    }
284  }
285  $query.= '
286;';
[587]287  $result = pwg_query($query);
[4325]288  while ($row = pwg_db_fetch_assoc($result))
[486]289  {
[657]290    array_push($cat_ids, $row['id']);
[486]291  }
292
[657]293  if (count($cat_ids) == 0)
[590]294  {
295    return array();
296  }
297
[657]298  $files = array();
[486]299
300  $query = '
[657]301SELECT id, path
[486]302  FROM '.IMAGES_TABLE.'
[1121]303  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
[486]304  if ($only_new)
305  {
306    $query.= '
307    AND date_metadata_update IS NULL
308';
309  }
310  $query.= '
311;';
[587]312  $result = pwg_query($query);
[4325]313  while ($row = pwg_db_fetch_assoc($result))
[486]314  {
[657]315    $files[$row['id']] = $row['path'];
[486]316  }
[2521]317
[486]318  return $files;
319}
320?>
Note: See TracBrowser for help on using the repository browser.