1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based picture gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2009 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 | /** |
---|
25 | * returns informations from IPTC metadata, mapping is done at the beginning |
---|
26 | * of the function |
---|
27 | * |
---|
28 | * @param string $filename |
---|
29 | * @return array |
---|
30 | */ |
---|
31 | function get_iptc_data($filename, $map) |
---|
32 | { |
---|
33 | $result = array(); |
---|
34 | |
---|
35 | $imginfo = array(); |
---|
36 | if (false == @getimagesize($filename, $imginfo) ) |
---|
37 | { |
---|
38 | return $result; |
---|
39 | } |
---|
40 | |
---|
41 | if (isset($imginfo['APP13'])) |
---|
42 | { |
---|
43 | $iptc = iptcparse($imginfo['APP13']); |
---|
44 | if (is_array($iptc)) |
---|
45 | { |
---|
46 | $rmap = array_flip($map); |
---|
47 | foreach (array_keys($rmap) as $iptc_key) |
---|
48 | { |
---|
49 | if (isset($iptc[$iptc_key][0])) |
---|
50 | { |
---|
51 | if ($iptc_key == '2#025') |
---|
52 | { |
---|
53 | $value = implode(',', |
---|
54 | array_map('clean_iptc_value',$iptc[$iptc_key])); |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | $value = clean_iptc_value($iptc[$iptc_key][0]); |
---|
59 | } |
---|
60 | |
---|
61 | foreach (array_keys($map, $iptc_key) as $pwg_key) |
---|
62 | { |
---|
63 | $result[$pwg_key] = $value; |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | } |
---|
69 | return $result; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * return a cleaned IPTC value |
---|
74 | * |
---|
75 | * @param string value |
---|
76 | * @return string |
---|
77 | */ |
---|
78 | function clean_iptc_value($value) |
---|
79 | { |
---|
80 | // strip leading zeros (weird Kodak Scanner software) |
---|
81 | while ( isset($value[0]) and $value[0] == chr(0)) |
---|
82 | { |
---|
83 | $value = substr($value, 1); |
---|
84 | } |
---|
85 | // remove binary nulls |
---|
86 | $value = str_replace(chr(0x00), ' ', $value); |
---|
87 | |
---|
88 | if ( preg_match('/[\x80-\xff]/', $value) ) |
---|
89 | { |
---|
90 | // apparently mac uses some MacRoman crap encoding. I don't know |
---|
91 | // how to detect it so a plugin should do the trick. |
---|
92 | $value = trigger_event('clean_iptc_value', $value); |
---|
93 | $is_utf8 = seems_utf8($value); |
---|
94 | $value = convert_charset( $value, |
---|
95 | $is_utf8 ? 'utf-8' : 'iso-8859-1', |
---|
96 | get_pwg_charset() ); |
---|
97 | } |
---|
98 | return $value; |
---|
99 | } |
---|
100 | |
---|
101 | /** |
---|
102 | * returns informations from EXIF metadata, mapping is done at the beginning |
---|
103 | * of the function |
---|
104 | * |
---|
105 | * @param string $filename |
---|
106 | * @return array |
---|
107 | */ |
---|
108 | function get_exif_data($filename, $map) |
---|
109 | { |
---|
110 | $result = array(); |
---|
111 | |
---|
112 | if (!function_exists('read_exif_data')) |
---|
113 | { |
---|
114 | die('Exif extension not available, admin should disable exif use'); |
---|
115 | } |
---|
116 | |
---|
117 | // Read EXIF data |
---|
118 | if ($exif = @read_exif_data($filename)) |
---|
119 | { |
---|
120 | foreach ($map as $key => $field) |
---|
121 | { |
---|
122 | if (strpos($field, ';') === false) |
---|
123 | { |
---|
124 | if (isset($exif[$field])) |
---|
125 | { |
---|
126 | $result[$key] = $exif[$field]; |
---|
127 | } |
---|
128 | } |
---|
129 | else |
---|
130 | { |
---|
131 | $tokens = explode(';', $field); |
---|
132 | if (isset($exif[$tokens[0]][$tokens[1]])) |
---|
133 | { |
---|
134 | $result[$key] = $exif[$tokens[0]][$tokens[1]]; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | return $result; |
---|
141 | } |
---|
142 | ?> |
---|