[493] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 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); |
---|
[17748] | 93 | if ( ($qual = qualify_utf8($value)) != 0) |
---|
| 94 | {// has non ascii chars |
---|
| 95 | $value = convert_charset( $value, |
---|
| 96 | $qual>0 ? 'utf-8' : 'iso-8859-1', |
---|
| 97 | get_pwg_charset() ); |
---|
| 98 | } |
---|
[2132] | 99 | } |
---|
[611] | 100 | return $value; |
---|
| 101 | } |
---|
[858] | 102 | |
---|
| 103 | /** |
---|
| 104 | * returns informations from EXIF metadata, mapping is done at the beginning |
---|
| 105 | * of the function |
---|
| 106 | * |
---|
| 107 | * @param string $filename |
---|
| 108 | * @return array |
---|
| 109 | */ |
---|
| 110 | function get_exif_data($filename, $map) |
---|
| 111 | { |
---|
| 112 | $result = array(); |
---|
| 113 | |
---|
| 114 | if (!function_exists('read_exif_data')) |
---|
| 115 | { |
---|
| 116 | die('Exif extension not available, admin should disable exif use'); |
---|
| 117 | } |
---|
[1113] | 118 | |
---|
[858] | 119 | // Read EXIF data |
---|
| 120 | if ($exif = @read_exif_data($filename)) |
---|
| 121 | { |
---|
[12254] | 122 | $exif = trigger_event('format_exif_data', $exif, $filename, $map ); |
---|
[6484] | 123 | foreach ($map as $key => $field) |
---|
| 124 | { |
---|
| 125 | if (strpos($field, ';') === false) |
---|
| 126 | { |
---|
| 127 | if (isset($exif[$field])) |
---|
| 128 | { |
---|
| 129 | $result[$key] = $exif[$field]; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | $tokens = explode(';', $field); |
---|
| 135 | if (isset($exif[$tokens[0]][$tokens[1]])) |
---|
| 136 | { |
---|
| 137 | $result[$key] = $exif[$tokens[0]][$tokens[1]]; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
[858] | 141 | } |
---|
| 142 | |
---|
| 143 | return $result; |
---|
| 144 | } |
---|
[493] | 145 | ?> |
---|