MediaWiki  master
BitmapMetadataHandler.php
Go to the documentation of this file.
1 <?php
25 use 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['SOF'] ) ) {
170  $meta->addMetadata( [ 'SOF' => $seg['SOF'] ] );
171  }
172  if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
173  $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
174  }
175  if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
176  foreach ( $seg['PSIR'] as $curPSIRValue ) {
177  $meta->doApp13( $curPSIRValue );
178  }
179  }
180  if ( isset( $seg['XMP'] ) && $showXMP ) {
181  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
182  $xmp->parse( $seg['XMP'] );
183  foreach ( $seg['XMP_ext'] as $xmpExt ) {
184  /* Support for extended xmp in jpeg files
185  * is not well tested and a bit fragile.
186  */
187  $xmp->parseExtended( $xmpExt );
188  }
189  $res = $xmp->getResults();
190  foreach ( $res as $type => $array ) {
191  $meta->addMetadata( $array, $type );
192  }
193  }
194 
195  $meta->getExif( $filename, $seg['byteOrder'] ?? 'BE' );
196 
197  return $meta->getMetadataArray();
198  }
199 
208  public static function PNG( $filename ) {
209  $showXMP = XMPReader::isSupported();
210 
211  $meta = new self();
212  $array = PNGMetadataExtractor::getMetadata( $filename );
213  if ( isset( $array['text']['xmp']['x-default'] )
214  && $array['text']['xmp']['x-default'] !== '' && $showXMP
215  ) {
216  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
217  $xmp->parse( $array['text']['xmp']['x-default'] );
218  $xmpRes = $xmp->getResults();
219  foreach ( $xmpRes as $type => $xmpSection ) {
220  $meta->addMetadata( $xmpSection, $type );
221  }
222  }
223  unset( $array['text']['xmp'] );
224  $meta->addMetadata( $array['text'], 'native' );
225  unset( $array['text'] );
226  $array['metadata'] = $meta->getMetadataArray();
227  $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
228 
229  return $array;
230  }
231 
240  public static function GIF( $filename ) {
241  $meta = new self();
242  $baseArray = GIFMetadataExtractor::getMetadata( $filename );
243 
244  if ( count( $baseArray['comment'] ) > 0 ) {
245  $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
246  }
247 
248  if ( $baseArray['xmp'] !== '' && XMPReader::isSupported() ) {
249  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
250  $xmp->parse( $baseArray['xmp'] );
251  $xmpRes = $xmp->getResults();
252  foreach ( $xmpRes as $type => $xmpSection ) {
253  $meta->addMetadata( $xmpSection, $type );
254  }
255  }
256 
257  unset( $baseArray['comment'] );
258  unset( $baseArray['xmp'] );
259 
260  $baseArray['metadata'] = $meta->getMetadataArray();
261  $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
262 
263  return $baseArray;
264  }
265 
279  public static function Tiff( $filename ) {
280  if ( file_exists( $filename ) ) {
281  $byteOrder = self::getTiffByteOrder( $filename );
282  if ( !$byteOrder ) {
283  throw new MWException( "Error determining byte order of $filename" );
284  }
285  $exif = new Exif( $filename, $byteOrder );
286  $data = $exif->getFilteredData();
287  if ( $data ) {
288  $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
289 
290  return $data;
291  } else {
292  throw new MWException( "Could not extract data from tiff file $filename" );
293  }
294  } else {
295  throw new MWException( "File doesn't exist - $filename" );
296  }
297  }
298 
306  public static function getTiffByteOrder( $filename ) {
307  $fh = fopen( $filename, 'rb' );
308  if ( !$fh ) {
309  return false;
310  }
311  $head = fread( $fh, 2 );
312  fclose( $fh );
313 
314  switch ( $head ) {
315  case 'II':
316  return 'LE'; // II for intel.
317  case 'MM':
318  return 'BE'; // MM for motorla.
319  default:
320  return false; // Something went wrong.
321 
322  }
323  }
324 }
BitmapMetadataHandler\Tiff
static Tiff( $filename)
This doesn't do much yet, but eventually I plan to add XMP support for Tiff.
Definition: BitmapMetadataHandler.php:279
PNGMetadataExtractor\VERSION
const VERSION
Definition: PNGMetadataExtractor.php:43
BitmapMetadataHandler\$iptcType
string $iptcType
Definition: BitmapMetadataHandler.php:58
BitmapMetadataHandler\GIF
static GIF( $filename)
function for gif images.
Definition: BitmapMetadataHandler.php:240
$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:41
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:46
JpegMetadataExtractor\doPSIR
static doPSIR( $app13)
This reads the photoshop image resource.
Definition: JpegMetadataExtractor.php:219
Exif\version
static version()
#-
Definition: Exif.php:592
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 priorites,...
Definition: BitmapMetadataHandler.php:128
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:208
BitmapMetadataHandler\getExif
getExif( $filename, $byteOrder)
Get exif info using exif class.
Definition: BitmapMetadataHandler.php:93
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:68
BitmapMetadataHandler\$metadata
array $metadata
Definition: BitmapMetadataHandler.php:43
GIFMetadataExtractor\VERSION
const VERSION
Definition: GIFMetadataExtractor.php:44
BitmapMetadataHandler\Jpeg
static Jpeg( $filename)
Main entry point for jpeg's.
Definition: BitmapMetadataHandler.php:163
$wgShowEXIF
$wgShowEXIF
Show Exif data, on by default if available.
Definition: DefaultSettings.php:962
BitmapMetadataHandler\addMetadata
addMetadata( $metaArray, $type='other')
Add misc metadata.
Definition: BitmapMetadataHandler.php:110
$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:306