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

Last change on this file since 862 was 862, checked in by plg, 19 years ago
  • improvement: long localized messages are in HTML files instead of $lang array. This is the case of admin/help and about pages.
  • deletion: of unused functions (ts_to_mysqldt, is_image, TN_exists, check_date_format, date_convert, get_category_directories, get_used_metadata_list, array_remove, pwg_write_debug, get_group_restrictions, get_all_group_restrictions, is_group_allowed, style_select, deprecated_getAttribute).
  • new: many new contextual help pages to replace descriptions previously included in pages.
  • modification: reorganisation of language files. Deletion of unused language keys, alphabetical sort. No faq.lang.php anymore (replaced by help.html). Only done for en_UK.iso-8859-1.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 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-14 21:57:05 +0000 (Wed, 14 Sep 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 862 $
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?>
Note: See TracBrowser for help on using the repository browser.