source: trunk/include/functions_metadata.inc.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
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// +-----------------------------------------------------------------------+
23// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | branch        : BSF (Best So Far)
29// | file          : $Id: functions_metadata.inc.php 2297 2008-04-04 22:57:23Z plg $
30// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
31// | last modifier : $Author: plg $
32// | revision      : $Revision: 2297 $
33// +-----------------------------------------------------------------------+
34// | This program is free software; you can redistribute it and/or modify  |
35// | it under the terms of the GNU General Public License as published by  |
36// | the Free Software Foundation                                          |
37// |                                                                       |
38// | This program is distributed in the hope that it will be useful, but   |
39// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
40// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
41// | General Public License for more details.                              |
42// |                                                                       |
43// | You should have received a copy of the GNU General Public License     |
44// | along with this program; if not, write to the Free Software           |
45// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
46// | USA.                                                                  |
47// +-----------------------------------------------------------------------+
48
49/**
50 * returns informations from IPTC metadata, mapping is done at the beginning
51 * of the function
52 *
53 * @param string $filename
54 * @return array
55 */
56function get_iptc_data($filename, $map)
57{
58  $result = array();
59
60  $imginfo = array();
61  if (false == @getimagesize($filename, $imginfo) )
62  {
63    return $result;
64  }
65
66  if (isset($imginfo['APP13']))
67  {
68    $iptc = iptcparse($imginfo['APP13']);
69    if (is_array($iptc))
70    {
71      $rmap = array_flip($map);
72      foreach (array_keys($rmap) as $iptc_key)
73      {
74        if (isset($iptc[$iptc_key][0]))
75        {
76          if ($iptc_key == '2#025')
77          {
78            $value = implode(',',
79                             array_map('clean_iptc_value',$iptc[$iptc_key]));
80          }
81          else
82          {
83            $value = clean_iptc_value($iptc[$iptc_key][0]);
84          }
85
86          foreach (array_keys($map, $iptc_key) as $pwg_key)
87          {
88            $result[$pwg_key] = $value;
89          }
90        }
91      }
92    }
93  }
94  return $result;
95}
96
97/**
98 * return a cleaned IPTC value
99 *
100 * @param string value
101 * @return string
102 */
103function clean_iptc_value($value)
104{
105  // strip leading zeros (weird Kodak Scanner software)
106  while ( isset($value[0]) and $value[0] == chr(0))
107  {
108    $value = substr($value, 1);
109  }
110  // remove binary nulls
111  $value = str_replace(chr(0x00), ' ', $value);
112
113  if ( preg_match('/[\x80-\xff]/', $value) )
114  {
115    // apparently mac uses some MacRoman crap encoding. I don't know
116    // how to detect it so a plugin should do the trick.
117    $value = trigger_event('clean_iptc_value', $value);
118    $is_utf8 = seems_utf8($value);
119    $value = convert_charset( $value,
120      $is_utf8 ? 'utf-8' : 'iso-8859-1',
121      get_pwg_charset() );
122  }
123  return $value;
124}
125
126/**
127 * returns informations from EXIF metadata, mapping is done at the beginning
128 * of the function
129 *
130 * @param string $filename
131 * @return array
132 */
133function get_exif_data($filename, $map)
134{
135  $result = array();
136
137  if (!function_exists('read_exif_data'))
138  {
139    die('Exif extension not available, admin should disable exif use');
140  }
141
142  // Read EXIF data
143  if ($exif = @read_exif_data($filename))
144  {
145    foreach ($map as $key => $field)
146    {
147      if (strpos($field, ';') === false)
148      {
149        if (isset($exif[$field]))
150        {
151          $result[$key] = $exif[$field];
152        }
153      }
154      else
155      {
156        $tokens = explode(';', $field);
157        if (isset($exif[$tokens[0]][$tokens[1]]))
158        {
159          $result[$key] = $exif[$tokens[0]][$tokens[1]];
160        }
161      }
162    }
163  }
164
165  return $result;
166}
167?>
Note: See TracBrowser for help on using the repository browser.