MediaWiki  master
BitmapMetadataHandler.php
Go to the documentation of this file.
1 <?php
27 use Wikimedia\XMPReader\Reader as XMPReader;
28 
45  private $metadata = [];
46 
48  private $metaPriority = [
49  20 => [ 'other' ],
50  40 => [ 'native' ],
51  60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
52  70 => [ 'xmp-deprecated' ],
53  80 => [ 'xmp-general' ],
54  90 => [ 'xmp-exif' ],
55  100 => [ 'iptc-bad-hash' ],
56  120 => [ 'exif' ],
57  ];
58 
60  private $iptcType = 'iptc-no-hash';
61 
70  private function doApp13( $app13 ) {
71  try {
72  $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
73  } catch ( InvalidPSIRException $e ) {
74  // Error reading the iptc hash information.
75  // This probably means the App13 segment is something other than what we expect.
76  // However, still try to read it, and treat it as if the hash didn't exist.
77  wfDebug( "Error parsing iptc data of file: " . $e->getMessage() );
78  $this->iptcType = 'iptc-no-hash';
79  }
80 
81  $iptc = IPTC::parse( $app13 );
82  $this->addMetadata( $iptc, $this->iptcType );
83  }
84 
95  public function getExif( $filename, $byteOrder ) {
96  $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
97  if ( file_exists( $filename ) && $showEXIF ) {
98  $exif = new Exif( $filename, $byteOrder );
99  $data = $exif->getFilteredData();
100  if ( $data ) {
101  $this->addMetadata( $data, 'exif' );
102  }
103  }
104  }
105 
112  public function addMetadata( $metaArray, $type = 'other' ) {
113  if ( isset( $this->metadata[$type] ) ) {
114  /* merge with old data */
115  $metaArray += $this->metadata[$type];
116  }
117 
118  $this->metadata[$type] = $metaArray;
119  }
120 
130  public function getMetadataArray() {
131  // this seems a bit ugly... This is all so its merged in right order
132  // based on the MWG recommendation.
133  $temp = [];
134  krsort( $this->metaPriority );
135  foreach ( $this->metaPriority as $pri ) {
136  foreach ( $pri as $type ) {
137  if ( isset( $this->metadata[$type] ) ) {
138  // Do some special casing for multilingual values.
139  // Don't discard translations if also as a simple value.
140  foreach ( $this->metadata[$type] as $itemName => $item ) {
141  if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' &&
142  isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
143  ) {
144  $default = $temp[$itemName];
145  $temp[$itemName] = $item;
146  $temp[$itemName]['x-default'] = $default;
147  unset( $this->metadata[$type][$itemName] );
148  }
149  }
150 
151  $temp += $this->metadata[$type];
152  }
153  }
154  }
155 
156  return $temp;
157  }
158 
165  public static function Jpeg( $filename ) {
166  $showXMP = XMPReader::isSupported();
167  $meta = new self();
168 
169  $seg = JpegMetadataExtractor::segmentSplitter( $filename );
170 
171  if ( isset( $seg['SOF'] ) ) {
172  $meta->addMetadata( [ 'SOF' => $seg['SOF'] ] );
173  }
174  if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
175  $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
176  }
177  if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
178  foreach ( $seg['PSIR'] as $curPSIRValue ) {
179  $meta->doApp13( $curPSIRValue );
180  }
181  }
182  if ( isset( $seg['XMP'] ) && $showXMP ) {
183  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
184  $xmp->parse( $seg['XMP'] );
185  foreach ( $seg['XMP_ext'] as $xmpExt ) {
186  /* Support for extended xmp in jpeg files
187  * is not well tested and a bit fragile.
188  */
189  $xmp->parseExtended( $xmpExt );
190  }
191  $res = $xmp->getResults();
192  foreach ( $res as $type => $array ) {
193  $meta->addMetadata( $array, $type );
194  }
195  }
196 
197  $meta->getExif( $filename, $seg['byteOrder'] ?? 'BE' );
198 
199  return $meta->getMetadataArray();
200  }
201 
210  public static function PNG( $filename ) {
211  $showXMP = XMPReader::isSupported();
212 
213  $meta = new self();
214  $array = PNGMetadataExtractor::getMetadata( $filename );
215  if ( isset( $array['text']['xmp']['x-default'] )
216  && $array['text']['xmp']['x-default'] !== '' && $showXMP
217  ) {
218  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
219  $xmp->parse( $array['text']['xmp']['x-default'] );
220  $xmpRes = $xmp->getResults();
221  foreach ( $xmpRes as $type => $xmpSection ) {
222  $meta->addMetadata( $xmpSection, $type );
223  }
224  }
225  unset( $array['text']['xmp'] );
226  // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset xmp is not alone in text, makes text always set
227  $meta->addMetadata( $array['text'], 'native' );
228  unset( $array['text'] );
229  $array['metadata'] = $meta->getMetadataArray();
230  $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
231 
232  return $array;
233  }
234 
243  public static function GIF( $filename ) {
244  $meta = new self();
245  $baseArray = GIFMetadataExtractor::getMetadata( $filename );
246 
247  if ( count( $baseArray['comment'] ) > 0 ) {
248  $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
249  }
250 
251  if ( $baseArray['xmp'] !== '' && XMPReader::isSupported() ) {
252  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
253  $xmp->parse( $baseArray['xmp'] );
254  $xmpRes = $xmp->getResults();
255  foreach ( $xmpRes as $type => $xmpSection ) {
256  $meta->addMetadata( $xmpSection, $type );
257  }
258  }
259 
260  unset( $baseArray['comment'] );
261  unset( $baseArray['xmp'] );
262 
263  $baseArray['metadata'] = $meta->getMetadataArray();
264  $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
265 
266  return $baseArray;
267  }
268 
282  public static function Tiff( $filename ) {
283  if ( file_exists( $filename ) ) {
284  $byteOrder = self::getTiffByteOrder( $filename );
285  if ( !$byteOrder ) {
286  throw new InvalidTiffException( "Error determining byte order of $filename" );
287  }
288  $exif = new Exif( $filename, $byteOrder );
289  $data = $exif->getFilteredData();
290  if ( $data ) {
291  $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
292 
293  return $data;
294  } else {
295  throw new InvalidTiffException( "Could not extract data from tiff file $filename" );
296  }
297  } else {
298  throw new InvalidTiffException( "File doesn't exist - $filename" );
299  }
300  }
301 
309  public static function getTiffByteOrder( $filename ) {
310  $fh = fopen( $filename, 'rb' );
311  if ( !$fh ) {
312  return false;
313  }
314  $head = fread( $fh, 2 );
315  fclose( $fh );
316 
317  switch ( $head ) {
318  case 'II':
319  return 'LE'; // II for intel.
320  case 'MM':
321  return 'BE'; // MM for motorla.
322  default:
323  return false; // Something went wrong.
324 
325  }
326  }
327 }
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.
static Jpeg( $filename)
Main entry point for jpeg's.
getMetadataArray()
Merge together the various types of metadata the different types have different priorities,...
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:35
static version()
#-
Definition: Exif.php:714
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:42
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.
PSR-3 logger instance factory.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
static getMetadata( $filename)