MediaWiki REL1_35
BitmapMetadataHandler.php
Go to the documentation of this file.
1<?php
25use Wikimedia\XMPReader\Reader as XMPReader;
26
43 private $metadata = [];
44
46 private $metaPriority = [
47 20 => [ 'other' ],
48 40 => [ 'native' ],
49 60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
50 70 => [ 'xmp-deprecated' ],
51 80 => [ 'xmp-general' ],
52 90 => [ 'xmp-exif' ],
53 100 => [ 'iptc-bad-hash' ],
54 120 => [ 'exif' ],
55 ];
56
58 private $iptcType = 'iptc-no-hash';
59
68 private function doApp13( $app13 ) {
69 try {
70 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
71 } catch ( Exception $e ) {
72 // Error reading the iptc hash information.
73 // This probably means the App13 segment is something other than what we expect.
74 // However, still try to read it, and treat it as if the hash didn't exist.
75 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() );
76 $this->iptcType = 'iptc-no-hash';
77 }
78
79 $iptc = IPTC::parse( $app13 );
80 $this->addMetadata( $iptc, $this->iptcType );
81 }
82
93 public function getExif( $filename, $byteOrder ) {
94 global $wgShowEXIF;
95 if ( file_exists( $filename ) && $wgShowEXIF ) {
96 $exif = new Exif( $filename, $byteOrder );
97 $data = $exif->getFilteredData();
98 if ( $data ) {
99 $this->addMetadata( $data, 'exif' );
100 }
101 }
102 }
103
110 public function addMetadata( $metaArray, $type = 'other' ) {
111 if ( isset( $this->metadata[$type] ) ) {
112 /* merge with old data */
113 $metaArray += $this->metadata[$type];
114 }
115
116 $this->metadata[$type] = $metaArray;
117 }
118
128 public function getMetadataArray() {
129 // this seems a bit ugly... This is all so its merged in right order
130 // based on the MWG recommendation.
131 $temp = [];
132 krsort( $this->metaPriority );
133 foreach ( $this->metaPriority as $pri ) {
134 foreach ( $pri as $type ) {
135 if ( isset( $this->metadata[$type] ) ) {
136 // Do some special casing for multilingual values.
137 // Don't discard translations if also as a simple value.
138 foreach ( $this->metadata[$type] as $itemName => $item ) {
139 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' &&
140 isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
141 ) {
142 $default = $temp[$itemName];
143 $temp[$itemName] = $item;
144 $temp[$itemName]['x-default'] = $default;
145 unset( $this->metadata[$type][$itemName] );
146 }
147 }
148
149 $temp += $this->metadata[$type];
150 }
151 }
152 }
153
154 return $temp;
155 }
156
163 public static function Jpeg( $filename ) {
164 $showXMP = XMPReader::isSupported();
165 $meta = new self();
166
167 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
168
169 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
170 $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
171 }
172 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
173 foreach ( $seg['PSIR'] as $curPSIRValue ) {
174 $meta->doApp13( $curPSIRValue );
175 }
176 }
177 if ( isset( $seg['XMP'] ) && $showXMP ) {
178 $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
179 $xmp->parse( $seg['XMP'] );
180 foreach ( $seg['XMP_ext'] as $xmpExt ) {
181 /* Support for extended xmp in jpeg files
182 * is not well tested and a bit fragile.
183 */
184 $xmp->parseExtended( $xmpExt );
185 }
186 $res = $xmp->getResults();
187 foreach ( $res as $type => $array ) {
188 $meta->addMetadata( $array, $type );
189 }
190 }
191
192 $meta->getExif( $filename, $seg['byteOrder'] ?? 'BE' );
193
194 return $meta->getMetadataArray();
195 }
196
205 public static function PNG( $filename ) {
206 $showXMP = XMPReader::isSupported();
207
208 $meta = new self();
209 $array = PNGMetadataExtractor::getMetadata( $filename );
210 if ( isset( $array['text']['xmp']['x-default'] )
211 && $array['text']['xmp']['x-default'] !== '' && $showXMP
212 ) {
213 $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
214 $xmp->parse( $array['text']['xmp']['x-default'] );
215 $xmpRes = $xmp->getResults();
216 foreach ( $xmpRes as $type => $xmpSection ) {
217 $meta->addMetadata( $xmpSection, $type );
218 }
219 }
220 unset( $array['text']['xmp'] );
221 $meta->addMetadata( $array['text'], 'native' );
222 unset( $array['text'] );
223 $array['metadata'] = $meta->getMetadataArray();
224 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
225
226 return $array;
227 }
228
237 public static function GIF( $filename ) {
238 $meta = new self();
239 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
240
241 if ( count( $baseArray['comment'] ) > 0 ) {
242 $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
243 }
244
245 if ( $baseArray['xmp'] !== '' && XMPReader::isSupported() ) {
246 $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
247 $xmp->parse( $baseArray['xmp'] );
248 $xmpRes = $xmp->getResults();
249 foreach ( $xmpRes as $type => $xmpSection ) {
250 $meta->addMetadata( $xmpSection, $type );
251 }
252 }
253
254 unset( $baseArray['comment'] );
255 unset( $baseArray['xmp'] );
256
257 $baseArray['metadata'] = $meta->getMetadataArray();
258 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
259
260 return $baseArray;
261 }
262
276 public static function Tiff( $filename ) {
277 if ( file_exists( $filename ) ) {
278 $byteOrder = self::getTiffByteOrder( $filename );
279 if ( !$byteOrder ) {
280 throw new MWException( "Error determining byte order of $filename" );
281 }
282 $exif = new Exif( $filename, $byteOrder );
283 $data = $exif->getFilteredData();
284 if ( $data ) {
285 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
286
287 return $data;
288 } else {
289 throw new MWException( "Could not extract data from tiff file $filename" );
290 }
291 } else {
292 throw new MWException( "File doesn't exist - $filename" );
293 }
294 }
295
303 public static function getTiffByteOrder( $filename ) {
304 $fh = fopen( $filename, 'rb' );
305 if ( !$fh ) {
306 return false;
307 }
308 $head = fread( $fh, 2 );
309 fclose( $fh );
310
311 switch ( $head ) {
312 case 'II':
313 return 'LE'; // II for intel.
314 case 'MM':
315 return 'BE'; // MM for motorla.
316 default:
317 return false; // Something went wrong.
318
319 }
320 }
321}
$wgShowEXIF
Show Exif data, on by default if available.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Class to deal with reconciling and extracting metadata from bitmap images.
getExif( $filename, $byteOrder)
Get exif info using exif class.
addMetadata( $metaArray, $type='other')
Add misc metadata.
static getTiffByteOrder( $filename)
Read the first 2 bytes of a tiff file to figure out Little Endian or Big Endian.
static Tiff( $filename)
This doesn't do much yet, but eventually I plan to add XMP support for Tiff.
array $metaPriority
Metadata priority.
doApp13( $app13)
This does the photoshop image resource app13 block of interest, IPTC-IIM metadata is stored here.
static Jpeg( $filename)
Main entry point for jpeg's.
getMetadataArray()
Merge together the various types of metadata the different types have different priorites,...
static PNG( $filename)
Entry point for png At some point in the future this might merge the png various tEXt chunks to that ...
static GIF( $filename)
function for gif images.
Class to extract and validate Exif data from jpeg (and possibly tiff) files.
Definition Exif.php:32
static version()
#-
Definition Exif.php:582
static getMetadata( $filename)
static parse( $rawData)
This takes the results of iptcparse() and puts it into a form that can be handled by mediawiki.
Definition IPTC.php:40
static doPSIR( $app13)
This reads the photoshop image resource.
static segmentSplitter( $filename)
Function to extract metadata segments of interest from jpeg files based on GIFMetadataExtractor.
MediaWiki exception.
PSR-3 logger instance factory.
static getMetadata( $filename)