source: branches/2.0/admin/include/functions_metadata.php @ 3217

Last change on this file since 3217 was 3046, checked in by plg, 15 years ago

Administration: happy new year 2009, all PHP headers updated.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
RevLine 
[486]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[3046]5// | Copyright(C) 2008-2009 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);
[486]144    }
145
146    if ($conf['use_iptc'])
147    {
[493]148      $iptc = get_sync_iptc_data($file);
149      if (count($iptc) > 0)
[486]150      {
[493]151        foreach (array_keys($iptc) as $key)
152        {
[1119]153          if ($key == 'keywords' or $key == 'tags')
154          {
155            if (!isset($tags_of[$id]))
156            {
157              $tags_of[$id] = array();
158            }
[2521]159
[1119]160            foreach (explode(',', $iptc[$key]) as $tag_name)
161            {
162              array_push(
163                $tags_of[$id],
164                tag_id_from_tag_name($tag_name)
165                );
166            }
167          }
[493]168        }
[486]169      }
170    }
171
[625]172    $data['date_metadata_update'] = CURRENT_DATE;
[486]173
[625]174    array_push($datas, $data);
[486]175  }
[2521]176
[625]177  if (count($datas) > 0)
[486]178  {
[903]179    $update_fields =
180      array(
181        'filesize',
182        'width',
183        'height',
[1883]184        'high_filesize',
[903]185        'date_metadata_update'
186        );
[2521]187
[702]188    if ($conf['use_exif'])
189    {
[903]190      $update_fields =
191        array_merge(
192          $update_fields,
193          array_keys($conf['use_exif_mapping'])
194          );
[702]195    }
[2521]196
[702]197    if ($conf['use_iptc'])
198    {
[903]199      $update_fields =
200        array_merge(
201          $update_fields,
[1119]202          array_diff(
203            array_keys($conf['use_iptc_mapping']),
204            array('tags', 'keywords')
205            )
[903]206          );
[702]207    }
[858]208
[1127]209    mass_updates(
210      IMAGES_TABLE,
[903]211      array(
212        'primary' => array('id'),
213        'update'  => array_unique($update_fields)
[1127]214        ),
[2521]215      $datas,
216      MASS_UPDATES_SKIP_EMPTY
[1127]217      );
[486]218  }
[1119]219
[1127]220  set_tags_of($tags_of);
[486]221}
222
223/**
224 * returns an array associating element id (images.id) with its complete
225 * path in the filesystem
226 *
227 * @param int id_uppercat
[1029]228 * @param int site_id
[486]229 * @param boolean recursive ?
230 * @param boolean only newly added files ?
231 * @return array
232 */
[2521]233function get_filelist($category_id = '', $site_id=1, $recursive = false,
[1029]234                      $only_new = false)
[486]235{
[657]236  // filling $cat_ids : all categories required
237  $cat_ids = array();
[2521]238
[486]239  $query = '
[657]240SELECT id
[582]241  FROM '.CATEGORIES_TABLE.'
[1029]242  WHERE site_id = '.$site_id.'
[582]243    AND dir IS NOT NULL';
[486]244  if (is_numeric($category_id))
245  {
246    if ($recursive)
247    {
248      $query.= '
[582]249    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
[486]250';
251    }
252    else
253    {
254      $query.= '
[582]255    AND id = '.$category_id.'
[486]256';
257    }
258  }
259  $query.= '
260;';
[587]261  $result = pwg_query($query);
[486]262  while ($row = mysql_fetch_array($result))
263  {
[657]264    array_push($cat_ids, $row['id']);
[486]265  }
266
[657]267  if (count($cat_ids) == 0)
[590]268  {
269    return array();
270  }
271
[657]272  $files = array();
[486]273
274  $query = '
[657]275SELECT id, path
[486]276  FROM '.IMAGES_TABLE.'
[1121]277  WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
[486]278  if ($only_new)
279  {
280    $query.= '
281    AND date_metadata_update IS NULL
282';
283  }
284  $query.= '
285;';
[587]286  $result = pwg_query($query);
[486]287  while ($row = mysql_fetch_array($result))
288  {
[657]289    $files[$row['id']] = $row['path'];
[486]290  }
[2521]291
[486]292  return $files;
293}
294?>
Note: See TracBrowser for help on using the repository browser.