source: branches/1.4/admin/include/functions_metadata.php @ 16878

Last change on this file since 16878 was 738, checked in by plg, 19 years ago
  • bug fixed : instead of showing a blank page when trying to use Exif tags read function, display a message telling the problem
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.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: 2005-02-13 12:49:52 +0000 (Sun, 13 Feb 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 738 $
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
30function get_sync_iptc_data($file)
31{
32  global $conf;
33 
34  $map = $conf['use_iptc_mapping'];
35  $datefields = array('date_creation', 'date_available');
36 
37  $iptc = get_iptc_data($file, $map);
38
39  foreach ($iptc as $pwg_key => $value)
40  {
41    if (in_array($pwg_key, $datefields))
42    {
43      if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
44      {
45        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
46      }
47    }
48  }
49
50  if (isset($iptc['keywords']))
51  {
52    // keywords separator is the comma, nothing else. Allowed characters in
53    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
54    // considered as separators
55    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
56    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
57  }
58
59  return $iptc;
60}
61
62function update_metadata($files)
63{
64  global $conf;
65
66  if (!defined('CURRENT_DATE'))
67  {
68    define('CURRENT_DATE', date('Y-m-d'));
69  }
70
71  $datas = array();
72
73  foreach ($files as $id => $file)
74  {
75    $data = array();
76    $data['id'] = $id;
77    $data['filesize'] = floor(filesize($file)/1024);
78 
79    if ($image_size = @getimagesize($file))
80    {
81      $data['width'] = $image_size[0];
82      $data['height'] = $image_size[1];
83    }
84 
85    if ($conf['use_exif'])
86    {
87      if (!function_exists('read_exif_data'))
88      {
89        die('Exif extension not available, admin should disable exif use');
90      }
91     
92      if ($exif = @read_exif_data($file))
93      {
94        if (isset($exif['DateTime']))
95        {
96          preg_match('/^(\d{4}).(\d{2}).(\d{2})/',$exif['DateTime'],$matches);
97          $data['date_creation'] = $matches[1].'-'.$matches[2].'-'.$matches[3];
98        }
99      }
100    }
101
102    if ($conf['use_iptc'])
103    {
104      $iptc = get_sync_iptc_data($file);
105      if (count($iptc) > 0)
106      {
107        foreach (array_keys($iptc) as $key)
108        {
109          $data[$key] = addslashes($iptc[$key]);
110        }
111      }
112    }
113
114    $data['date_metadata_update'] = CURRENT_DATE;
115
116    array_push($datas, $data);
117  }
118 
119  if (count($datas) > 0)
120  {
121    $update_fields = array('filesize','width','height','date_metadata_update');
122    if ($conf['use_exif'])
123    {
124      array_push($update_fields, 'date_creation');
125    }
126    if ($conf['use_iptc'])
127    {
128      $update_fields = array_merge($update_fields,
129                                   array_keys($conf['use_iptc_mapping']));
130    }
131   
132    $fields = array('primary' => array('id'),
133                    'update'  => array_unique($update_fields));
134    mass_updates(IMAGES_TABLE, $fields, $datas);
135  }
136}
137
138/**
139 * returns an array associating element id (images.id) with its complete
140 * path in the filesystem
141 *
142 * @param int id_uppercat
143 * @param boolean recursive ?
144 * @param boolean only newly added files ?
145 * @return array
146 */
147function get_filelist($category_id = '', $recursive = false, $only_new = false)
148{
149  // filling $cat_ids : all categories required
150  $cat_ids = array();
151 
152  $query = '
153SELECT id
154  FROM '.CATEGORIES_TABLE.'
155  WHERE site_id = 1
156    AND dir IS NOT NULL';
157  if (is_numeric($category_id))
158  {
159    if ($recursive)
160    {
161      $query.= '
162    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
163';
164    }
165    else
166    {
167      $query.= '
168    AND id = '.$category_id.'
169';
170    }
171  }
172  $query.= '
173;';
174  $result = pwg_query($query);
175  while ($row = mysql_fetch_array($result))
176  {
177    array_push($cat_ids, $row['id']);
178  }
179
180  if (count($cat_ids) == 0)
181  {
182    return array();
183  }
184
185  $files = array();
186
187  $query = '
188SELECT id, path
189  FROM '.IMAGES_TABLE.'
190  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
191  if ($only_new)
192  {
193    $query.= '
194    AND date_metadata_update IS NULL
195';
196  }
197  $query.= '
198;';
199  $result = pwg_query($query);
200  while ($row = mysql_fetch_array($result))
201  {
202    $files[$row['id']] = $row['path'];
203  }
204 
205  return $files;
206}
207?>
Note: See TracBrowser for help on using the repository browser.