3 * Extraction of metadata from different bitmap image types.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\Logger\LoggerFactory
;
25 use Wikimedia\XMPReader\Reader
as XMPReader
;
28 * Class to deal with reconciling and extracting metadata from bitmap images.
29 * This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
31 * This sort of acts as an intermediary between MediaHandler::getMetadata
32 * and the various metadata extractors.
34 * @todo Other image formats.
37 class BitmapMetadataHandler
{
39 private $metadata = [];
41 /** @var array Metadata priority */
42 private $metaPriority = [
45 60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
46 70 => [ 'xmp-deprecated' ],
47 80 => [ 'xmp-general' ],
49 100 => [ 'iptc-bad-hash' ],
54 private $iptcType = 'iptc-no-hash';
57 * This does the photoshop image resource app13 block
58 * of interest, IPTC-IIM metadata is stored here.
60 * Mostly just calls doPSIR and doIPTC
62 * @param string $app13 String containing app13 block from jpeg file
64 private function doApp13( $app13 ) {
66 $this->iptcType
= JpegMetadataExtractor
::doPSIR( $app13 );
67 } catch ( Exception
$e ) {
68 // Error reading the iptc hash information.
69 // This probably means the App13 segment is something other than what we expect.
70 // However, still try to read it, and treat it as if the hash didn't exist.
71 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
72 $this->iptcType
= 'iptc-no-hash';
75 $iptc = IPTC
::parse( $app13 );
76 $this->addMetadata( $iptc, $this->iptcType
);
80 * Get exif info using exif class.
81 * Basically what used to be in BitmapHandler::getMetadata().
82 * Just calls stuff in the Exif class.
84 * Parameters are passed to the Exif class.
86 * @param string $filename
87 * @param string $byteOrder
89 function getExif( $filename, $byteOrder ) {
91 if ( file_exists( $filename ) && $wgShowEXIF ) {
92 $exif = new Exif( $filename, $byteOrder );
93 $data = $exif->getFilteredData();
95 $this->addMetadata( $data, 'exif' );
100 /** Add misc metadata. Warning: atm if the metadata category
101 * doesn't have a priority, it will be silently discarded.
103 * @param array $metaArray Array of metadata values
104 * @param string $type Type. defaults to other. if two things have the same type they're merged
106 function addMetadata( $metaArray, $type = 'other' ) {
107 if ( isset( $this->metadata
[$type] ) ) {
108 /* merge with old data */
109 $metaArray = $metaArray +
$this->metadata
[$type];
112 $this->metadata
[$type] = $metaArray;
116 * Merge together the various types of metadata
117 * the different types have different priorites,
118 * and are merged in order.
120 * This function is generally called by the media handlers' getMetadata()
122 * @return array Metadata array
124 function getMetadataArray() {
125 // this seems a bit ugly... This is all so its merged in right order
126 // based on the MWG recommendation.
128 krsort( $this->metaPriority
);
129 foreach ( $this->metaPriority
as $pri ) {
130 foreach ( $pri as $type ) {
131 if ( isset( $this->metadata
[$type] ) ) {
132 // Do some special casing for multilingual values.
133 // Don't discard translations if also as a simple value.
134 foreach ( $this->metadata
[$type] as $itemName => $item ) {
135 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' &&
136 isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
138 $default = $temp[$itemName];
139 $temp[$itemName] = $item;
140 $temp[$itemName]['x-default'] = $default;
141 unset( $this->metadata
[$type][$itemName] );
145 $temp = $temp +
$this->metadata
[$type];
153 /** Main entry point for jpeg's.
155 * @param string $filename Filename (with full path)
156 * @return array Metadata result array.
157 * @throws MWException On invalid file.
159 static function Jpeg( $filename ) {
160 $showXMP = XMPReader
::isSupported();
163 $seg = JpegMetadataExtractor
::segmentSplitter( $filename );
165 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
166 $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
168 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
169 foreach ( $seg['PSIR'] as $curPSIRValue ) {
170 $meta->doApp13( $curPSIRValue );
173 if ( isset( $seg['XMP'] ) && $showXMP ) {
174 $xmp = new XMPReader( LoggerFactory
::getInstance( 'XMP' ), $filename );
175 $xmp->parse( $seg['XMP'] );
176 foreach ( $seg['XMP_ext'] as $xmpExt ) {
177 /* Support for extended xmp in jpeg files
178 * is not well tested and a bit fragile.
180 $xmp->parseExtended( $xmpExt );
182 $res = $xmp->getResults();
183 foreach ( $res as $type => $array ) {
184 $meta->addMetadata( $array, $type );
188 $meta->getExif( $filename, $seg['byteOrder'] ??
'BE' );
190 return $meta->getMetadataArray();
193 /** Entry point for png
194 * At some point in the future this might
195 * merge the png various tEXt chunks to that
196 * are interesting, but for now it only does XMP
198 * @param string $filename Full path to file
199 * @return array Array for storage in img_metadata.
201 public static function PNG( $filename ) {
202 $showXMP = XMPReader
::isSupported();
205 $array = PNGMetadataExtractor
::getMetadata( $filename );
206 if ( isset( $array['text']['xmp']['x-default'] )
207 && $array['text']['xmp']['x-default'] !== '' && $showXMP
209 $xmp = new XMPReader( LoggerFactory
::getInstance( 'XMP' ), $filename );
210 $xmp->parse( $array['text']['xmp']['x-default'] );
211 $xmpRes = $xmp->getResults();
212 foreach ( $xmpRes as $type => $xmpSection ) {
213 $meta->addMetadata( $xmpSection, $type );
216 unset( $array['text']['xmp'] );
217 $meta->addMetadata( $array['text'], 'native' );
218 unset( $array['text'] );
219 $array['metadata'] = $meta->getMetadataArray();
220 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor
::VERSION
;
225 /** function for gif images.
227 * They don't really have native metadata, so just merges together
228 * XMP and image comment.
230 * @param string $filename Full path to file
231 * @return array Metadata array
233 public static function GIF( $filename ) {
235 $baseArray = GIFMetadataExtractor
::getMetadata( $filename );
237 if ( count( $baseArray['comment'] ) > 0 ) {
238 $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
241 if ( $baseArray['xmp'] !== '' && XMPReader
::isSupported() ) {
242 $xmp = new XMPReader( LoggerFactory
::getInstance( 'XMP' ), $filename );
243 $xmp->parse( $baseArray['xmp'] );
244 $xmpRes = $xmp->getResults();
245 foreach ( $xmpRes as $type => $xmpSection ) {
246 $meta->addMetadata( $xmpSection, $type );
250 unset( $baseArray['comment'] );
251 unset( $baseArray['xmp'] );
253 $baseArray['metadata'] = $meta->getMetadataArray();
254 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor
::VERSION
;
260 * This doesn't do much yet, but eventually I plan to add
261 * XMP support for Tiff. (PHP's exif support already extracts
262 * but needs some further processing because PHP's exif support
265 * @todo Add XMP support, so this function actually makes sense to put here.
267 * The various exceptions this throws are caught later.
268 * @param string $filename
269 * @throws MWException
270 * @return array The metadata.
272 public static function Tiff( $filename ) {
273 if ( file_exists( $filename ) ) {
274 $byteOrder = self
::getTiffByteOrder( $filename );
276 throw new MWException( "Error determining byte order of $filename" );
278 $exif = new Exif( $filename, $byteOrder );
279 $data = $exif->getFilteredData();
281 $data['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
285 throw new MWException( "Could not extract data from tiff file $filename" );
288 throw new MWException( "File doesn't exist - $filename" );
293 * Read the first 2 bytes of a tiff file to figure out
294 * Little Endian or Big Endian. Needed for exif stuff.
296 * @param string $filename
297 * @return string 'BE' or 'LE' or false
299 static function getTiffByteOrder( $filename ) {
300 $fh = fopen( $filename, 'rb' );
304 $head = fread( $fh, 2 );
309 return 'LE'; // II for intel.
311 return 'BE'; // MM for motorla.
313 return false; // Something went wrong.