source: branches/branch-1_7/admin/include/functions_metadata.php @ 2694

Last change on this file since 2694 was 1900, checked in by rub, 17 years ago

Apply property svn:eol-style Value: LF

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 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-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: functions_metadata.php 1900 2007-03-12 22:33:53Z rub $
9// | last update   : $Date: 2007-03-12 22:33:53 +0000 (Mon, 12 Mar 2007) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1900 $
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    // official keywords separator is the comma
54    $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
55    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
56
57    $iptc['keywords'] = implode(
58      ',',
59      array_unique(
60        explode(
61          ',',
62          $iptc['keywords']
63          )
64        )
65      );
66  }
67
68  foreach ($iptc as $pwg_key => $value)
69  {
70    $iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
71  }
72
73  return $iptc;
74}
75
76function get_sync_exif_data($file)
77{
78  global $conf, $page;
79
80  $exif = get_exif_data($file, $conf['use_exif_mapping']);
81
82  foreach ($exif as $pwg_key => $value)
83  {
84    if (in_array($pwg_key, $page['datefields']))
85    {
86      if (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches))
87      {
88        $exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
89      }
90    }
91    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
92  }
93
94  return $exif;
95}
96
97function update_metadata($files)
98{
99  global $conf;
100
101  if (!defined('CURRENT_DATE'))
102  {
103    define('CURRENT_DATE', date('Y-m-d'));
104  }
105
106  $datas = array();
107  $tags_of = array();
108  $has_high_images = array();
109
110  $image_ids = array();
111  foreach ($files as $id => $file)
112  {
113    array_push($image_ids, $id);
114  }
115 
116  $query = '
117SELECT id
118  FROM '.IMAGES_TABLE.'
119  WHERE has_high = \'true\'
120    AND id IN (
121'.wordwrap(implode(', ', $image_ids), 80, "\n").'
122)
123;';
124
125  $result = pwg_query($query);
126  while ($row = mysql_fetch_array($result))
127  {
128    array_push($has_high_images, $row['id']);
129  }
130
131  foreach ($files as $id => $file)
132  {
133    $data = array();
134    $data['id'] = $id;
135    $data['filesize'] = floor(filesize($file)/1024);
136 
137    if ($image_size = @getimagesize($file))
138    {
139      $data['width'] = $image_size[0];
140      $data['height'] = $image_size[1];
141    }
142
143    if (in_array($id, $has_high_images))
144    {
145      $high_file = dirname($file).'/pwg_high/'.basename($file);
146
147      $data['high_filesize'] = floor(filesize($high_file)/1024);
148    }
149 
150    if ($conf['use_exif'])
151    {
152      $exif = get_sync_exif_data($file);
153    }
154
155    if ($conf['use_iptc'])
156    {
157      $iptc = get_sync_iptc_data($file);
158      if (count($iptc) > 0)
159      {
160        foreach (array_keys($iptc) as $key)
161        {
162          if ($key == 'keywords' or $key == 'tags')
163          {
164            if (!isset($tags_of[$id]))
165            {
166              $tags_of[$id] = array();
167            }
168           
169            foreach (explode(',', $iptc[$key]) as $tag_name)
170            {
171              array_push(
172                $tags_of[$id],
173                tag_id_from_tag_name($tag_name)
174                );
175            }
176          }
177        }
178      }
179    }
180
181    $data['date_metadata_update'] = CURRENT_DATE;
182
183    array_push($datas, $data);
184  }
185 
186  if (count($datas) > 0)
187  {
188    $update_fields =
189      array(
190        'filesize',
191        'width',
192        'height',
193        'high_filesize',
194        'date_metadata_update'
195        );
196   
197    if ($conf['use_exif'])
198    {
199      $update_fields =
200        array_merge(
201          $update_fields,
202          array_keys($conf['use_exif_mapping'])
203          );
204    }
205   
206    if ($conf['use_iptc'])
207    {
208      $update_fields =
209        array_merge(
210          $update_fields,
211          array_diff(
212            array_keys($conf['use_iptc_mapping']),
213            array('tags', 'keywords')
214            )
215          );
216    }
217
218    mass_updates(
219      IMAGES_TABLE,
220      array(
221        'primary' => array('id'),
222        'update'  => array_unique($update_fields)
223        ),
224      $datas
225      );
226  }
227
228  set_tags_of($tags_of);
229}
230
231/**
232 * returns an array associating element id (images.id) with its complete
233 * path in the filesystem
234 *
235 * @param int id_uppercat
236 * @param int site_id
237 * @param boolean recursive ?
238 * @param boolean only newly added files ?
239 * @return array
240 */
241function get_filelist($category_id = '', $site_id=1, $recursive = false, 
242                      $only_new = false)
243{
244  // filling $cat_ids : all categories required
245  $cat_ids = array();
246 
247  $query = '
248SELECT id
249  FROM '.CATEGORIES_TABLE.'
250  WHERE site_id = '.$site_id.'
251    AND dir IS NOT NULL';
252  if (is_numeric($category_id))
253  {
254    if ($recursive)
255    {
256      $query.= '
257    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
258';
259    }
260    else
261    {
262      $query.= '
263    AND id = '.$category_id.'
264';
265    }
266  }
267  $query.= '
268;';
269  $result = pwg_query($query);
270  while ($row = mysql_fetch_array($result))
271  {
272    array_push($cat_ids, $row['id']);
273  }
274
275  if (count($cat_ids) == 0)
276  {
277    return array();
278  }
279
280  $files = array();
281
282  $query = '
283SELECT id, path
284  FROM '.IMAGES_TABLE.'
285  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
286  if ($only_new)
287  {
288    $query.= '
289    AND date_metadata_update IS NULL
290';
291  }
292  $query.= '
293;';
294  $result = pwg_query($query);
295  while ($row = mysql_fetch_array($result))
296  {
297    $files[$row['id']] = $row['path'];
298  }
299 
300  return $files;
301}
302?>
Note: See TracBrowser for help on using the repository browser.