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

Last change on this file since 858 was 858, checked in by plg, 19 years ago
  • modification : less configuration parameters in administration screen. These parameters are move to include/config_default.inc.php.
  • new : ability to add a single picture to caddie from picture.php
  • new : contextual help, only a few pages are available.
  • new : ability to delete users from admin/user_list
  • modification : reorganization of configuration file
  • new : configuration parameter use_exif_mapping
  • improvement : MOD hidemail added to standard
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-09-03 16:36:05 +0000 (Sat, 03 Sep 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 858 $
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
28include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
29
30$page['datefields'] = array('date_creation', 'date_available');
31
32function get_sync_iptc_data($file)
33{
34  global $conf, $page;
35 
36  $map = $conf['use_iptc_mapping'];
37 
38  $iptc = get_iptc_data($file, $map);
39
40  foreach ($iptc as $pwg_key => $value)
41  {
42    if (in_array($pwg_key, $page['datefields']))
43    {
44      if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
45      {
46        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
47      }
48    }
49  }
50
51  if (isset($iptc['keywords']))
52  {
53    // keywords separator is the comma, nothing else. Allowed characters in
54    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
55    // considered as separators
56    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
57    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
58  }
59
60  return $iptc;
61}
62
63function get_sync_exif_data($file)
64{
65  global $conf, $page;
66
67  $exif = get_exif_data($file, $conf['use_exif_mapping']);
68
69  foreach ($exif as $pwg_key => $value)
70  {
71    if (in_array($pwg_key, $page['datefields']))
72    {
73      if (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches))
74      {
75        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
76      }
77    }
78  }
79
80  return $exif;
81}
82
83function update_metadata($files)
84{
85  global $conf;
86
87  if (!defined('CURRENT_DATE'))
88  {
89    define('CURRENT_DATE', date('Y-m-d'));
90  }
91
92  $datas = array();
93
94  foreach ($files as $id => $file)
95  {
96    $data = array();
97    $data['id'] = $id;
98    $data['filesize'] = floor(filesize($file)/1024);
99 
100    if ($image_size = @getimagesize($file))
101    {
102      $data['width'] = $image_size[0];
103      $data['height'] = $image_size[1];
104    }
105 
106    if ($conf['use_exif'])
107    {
108      $exif = get_sync_exif_data($file);
109
110      if (count($exif) > 0)
111      {
112        foreach (array_keys($exif) as $key)
113        {
114          $data[$key] = addslashes($exif[$key]);
115        }
116      }
117    }
118
119    if ($conf['use_iptc'])
120    {
121      $iptc = get_sync_iptc_data($file);
122      if (count($iptc) > 0)
123      {
124        foreach (array_keys($iptc) as $key)
125        {
126          $data[$key] = addslashes($iptc[$key]);
127        }
128      }
129    }
130
131    $data['date_metadata_update'] = CURRENT_DATE;
132
133    array_push($datas, $data);
134  }
135 
136  if (count($datas) > 0)
137  {
138    $update_fields = array('filesize','width','height','date_metadata_update');
139    if ($conf['use_exif'])
140    {
141      array_push($update_fields, 'date_creation');
142    }
143    if ($conf['use_iptc'])
144    {
145      $update_fields = array_merge($update_fields,
146                                   array_keys($conf['use_iptc_mapping']));
147    }
148
149    $fields = array('primary' => array('id'),
150                    'update'  => array_unique($update_fields));
151    mass_updates(IMAGES_TABLE, $fields, $datas);
152  }
153}
154
155/**
156 * returns an array associating element id (images.id) with its complete
157 * path in the filesystem
158 *
159 * @param int id_uppercat
160 * @param boolean recursive ?
161 * @param boolean only newly added files ?
162 * @return array
163 */
164function get_filelist($category_id = '', $recursive = false, $only_new = false)
165{
166  // filling $cat_ids : all categories required
167  $cat_ids = array();
168 
169  $query = '
170SELECT id
171  FROM '.CATEGORIES_TABLE.'
172  WHERE site_id = 1
173    AND dir IS NOT NULL';
174  if (is_numeric($category_id))
175  {
176    if ($recursive)
177    {
178      $query.= '
179    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
180';
181    }
182    else
183    {
184      $query.= '
185    AND id = '.$category_id.'
186';
187    }
188  }
189  $query.= '
190;';
191  $result = pwg_query($query);
192  while ($row = mysql_fetch_array($result))
193  {
194    array_push($cat_ids, $row['id']);
195  }
196
197  if (count($cat_ids) == 0)
198  {
199    return array();
200  }
201
202  $files = array();
203
204  $query = '
205SELECT id, path
206  FROM '.IMAGES_TABLE.'
207  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
208  if ($only_new)
209  {
210    $query.= '
211    AND date_metadata_update IS NULL
212';
213  }
214  $query.= '
215;';
216  $result = pwg_query($query);
217  while ($row = mysql_fetch_array($result))
218  {
219    $files[$row['id']] = $row['path'];
220  }
221 
222  return $files;
223}
224
225// used_metadata string is displayed to inform admin which metadata will be
226// used from files for synchronization
227function get_used_metadata_list()
228{
229  global $conf;
230 
231  $used_metadata = array('filesize', 'width', 'height');
232
233  if ($conf['use_exif'])
234  {
235    array_push($used_metadata, 'date_creation');
236  }
237
238  if ($conf['use_iptc'])
239  {
240    foreach (array_keys($conf['use_iptc_mapping']) as $key)
241    {
242      array_push($used_metadata, $key);
243    }
244  }
245
246  return array_unique($used_metadata);
247}
248?>
Note: See TracBrowser for help on using the repository browser.