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

Last change on this file since 1590 was 1223, checked in by plg, 18 years ago

merge -r1221:1222 from branch 1.6 to trunk (bug 339)

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