Remove some unused variables
[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 /** get exif info using exif class.
43 * Basically what used to be in BitmapHandler::getMetadata().
44 * Just calls stuff in the Exif class.
45 */
46 function getExif ( $filename ) {
47 if ( file_exists( $filename ) ) {
48 $exif = new Exif( $filename );
49 $data = $exif->getFilteredData();
50 if ( $data ) {
51 $this->addMetadata( $data, 'exif' );
52 }
53 }
54 }
55 /** Add misc metadata. Warning: atm if the metadata category
56 * doesn't have a priority, it will be silently discarded.
57 *
58 * @param Array $metaArray array of metadata values
59 * @param string $type type. defaults to other. if two things have the same type they're merged
60 */
61 function addMetadata ( $metaArray, $type = 'other' ) {
62 if ( isset( $this->metadata[$type] ) ) {
63 /* merge with old data */
64 $metaArray = $metaArray + $this->metadata[$type];
65 }
66
67 $this->metadata[$type] = $metaArray;
68 }
69
70 /**
71 * Merge together the various types of metadata
72 * the different types have different priorites,
73 * and are merged in order.
74 *
75 * This function is generally called by the media handlers' getMetadata()
76 *
77 * @return Array metadata array
78 */
79 function getMetadataArray () {
80 // this seems a bit ugly... This is all so its merged in right order
81 // based on the MWG recomendation.
82 $temp = Array();
83 krsort( $this->metaPriority );
84 foreach ( $this->metaPriority as $pri ) {
85 foreach ( $pri as $type ) {
86 if ( isset( $this->metadata[$type] ) ) {
87 // Do some special casing for multilingual values.
88 // Don't discard translations if also as a simple value.
89 foreach ( $this->metadata[$type] as $itemName => $item ) {
90 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
91 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
92 $default = $temp[$itemName];
93 $temp[$itemName] = $item;
94 $temp[$itemName]['x-default'] = $default;
95 unset( $this->metadata[$type][$itemName] );
96 }
97 }
98 }
99
100 $temp = $temp + $this->metadata[$type];
101 }
102 }
103 }
104 return $temp;
105 }
106
107 /** Main entry point for jpeg's.
108 *
109 * @param string $file filename (with full path)
110 * @return metadata result array.
111 * @throws MWException on invalid file.
112 */
113 static function Jpeg ( $filename ) {
114 $showXMP = function_exists( 'xml_parser_create_ns' );
115 $meta = new self();
116 $meta->getExif( $filename );
117
118 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
119 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
120 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
121 }
122 if ( isset( $seg['PSIR'] ) ) {
123 $meta->doApp13( $seg['PSIR'] );
124 }
125 if ( isset( $seg['XMP'] ) && $showXMP ) {
126 $xmp = new XMPReader();
127 $xmp->parse( $seg['XMP'] );
128 foreach ( $seg['XMP_ext'] as $xmpExt ) {
129 /* Support for extended xmp in jpeg files
130 * is not well tested and a bit fragile.
131 */
132 $xmp->parseExtended( $xmpExt );
133
134 }
135 $res = $xmp->getResults();
136 foreach ( $res as $type => $array ) {
137 $meta->addMetadata( $array, $type );
138 }
139 }
140 return $meta->getMetadataArray();
141 }
142 /** Entry point for png
143 * At some point in the future this might
144 * merge the png various tEXt chunks to that
145 * are interesting, but for now it only does XMP
146 *
147 * @param $filename String full path to file
148 * @return Array Array for storage in img_metadata.
149 */
150 static public function PNG ( $filename ) {
151 $showXMP = function_exists( 'xml_parser_create_ns' );
152
153 $meta = new self();
154 $array = PNGMetadataExtractor::getMetadata( $filename );
155 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
156 $xmp = new XMPReader();
157 $xmp->parse( $array['text']['xmp']['x-default'] );
158 $xmpRes = $xmp->getResults();
159 foreach ( $xmpRes as $type => $xmpSection ) {
160 $meta->addMetadata( $xmpSection, $type );
161 }
162 }
163 unset( $array['text']['xmp'] );
164 $meta->addMetadata( $array['text'], 'native' );
165 unset( $array['text'] );
166 $array['metadata'] = $meta->getMetadataArray();
167 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
168 return $array;
169 }
170
171 /** function for gif images.
172 *
173 * They don't really have native metadata, so just merges together
174 * XMP and image comment.
175 *
176 * @param $filename full path to file
177 * @return Array metadata array
178 */
179 static public function GIF ( $filename ) {
180
181 $meta = new self();
182 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
183
184 if ( count( $baseArray['comment'] ) > 0 ) {
185 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
186 }
187
188 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
189 $xmp = new XMPReader();
190 $xmp->parse( $baseArray['xmp'] );
191 $xmpRes = $xmp->getResults();
192 foreach ( $xmpRes as $type => $xmpSection ) {
193 $meta->addMetadata( $xmpSection, $type );
194 }
195
196 }
197
198 unset( $baseArray['comment'] );
199 unset( $baseArray['xmp'] );
200
201 $baseArray['metadata'] = $meta->getMetadataArray();
202 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
203 return $baseArray;
204 }
205
206 }