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

Last change on this file since 8227 was 7507, checked in by plg, 14 years ago

merge r7506 from branch 2.1 to trunk

bug 1974 fixed: don't generate invalid creation date from IPTC,
if month = 0 or day = 0, then month=1 and day=1.

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