3 * Extraction of metadata from different bitmap image types.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Class to deal with reconciling and extracting metadata from bitmap images.
26 * This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
28 * This sort of acts as an intermediary between MediaHandler::getMetadata
29 * and the various metadata extractors.
31 * @todo other image formats.
34 class BitmapMetadataHandler
{
36 private $metadata = array();
37 private $metaPriority = array(
38 20 => array( 'other' ),
39 40 => array( 'native' ),
40 60 => array( 'iptc-good-hash', 'iptc-no-hash' ),
41 70 => array( 'xmp-deprecated' ),
42 80 => array( 'xmp-general' ),
43 90 => array( 'xmp-exif' ),
44 100 => array( 'iptc-bad-hash' ),
45 120 => array( 'exif' ),
47 private $iptcType = 'iptc-no-hash';
50 * This does the photoshop image resource app13 block
51 * of interest, IPTC-IIM metadata is stored here.
53 * Mostly just calls doPSIR and doIPTC
55 * @param string $app13 String containing app13 block from jpeg file
57 private function doApp13( $app13 ) {
59 $this->iptcType
= JpegMetadataExtractor
::doPSIR( $app13 );
60 } catch ( MWException
$e ) {
61 // Error reading the iptc hash information.
62 // This probably means the App13 segment is something other than what we expect.
63 // However, still try to read it, and treat it as if the hash didn't exist.
64 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
65 $this->iptcType
= 'iptc-no-hash';
68 $iptc = IPTC
::parse( $app13 );
69 $this->addMetadata( $iptc, $this->iptcType
);
73 * Get exif info using exif class.
74 * Basically what used to be in BitmapHandler::getMetadata().
75 * Just calls stuff in the Exif class.
77 * Parameters are passed to the Exif class.
79 * @param $filename string
80 * @param $byteOrder string
82 function getExif( $filename, $byteOrder ) {
84 if ( file_exists( $filename ) && $wgShowEXIF ) {
85 $exif = new Exif( $filename, $byteOrder );
86 $data = $exif->getFilteredData();
88 $this->addMetadata( $data, 'exif' );
92 /** Add misc metadata. Warning: atm if the metadata category
93 * doesn't have a priority, it will be silently discarded.
95 * @param array $metaArray array of metadata values
96 * @param string $type type. defaults to other. if two things have the same type they're merged
98 function addMetadata( $metaArray, $type = 'other' ) {
99 if ( isset( $this->metadata
[$type] ) ) {
100 /* merge with old data */
101 $metaArray = $metaArray +
$this->metadata
[$type];
104 $this->metadata
[$type] = $metaArray;
108 * Merge together the various types of metadata
109 * the different types have different priorites,
110 * and are merged in order.
112 * This function is generally called by the media handlers' getMetadata()
114 * @return Array metadata array
116 function getMetadataArray() {
117 // this seems a bit ugly... This is all so its merged in right order
118 // based on the MWG recomendation.
120 krsort( $this->metaPriority
);
121 foreach ( $this->metaPriority
as $pri ) {
122 foreach ( $pri as $type ) {
123 if ( isset( $this->metadata
[$type] ) ) {
124 // Do some special casing for multilingual values.
125 // Don't discard translations if also as a simple value.
126 foreach ( $this->metadata
[$type] as $itemName => $item ) {
127 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
128 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
129 $default = $temp[$itemName];
130 $temp[$itemName] = $item;
131 $temp[$itemName]['x-default'] = $default;
132 unset( $this->metadata
[$type][$itemName] );
137 $temp = $temp +
$this->metadata
[$type];
144 /** Main entry point for jpeg's.
146 * @param string $filename filename (with full path)
147 * @return array metadata result array.
148 * @throws MWException on invalid file.
150 static function Jpeg( $filename ) {
151 $showXMP = function_exists( 'xml_parser_create_ns' );
154 $seg = JpegMetadataExtractor
::segmentSplitter( $filename );
155 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
156 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
158 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
159 foreach( $seg['PSIR'] as $curPSIRValue ) {
160 $meta->doApp13( $curPSIRValue );
163 if ( isset( $seg['XMP'] ) && $showXMP ) {
164 $xmp = new XMPReader();
165 $xmp->parse( $seg['XMP'] );
166 foreach ( $seg['XMP_ext'] as $xmpExt ) {
167 /* Support for extended xmp in jpeg files
168 * is not well tested and a bit fragile.
170 $xmp->parseExtended( $xmpExt );
173 $res = $xmp->getResults();
174 foreach ( $res as $type => $array ) {
175 $meta->addMetadata( $array, $type );
178 if ( isset( $seg['byteOrder'] ) ) {
179 $meta->getExif( $filename, $seg['byteOrder'] );
181 return $meta->getMetadataArray();
184 /** Entry point for png
185 * At some point in the future this might
186 * merge the png various tEXt chunks to that
187 * are interesting, but for now it only does XMP
189 * @param string $filename full path to file
190 * @return Array Array for storage in img_metadata.
192 public static function PNG( $filename ) {
193 $showXMP = function_exists( 'xml_parser_create_ns' );
196 $array = PNGMetadataExtractor
::getMetadata( $filename );
197 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
198 $xmp = new XMPReader();
199 $xmp->parse( $array['text']['xmp']['x-default'] );
200 $xmpRes = $xmp->getResults();
201 foreach ( $xmpRes as $type => $xmpSection ) {
202 $meta->addMetadata( $xmpSection, $type );
205 unset( $array['text']['xmp'] );
206 $meta->addMetadata( $array['text'], 'native' );
207 unset( $array['text'] );
208 $array['metadata'] = $meta->getMetadataArray();
209 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor
::VERSION
;
213 /** function for gif images.
215 * They don't really have native metadata, so just merges together
216 * XMP and image comment.
218 * @param string $filename full path to file
219 * @return Array metadata array
221 public static function GIF( $filename ) {
224 $baseArray = GIFMetadataExtractor
::getMetadata( $filename );
226 if ( count( $baseArray['comment'] ) > 0 ) {
227 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
230 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
231 $xmp = new XMPReader();
232 $xmp->parse( $baseArray['xmp'] );
233 $xmpRes = $xmp->getResults();
234 foreach ( $xmpRes as $type => $xmpSection ) {
235 $meta->addMetadata( $xmpSection, $type );
240 unset( $baseArray['comment'] );
241 unset( $baseArray['xmp'] );
243 $baseArray['metadata'] = $meta->getMetadataArray();
244 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor
::VERSION
;
249 * This doesn't do much yet, but eventually I plan to add
250 * XMP support for Tiff. (PHP's exif support already extracts
251 * but needs some further processing because PHP's exif support
254 * @todo Add XMP support, so this function actually makes
257 * The various exceptions this throws are caught later.
258 * @param $filename String
259 * @throws MWException
260 * @return Array The metadata.
262 public static function Tiff( $filename ) {
263 if ( file_exists( $filename ) ) {
264 $byteOrder = self
::getTiffByteOrder( $filename );
266 throw new MWException( "Error determining byte order of $filename" );
268 $exif = new Exif( $filename, $byteOrder );
269 $data = $exif->getFilteredData();
271 $data['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
274 throw new MWException( "Could not extract data from tiff file $filename" );
277 throw new MWException( "File doesn't exist - $filename" );
281 * Read the first 2 bytes of a tiff file to figure out
282 * Little Endian or Big Endian. Needed for exif stuff.
284 * @param string $filename The filename
285 * @return String 'BE' or 'LE' or false
287 static function getTiffByteOrder( $filename ) {
288 $fh = fopen( $filename, 'rb' );
289 if ( !$fh ) return false;
290 $head = fread( $fh, 2 );
295 return 'LE'; // II for intel.
297 return 'BE'; // MM for motorla.
299 return false; // Something went wrong.