[493] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 5 | // | Copyright(C) 2008-2011 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 | // +-----------------------------------------------------------------------+ |
---|
[493] | 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(); |
---|
[1113] | 34 | |
---|
[493] | 35 | $imginfo = array(); |
---|
[1711] | 36 | if (false == @getimagesize($filename, $imginfo) ) |
---|
| 37 | { |
---|
| 38 | return $result; |
---|
| 39 | } |
---|
[1113] | 40 | |
---|
[493] | 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 | { |
---|
[611] | 49 | if (isset($iptc[$iptc_key][0])) |
---|
[493] | 50 | { |
---|
[611] | 51 | if ($iptc_key == '2#025') |
---|
[493] | 52 | { |
---|
[611] | 53 | $value = implode(',', |
---|
| 54 | array_map('clean_iptc_value',$iptc[$iptc_key])); |
---|
[493] | 55 | } |
---|
[611] | 56 | else |
---|
| 57 | { |
---|
| 58 | $value = clean_iptc_value($iptc[$iptc_key][0]); |
---|
| 59 | } |
---|
| 60 | |
---|
[493] | 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 | } |
---|
[611] | 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) |
---|
[1113] | 81 | while ( isset($value[0]) and $value[0] == chr(0)) |
---|
[611] | 82 | { |
---|
| 83 | $value = substr($value, 1); |
---|
| 84 | } |
---|
| 85 | // remove binary nulls |
---|
| 86 | $value = str_replace(chr(0x00), ' ', $value); |
---|
[1113] | 87 | |
---|
[2132] | 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 | } |
---|
[611] | 98 | return $value; |
---|
| 99 | } |
---|
[858] | 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 | } |
---|
[1113] | 116 | |
---|
[858] | 117 | // Read EXIF data |
---|
| 118 | if ($exif = @read_exif_data($filename)) |
---|
| 119 | { |
---|
[6484] | 120 | $exif = trigger_event('format_exif_data', $exif, $filename ); |
---|
| 121 | foreach ($map as $key => $field) |
---|
| 122 | { |
---|
| 123 | if (strpos($field, ';') === false) |
---|
| 124 | { |
---|
| 125 | if (isset($exif[$field])) |
---|
| 126 | { |
---|
| 127 | $result[$key] = $exif[$field]; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | $tokens = explode(';', $field); |
---|
| 133 | if (isset($exif[$tokens[0]][$tokens[1]])) |
---|
| 134 | { |
---|
| 135 | $result[$key] = $exif[$tokens[0]][$tokens[1]]; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
[858] | 139 | } |
---|
| 140 | |
---|
| 141 | return $result; |
---|
| 142 | } |
---|
[493] | 143 | ?> |
---|