And even more documentation
[lhc/web/wiklou.git] / includes / media / BitmapMetadataHandler.php
1 <?php
2 /**
3 Class to deal with reconciling and extracting metadata from bitmap images.
4 This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
5
6 This sort of acts as an intermediary between MediaHandler::getMetadata
7 and the various metadata extractors.
8
9 @todo other image formats.
10 */
11 class BitmapMetadataHandler {
12
13 private $metadata = array();
14 private $metaPriority = array(
15 20 => array( 'other' ),
16 40 => array( 'native' ),
17 60 => array( 'iptc-good-hash', 'iptc-no-hash' ),
18 70 => array( 'xmp-deprecated' ),
19 80 => array( 'xmp-general' ),
20 90 => array( 'xmp-exif' ),
21 100 => array( 'iptc-bad-hash' ),
22 120 => array( 'exif' ),
23 );
24 private $iptcType = 'iptc-no-hash';
25
26 /**
27 * This does the photoshop image resource app13 block
28 * of interest, IPTC-IIM metadata is stored here.
29 *
30 * Mostly just calls doPSIR and doIPTC
31 *
32 * @param String $app13 String containing app13 block from jpeg file
33 */
34 private function doApp13 ( $app13 ) {
35 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
36
37 $iptc = IPTC::parse( $app13 );
38 $this->addMetadata( $iptc, $this->iptcType );
39 }
40
41
42 /**
43 *
44 * get exif info using exif class.
45 * Basically what used to be in BitmapHandler::getMetadata().
46 * Just calls stuff in the Exif class.
47 *
48 * @param $filename string
49 */
50 function getExif ( $filename ) {
51 if ( file_exists( $filename ) ) {
52 $exif = new Exif( $filename );
53 $data = $exif->getFilteredData();
54 if ( $data ) {
55 $this->addMetadata( $data, 'exif' );
56 }
57 }
58 }
59 /** Add misc metadata. Warning: atm if the metadata category
60 * doesn't have a priority, it will be silently discarded.
61 *
62 * @param Array $metaArray array of metadata values
63 * @param string $type type. defaults to other. if two things have the same type they're merged
64 */
65 function addMetadata ( $metaArray, $type = 'other' ) {
66 if ( isset( $this->metadata[$type] ) ) {
67 /* merge with old data */
68 $metaArray = $metaArray + $this->metadata[$type];
69 }
70
71 $this->metadata[$type] = $metaArray;
72 }
73
74 /**
75 * Merge together the various types of metadata
76 * the different types have different priorites,
77 * and are merged in order.
78 *
79 * This function is generally called by the media handlers' getMetadata()
80 *
81 * @return Array metadata array
82 */
83 function getMetadataArray () {
84 // this seems a bit ugly... This is all so its merged in right order
85 // based on the MWG recomendation.
86 $temp = Array();
87 krsort( $this->metaPriority );
88 foreach ( $this->metaPriority as $pri ) {
89 foreach ( $pri as $type ) {
90 if ( isset( $this->metadata[$type] ) ) {
91 // Do some special casing for multilingual values.
92 // Don't discard translations if also as a simple value.
93 foreach ( $this->metadata[$type] as $itemName => $item ) {
94 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
95 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
96 $default = $temp[$itemName];
97 $temp[$itemName] = $item;
98 $temp[$itemName]['x-default'] = $default;
99 unset( $this->metadata[$type][$itemName] );
100 }
101 }
102 }
103
104 $temp = $temp + $this->metadata[$type];
105 }
106 }
107 }
108 return $temp;
109 }
110
111 /** Main entry point for jpeg's.
112 *
113 * @param string $file filename (with full path)
114 * @return metadata result array.
115 * @throws MWException on invalid file.
116 */
117 static function Jpeg ( $filename ) {
118 $showXMP = function_exists( 'xml_parser_create_ns' );
119 $meta = new self();
120 $meta->getExif( $filename );
121
122 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
123 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
124 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
125 }
126 if ( isset( $seg['PSIR'] ) ) {
127 $meta->doApp13( $seg['PSIR'] );
128 }
129 if ( isset( $seg['XMP'] ) && $showXMP ) {
130 $xmp = new XMPReader();
131 $xmp->parse( $seg['XMP'] );
132 foreach ( $seg['XMP_ext'] as $xmpExt ) {
133 /* Support for extended xmp in jpeg files
134 * is not well tested and a bit fragile.
135 */
136 $xmp->parseExtended( $xmpExt );
137
138 }
139 $res = $xmp->getResults();
140 foreach ( $res as $type => $array ) {
141 $meta->addMetadata( $array, $type );
142 }
143 }
144 return $meta->getMetadataArray();
145 }
146 /** Entry point for png
147 * At some point in the future this might
148 * merge the png various tEXt chunks to that
149 * are interesting, but for now it only does XMP
150 *
151 * @param $filename String full path to file
152 * @return Array Array for storage in img_metadata.
153 */
154 static public function PNG ( $filename ) {
155 $showXMP = function_exists( 'xml_parser_create_ns' );
156
157 $meta = new self();
158 $array = PNGMetadataExtractor::getMetadata( $filename );
159 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
160 $xmp = new XMPReader();
161 $xmp->parse( $array['text']['xmp']['x-default'] );
162 $xmpRes = $xmp->getResults();
163 foreach ( $xmpRes as $type => $xmpSection ) {
164 $meta->addMetadata( $xmpSection, $type );
165 }
166 }
167 unset( $array['text']['xmp'] );
168 $meta->addMetadata( $array['text'], 'native' );
169 unset( $array['text'] );
170 $array['metadata'] = $meta->getMetadataArray();
171 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
172 return $array;
173 }
174
175 /** function for gif images.
176 *
177 * They don't really have native metadata, so just merges together
178 * XMP and image comment.
179 *
180 * @param $filename full path to file
181 * @return Array metadata array
182 */
183 static public function GIF ( $filename ) {
184
185 $meta = new self();
186 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
187
188 if ( count( $baseArray['comment'] ) > 0 ) {
189 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
190 }
191
192 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
193 $xmp = new XMPReader();
194 $xmp->parse( $baseArray['xmp'] );
195 $xmpRes = $xmp->getResults();
196 foreach ( $xmpRes as $type => $xmpSection ) {
197 $meta->addMetadata( $xmpSection, $type );
198 }
199
200 }
201
202 unset( $baseArray['comment'] );
203 unset( $baseArray['xmp'] );
204
205 $baseArray['metadata'] = $meta->getMetadataArray();
206 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
207 return $baseArray;
208 }
209
210 }