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

Last change on this file since 7170 was 5196, checked in by plg, 14 years ago

increase copyright year to 2010

  • Property svn:eol-style set to LF
File size: 7.2 KB
RevLine 
[486]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[486]23
[493]24include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
25
[858]26
[493]27function get_sync_iptc_data($file)
[486]28{
[2560]29  global $conf;
[2521]30
[600]31  $map = $conf['use_iptc_mapping'];
[2521]32
[493]33  $iptc = get_iptc_data($file, $map);
34
35  foreach ($iptc as $pwg_key => $value)
[486]36  {
[2560]37    if (in_array($pwg_key, array('date_creation', 'date_available')))
[486]38    {
[614]39      if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
[486]40      {
[493]41        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
[486]42      }
43    }
44  }
[493]45
[562]46  if (isset($iptc['keywords']))
47  {
[1127]48    // official keywords separator is the comma
49    $iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
[562]50    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
[1223]51
52    $iptc['keywords'] = implode(
53      ',',
54      array_unique(
55        explode(
56          ',',
57          $iptc['keywords']
58          )
59        )
60      );
[562]61  }
62
[1717]63  foreach ($iptc as $pwg_key => $value)
64  {
65    $iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
66  }
67
[493]68  return $iptc;
[486]69}
70
[858]71function get_sync_exif_data($file)
72{
[2560]73  global $conf;
[858]74
75  $exif = get_exif_data($file, $conf['use_exif_mapping']);
76
77  foreach ($exif as $pwg_key => $value)
78  {
[2560]79    if (in_array($pwg_key, array('date_creation', 'date_available')))
[858]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    }
[1717]86    $exif[$pwg_key] = addslashes($exif[$pwg_key]);
[858]87  }
88
89  return $exif;
90}
91
[486]92function update_metadata($files)
93{
94  global $conf;
[582]95
96  if (!defined('CURRENT_DATE'))
97  {
[624]98    define('CURRENT_DATE', date('Y-m-d'));
[582]99  }
100
[625]101  $datas = array();
[1119]102  $tags_of = array();
[1883]103  $has_high_images = array();
[486]104
[1883]105  $image_ids = array();
[486]106  foreach ($files as $id => $file)
107  {
[1883]108    array_push($image_ids, $id);
109  }
[2521]110
[1883]111  $query = '
112SELECT id
113  FROM '.IMAGES_TABLE.'
114  WHERE has_high = \'true\'
115    AND id IN (
116'.wordwrap(implode(', ', $image_ids), 80, "\n").'
117)
118;';
119
[2521]120  $has_high_images = array_from_query($query, 'id');
[1883]121
122  foreach ($files as $id => $file)
123  {
[625]124    $data = array();
125    $data['id'] = $id;
126    $data['filesize'] = floor(filesize($file)/1024);
[2521]127
[486]128    if ($image_size = @getimagesize($file))
129    {
[625]130      $data['width'] = $image_size[0];
131      $data['height'] = $image_size[1];
[486]132    }
[1883]133
134    if (in_array($id, $has_high_images))
135    {
136      $high_file = dirname($file).'/pwg_high/'.basename($file);
137
138      $data['high_filesize'] = floor(filesize($high_file)/1024);
139    }
[2521]140
[486]141    if ($conf['use_exif'])
142    {
[858]143      $exif = get_sync_exif_data($file);
[4683]144      if (count($exif) == 0 and isset($data['high_filesize']))
145      {
146        $exif = get_sync_exif_data($high_file);
147      }
[4681]148      $data = array_merge($data, $exif);
[486]149    }
150
151    if ($conf['use_iptc'])
152    {
[493]153      $iptc = get_sync_iptc_data($file);
[4683]154      if (count($iptc) == 0 and isset($data['high_filesize']))
155      {
156        $iptc = get_sync_iptc_data($high_file);
157      }
[4681]158      $data = array_merge($data, $iptc);
159     
[493]160      if (count($iptc) > 0)
[486]161      {
[493]162        foreach (array_keys($iptc) as $key)
163        {
[1119]164          if ($key == 'keywords' or $key == 'tags')
165          {
166            if (!isset($tags_of[$id]))
167            {
168              $tags_of[$id] = array();
169            }
[2521]170
[1119]171            foreach (explode(',', $iptc[$key]) as $tag_name)
172            {
173              array_push(
174                $tags_of[$id],
175                tag_id_from_tag_name($tag_name)
176                );
177            }
178          }
[493]179        }
[486]180      }
181    }
182
[625]183    $data['date_metadata_update'] = CURRENT_DATE;
[486]184
[625]185    array_push($datas, $data);
[486]186  }
[2521]187
[625]188  if (count($datas) > 0)
[486]189  {
[903]190    $update_fields =
191      array(
192        'filesize',
193        'width',
194        'height',
[1883]195        'high_filesize',
[903]196        'date_metadata_update'
197        );
[2521]198
[702]199    if ($conf['use_exif'])
200    {
[903]201      $update_fields =
202        array_merge(
203          $update_fields,
204          array_keys($conf['use_exif_mapping'])
205          );
[702]206    }
[2521]207
[702]208    if ($conf['use_iptc'])
209    {
[903]210      $update_fields =
211        array_merge(
212          $update_fields,
[1119]213          array_diff(
214            array_keys($conf['use_iptc_mapping']),
215            array('tags', 'keywords')
216            )
[903]217          );
[702]218    }
[858]219
[1127]220    mass_updates(
221      IMAGES_TABLE,
[903]222      array(
223        'primary' => array('id'),
224        'update'  => array_unique($update_fields)
[1127]225        ),
[2521]226      $datas,
227      MASS_UPDATES_SKIP_EMPTY
[1127]228      );
[486]229  }
[1119]230
[1127]231  set_tags_of($tags_of);
[486]232}
233
234/**
235 * returns an array associating element id (images.id) with its complete
236 * path in the filesystem
237 *
238 * @param int id_uppercat
[1029]239 * @param int site_id
[486]240 * @param boolean recursive ?
241 * @param boolean only newly added files ?
242 * @return array
243 */
[2521]244function get_filelist($category_id = '', $site_id=1, $recursive = false,
[1029]245                      $only_new = false)
[486]246{
[657]247  // filling $cat_ids : all categories required
248  $cat_ids = array();
[2521]249
[486]250  $query = '
[657]251SELECT id
[582]252  FROM '.CATEGORIES_TABLE.'
[1029]253  WHERE site_id = '.$site_id.'
[582]254    AND dir IS NOT NULL';
[486]255  if (is_numeric($category_id))
256  {
257    if ($recursive)
258    {
259      $query.= '
[4367]260    AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
[486]261';
262    }
263    else
264    {
265      $query.= '
[582]266    AND id = '.$category_id.'
[486]267';
268    }
269  }
270  $query.= '
271;';
[587]272  $result = pwg_query($query);
[4325]273  while ($row = pwg_db_fetch_assoc($result))
[486]274  {
[657]275    array_push($cat_ids, $row['id']);
[486]276  }
277
[657]278  if (count($cat_ids) == 0)
[590]279  {
280    return array();
281  }
282
[657]283  $files = array();
[486]284
285  $query = '
[657]286SELECT id, path
[486]287  FROM '.IMAGES_TABLE.'
[1121]288  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
[486]289  if ($only_new)
290  {
291    $query.= '
292    AND date_metadata_update IS NULL
293';
294  }
295  $query.= '
296;';
[587]297  $result = pwg_query($query);
[4325]298  while ($row = pwg_db_fetch_assoc($result))
[486]299  {
[657]300    $files[$row['id']] = $row['path'];
[486]301  }
[2521]302
[486]303  return $files;
304}
305?>
Note: See TracBrowser for help on using the repository browser.