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