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
;
27 * Class to deal with reconciling and extracting metadata from bitmap images.
28 * This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
30 * This sort of acts as an intermediary between MediaHandler::getMetadata
31 * and the various metadata extractors.
33 * @todo Other image formats.
36 class BitmapMetadataHandler
{
38 private $metadata = [];
40 /** @var array Metadata priority */
41 private $metaPriority = [
44 60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
45 70 => [ 'xmp-deprecated' ],
46 80 => [ 'xmp-general' ],
48 100 => [ 'iptc-bad-hash' ],
53 private $iptcType = 'iptc-no-hash';
56 * This does the photoshop image resource app13 block
57 * of interest, IPTC-IIM metadata is stored here.
59 * Mostly just calls doPSIR and doIPTC
61 * @param string $app13 String containing app13 block from jpeg file
63 private function doApp13( $app13 ) {
65 $this->iptcType
= JpegMetadataExtractor
::doPSIR( $app13 );
66 } catch ( Exception
$e ) {
67 // Error reading the iptc hash information.
68 // This probably means the App13 segment is something other than what we expect.
69 // However, still try to read it, and treat it as if the hash didn't exist.
70 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
71 $this->iptcType
= 'iptc-no-hash';
74 $iptc = IPTC
::parse( $app13 );
75 $this->addMetadata( $iptc, $this->iptcType
);
79 * Get exif info using exif class.
80 * Basically what used to be in BitmapHandler::getMetadata().
81 * Just calls stuff in the Exif class.
83 * Parameters are passed to the Exif class.
85 * @param string $filename
86 * @param string $byteOrder
88 function getExif( $filename, $byteOrder ) {
90 if ( file_exists( $filename ) && $wgShowEXIF ) {
91 $exif = new Exif( $filename, $byteOrder );
92 $data = $exif->getFilteredData();
94 $this->addMetadata( $data, 'exif' );
99 /** Add misc metadata. Warning: atm if the metadata category
100 * doesn't have a priority, it will be silently discarded.
102 * @param array $metaArray Array of metadata values
103 * @param string $type Type. defaults to other. if two things have the same type they're merged
105 function addMetadata( $metaArray, $type = 'other' ) {
106 if ( isset( $this->metadata
[$type] ) ) {
107 /* merge with old data */
108 $metaArray = $metaArray +
$this->metadata
[$type];
111 $this->metadata
[$type] = $metaArray;
115 * Merge together the various types of metadata
116 * the different types have different priorites,
117 * and are merged in order.
119 * This function is generally called by the media handlers' getMetadata()
121 * @return array Metadata array
123 function getMetadataArray() {
124 // this seems a bit ugly... This is all so its merged in right order
125 // based on the MWG recomendation.
127 krsort( $this->metaPriority
);
128 foreach ( $this->metaPriority
as $pri ) {
129 foreach ( $pri as $type ) {
130 if ( isset( $this->metadata
[$type] ) ) {
131 // Do some special casing for multilingual values.
132 // Don't discard translations if also as a simple value.
133 foreach ( $this->metadata
[$type] as $itemName => $item ) {
134 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
135 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
136 $default = $temp[$itemName];
137 $temp[$itemName] = $item;
138 $temp[$itemName]['x-default'] = $default;
139 unset( $this->metadata
[$type][$itemName] );
144 $temp = $temp +
$this->metadata
[$type];
152 /** Main entry point for jpeg's.
154 * @param string $filename Filename (with full path)
155 * @return array Metadata result array.
156 * @throws MWException On invalid file.
158 static function Jpeg( $filename ) {
159 $showXMP = XMPReader
::isSupported();
162 $seg = JpegMetadataExtractor
::segmentSplitter( $filename );
164 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
165 $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
167 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
168 foreach ( $seg['PSIR'] as $curPSIRValue ) {
169 $meta->doApp13( $curPSIRValue );
172 if ( isset( $seg['XMP'] ) && $showXMP ) {
173 $xmp = new XMPReader( LoggerFactory
::getInstance( 'XMP' ) );
174 $xmp->parse( $seg['XMP'] );
175 foreach ( $seg['XMP_ext'] as $xmpExt ) {
176 /* Support for extended xmp in jpeg files
177 * is not well tested and a bit fragile.
179 $xmp->parseExtended( $xmpExt );
181 $res = $xmp->getResults();
182 foreach ( $res as $type => $array ) {
183 $meta->addMetadata( $array, $type );
187 $meta->getExif( $filename, isset( $seg['byteOrder'] ) ?
$seg['byteOrder'] : 'BE' );
189 return $meta->getMetadataArray();
192 /** Entry point for png
193 * At some point in the future this might
194 * merge the png various tEXt chunks to that
195 * are interesting, but for now it only does XMP
197 * @param string $filename Full path to file
198 * @return array Array for storage in img_metadata.
200 public static function PNG( $filename ) {
201 $showXMP = XMPReader
::isSupported();
204 $array = PNGMetadataExtractor
::getMetadata( $filename );
205 if ( isset( $array['text']['xmp']['x-default'] )
206 && $array['text']['xmp']['x-default'] !== '' && $showXMP
208 $xmp = new XMPReader( LoggerFactory
::getInstance( 'XMP' ) );
209 $xmp->parse( $array['text']['xmp']['x-default'] );
210 $xmpRes = $xmp->getResults();
211 foreach ( $xmpRes as $type => $xmpSection ) {
212 $meta->addMetadata( $xmpSection, $type );
215 unset( $array['text']['xmp'] );
216 $meta->addMetadata( $array['text'], 'native' );
217 unset( $array['text'] );
218 $array['metadata'] = $meta->getMetadataArray();
219 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor
::VERSION
;
224 /** function for gif images.
226 * They don't really have native metadata, so just merges together
227 * XMP and image comment.
229 * @param string $filename Full path to file
230 * @return array Metadata array
232 public static function GIF( $filename ) {
234 $baseArray = GIFMetadataExtractor
::getMetadata( $filename );
236 if ( count( $baseArray['comment'] ) > 0 ) {
237 $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
240 if ( $baseArray['xmp'] !== '' && XMPReader
::isSupported() ) {
241 $xmp = new XMPReader( LoggerFactory
::getInstance( 'XMP' ) );
242 $xmp->parse( $baseArray['xmp'] );
243 $xmpRes = $xmp->getResults();
244 foreach ( $xmpRes as $type => $xmpSection ) {
245 $meta->addMetadata( $xmpSection, $type );
249 unset( $baseArray['comment'] );
250 unset( $baseArray['xmp'] );
252 $baseArray['metadata'] = $meta->getMetadataArray();
253 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor
::VERSION
;
259 * This doesn't do much yet, but eventually I plan to add
260 * XMP support for Tiff. (PHP's exif support already extracts
261 * but needs some further processing because PHP's exif support
264 * @todo Add XMP support, so this function actually makes sense to put here.
266 * The various exceptions this throws are caught later.
267 * @param string $filename
268 * @throws MWException
269 * @return array The metadata.
271 public static function Tiff( $filename ) {
272 if ( file_exists( $filename ) ) {
273 $byteOrder = self
::getTiffByteOrder( $filename );
275 throw new MWException( "Error determining byte order of $filename" );
277 $exif = new Exif( $filename, $byteOrder );
278 $data = $exif->getFilteredData();
280 $data['MEDIAWIKI_EXIF_VERSION'] = Exif
::version();
284 throw new MWException( "Could not extract data from tiff file $filename" );
287 throw new MWException( "File doesn't exist - $filename" );
292 * Read the first 2 bytes of a tiff file to figure out
293 * Little Endian or Big Endian. Needed for exif stuff.
295 * @param string $filename The filename
296 * @return string 'BE' or 'LE' or false
298 static function getTiffByteOrder( $filename ) {
299 $fh = fopen( $filename, 'rb' );
303 $head = fread( $fh, 2 );
308 return 'LE'; // II for intel.
310 return 'BE'; // MM for motorla.
312 return false; // Something went wrong.