13188ecf4830182967f5a536cfb986ed21859edc
3 Class to deal with reconciling and extracting metadata from bitmap images.
4 This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
6 This sort of acts as an intermediary between MediaHandler::getMetadata
7 and the various metadata extractors.
9 @todo other image formats.
11 class BitmapMetadataHandler
{
13 private $metadata = array();
14 private $metaPriority = array(
15 20 => array( 'other' ),
16 40 => array( 'native' ),
17 60 => array( 'iptc-good-hash', 'iptc-no-hash' ),
18 70 => array( 'xmp-deprecated' ),
19 80 => array( 'xmp-general' ),
20 90 => array( 'xmp-exif' ),
21 100 => array( 'iptc-bad-hash' ),
22 120 => array( 'exif' ),
24 private $iptcType = 'iptc-no-hash';
27 * This does the photoshop image resource app13 block
28 * of interest, IPTC-IIM metadata is stored here.
30 * Mostly just calls doPSIR and doIPTC
32 * @param String $app13 String containing app13 block from jpeg file
34 private function doApp13 ( $app13 ) {
35 $this->iptcType
= JpegMetadataExtractor
::doPSIR( $app13 );
37 $iptc = IPTC
::parse( $app13 );
38 $this->addMetadata( $iptc, $this->iptcType
);
44 * get exif info using exif class.
45 * Basically what used to be in BitmapHandler::getMetadata().
46 * Just calls stuff in the Exif class.
48 * @param $filename string
50 function getExif ( $filename ) {
51 if ( file_exists( $filename ) ) {
52 $exif = new Exif( $filename );
53 $data = $exif->getFilteredData();
55 $this->addMetadata( $data, 'exif' );
59 /** Add misc metadata. Warning: atm if the metadata category
60 * doesn't have a priority, it will be silently discarded.
62 * @param Array $metaArray array of metadata values
63 * @param string $type type. defaults to other. if two things have the same type they're merged
65 function addMetadata ( $metaArray, $type = 'other' ) {
66 if ( isset( $this->metadata
[$type] ) ) {
67 /* merge with old data */
68 $metaArray = $metaArray +
$this->metadata
[$type];
71 $this->metadata
[$type] = $metaArray;
75 * Merge together the various types of metadata
76 * the different types have different priorites,
77 * and are merged in order.
79 * This function is generally called by the media handlers' getMetadata()
81 * @return Array metadata array
83 function getMetadataArray () {
84 // this seems a bit ugly... This is all so its merged in right order
85 // based on the MWG recomendation.
87 krsort( $this->metaPriority
);
88 foreach ( $this->metaPriority
as $pri ) {
89 foreach ( $pri as $type ) {
90 if ( isset( $this->metadata
[$type] ) ) {
91 // Do some special casing for multilingual values.
92 // Don't discard translations if also as a simple value.
93 foreach ( $this->metadata
[$type] as $itemName => $item ) {
94 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
95 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
96 $default = $temp[$itemName];
97 $temp[$itemName] = $item;
98 $temp[$itemName]['x-default'] = $default;
99 unset( $this->metadata
[$type][$itemName] );
104 $temp = $temp +
$this->metadata
[$type];
111 /** Main entry point for jpeg's.
113 * @param $filename string filename (with full path)
114 * @return metadata result array.
115 * @throws MWException on invalid file.
117 static function Jpeg ( $filename ) {
118 $showXMP = function_exists( 'xml_parser_create_ns' );
120 $meta->getExif( $filename );
122 $seg = JpegMetadataExtractor
::segmentSplitter( $filename );
123 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
124 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
126 if ( isset( $seg['PSIR'] ) ) {
127 $meta->doApp13( $seg['PSIR'] );
129 if ( isset( $seg['XMP'] ) && $showXMP ) {
130 $xmp = new XMPReader();
131 $xmp->parse( $seg['XMP'] );
132 foreach ( $seg['XMP_ext'] as $xmpExt ) {
133 /* Support for extended xmp in jpeg files
134 * is not well tested and a bit fragile.
136 $xmp->parseExtended( $xmpExt );
139 $res = $xmp->getResults();
140 foreach ( $res as $type => $array ) {
141 $meta->addMetadata( $array, $type );
144 return $meta->getMetadataArray();
147 /** Entry point for png
148 * At some point in the future this might
149 * merge the png various tEXt chunks to that
150 * are interesting, but for now it only does XMP
152 * @param $filename String full path to file
153 * @return Array Array for storage in img_metadata.
155 static public function PNG ( $filename ) {
156 $showXMP = function_exists( 'xml_parser_create_ns' );
159 $array = PNGMetadataExtractor
::getMetadata( $filename );
160 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
161 $xmp = new XMPReader();
162 $xmp->parse( $array['text']['xmp']['x-default'] );
163 $xmpRes = $xmp->getResults();
164 foreach ( $xmpRes as $type => $xmpSection ) {
165 $meta->addMetadata( $xmpSection, $type );
168 unset( $array['text']['xmp'] );
169 $meta->addMetadata( $array['text'], 'native' );
170 unset( $array['text'] );
171 $array['metadata'] = $meta->getMetadataArray();
172 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor
::VERSION
;
176 /** function for gif images.
178 * They don't really have native metadata, so just merges together
179 * XMP and image comment.
181 * @param $filename full path to file
182 * @return Array metadata array
184 static public function GIF ( $filename ) {
187 $baseArray = GIFMetadataExtractor
::getMetadata( $filename );
189 if ( count( $baseArray['comment'] ) > 0 ) {
190 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
193 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
194 $xmp = new XMPReader();
195 $xmp->parse( $baseArray['xmp'] );
196 $xmpRes = $xmp->getResults();
197 foreach ( $xmpRes as $type => $xmpSection ) {
198 $meta->addMetadata( $xmpSection, $type );
203 unset( $baseArray['comment'] );
204 unset( $baseArray['xmp'] );
206 $baseArray['metadata'] = $meta->getMetadataArray();
207 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor
::VERSION
;