PageRenderTime 66ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/media/BitmapMetadataHandler.php

https://github.com/daevid/MWFork
PHP | 211 lines | 120 code | 21 blank | 70 comment | 20 complexity | 6cc092b2be0fae5f5065811c93a31227 MD5 | raw file
  1. <?php
  2. /**
  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
  5. This sort of acts as an intermediary between MediaHandler::getMetadata
  6. and the various metadata extractors.
  7. @todo other image formats.
  8. */
  9. class BitmapMetadataHandler {
  10. private $metadata = array();
  11. private $metaPriority = array(
  12. 20 => array( 'other' ),
  13. 40 => array( 'native' ),
  14. 60 => array( 'iptc-good-hash', 'iptc-no-hash' ),
  15. 70 => array( 'xmp-deprecated' ),
  16. 80 => array( 'xmp-general' ),
  17. 90 => array( 'xmp-exif' ),
  18. 100 => array( 'iptc-bad-hash' ),
  19. 120 => array( 'exif' ),
  20. );
  21. private $iptcType = 'iptc-no-hash';
  22. /**
  23. * This does the photoshop image resource app13 block
  24. * of interest, IPTC-IIM metadata is stored here.
  25. *
  26. * Mostly just calls doPSIR and doIPTC
  27. *
  28. * @param String $app13 String containing app13 block from jpeg file
  29. */
  30. private function doApp13 ( $app13 ) {
  31. $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
  32. $iptc = IPTC::parse( $app13 );
  33. $this->addMetadata( $iptc, $this->iptcType );
  34. }
  35. /**
  36. *
  37. * get exif info using exif class.
  38. * Basically what used to be in BitmapHandler::getMetadata().
  39. * Just calls stuff in the Exif class.
  40. *
  41. * @param $filename string
  42. */
  43. function getExif ( $filename ) {
  44. if ( file_exists( $filename ) ) {
  45. $exif = new Exif( $filename );
  46. $data = $exif->getFilteredData();
  47. if ( $data ) {
  48. $this->addMetadata( $data, 'exif' );
  49. }
  50. }
  51. }
  52. /** Add misc metadata. Warning: atm if the metadata category
  53. * doesn't have a priority, it will be silently discarded.
  54. *
  55. * @param Array $metaArray array of metadata values
  56. * @param string $type type. defaults to other. if two things have the same type they're merged
  57. */
  58. function addMetadata ( $metaArray, $type = 'other' ) {
  59. if ( isset( $this->metadata[$type] ) ) {
  60. /* merge with old data */
  61. $metaArray = $metaArray + $this->metadata[$type];
  62. }
  63. $this->metadata[$type] = $metaArray;
  64. }
  65. /**
  66. * Merge together the various types of metadata
  67. * the different types have different priorites,
  68. * and are merged in order.
  69. *
  70. * This function is generally called by the media handlers' getMetadata()
  71. *
  72. * @return Array metadata array
  73. */
  74. function getMetadataArray () {
  75. // this seems a bit ugly... This is all so its merged in right order
  76. // based on the MWG recomendation.
  77. $temp = Array();
  78. krsort( $this->metaPriority );
  79. foreach ( $this->metaPriority as $pri ) {
  80. foreach ( $pri as $type ) {
  81. if ( isset( $this->metadata[$type] ) ) {
  82. // Do some special casing for multilingual values.
  83. // Don't discard translations if also as a simple value.
  84. foreach ( $this->metadata[$type] as $itemName => $item ) {
  85. if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
  86. if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
  87. $default = $temp[$itemName];
  88. $temp[$itemName] = $item;
  89. $temp[$itemName]['x-default'] = $default;
  90. unset( $this->metadata[$type][$itemName] );
  91. }
  92. }
  93. }
  94. $temp = $temp + $this->metadata[$type];
  95. }
  96. }
  97. }
  98. return $temp;
  99. }
  100. /** Main entry point for jpeg's.
  101. *
  102. * @param $filename string filename (with full path)
  103. * @return metadata result array.
  104. * @throws MWException on invalid file.
  105. */
  106. static function Jpeg ( $filename ) {
  107. $showXMP = function_exists( 'xml_parser_create_ns' );
  108. $meta = new self();
  109. $meta->getExif( $filename );
  110. $seg = JpegMetadataExtractor::segmentSplitter( $filename );
  111. if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
  112. $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
  113. }
  114. if ( isset( $seg['PSIR'] ) ) {
  115. $meta->doApp13( $seg['PSIR'] );
  116. }
  117. if ( isset( $seg['XMP'] ) && $showXMP ) {
  118. $xmp = new XMPReader();
  119. $xmp->parse( $seg['XMP'] );
  120. foreach ( $seg['XMP_ext'] as $xmpExt ) {
  121. /* Support for extended xmp in jpeg files
  122. * is not well tested and a bit fragile.
  123. */
  124. $xmp->parseExtended( $xmpExt );
  125. }
  126. $res = $xmp->getResults();
  127. foreach ( $res as $type => $array ) {
  128. $meta->addMetadata( $array, $type );
  129. }
  130. }
  131. return $meta->getMetadataArray();
  132. }
  133. /** Entry point for png
  134. * At some point in the future this might
  135. * merge the png various tEXt chunks to that
  136. * are interesting, but for now it only does XMP
  137. *
  138. * @param $filename String full path to file
  139. * @return Array Array for storage in img_metadata.
  140. */
  141. static public function PNG ( $filename ) {
  142. $showXMP = function_exists( 'xml_parser_create_ns' );
  143. $meta = new self();
  144. $array = PNGMetadataExtractor::getMetadata( $filename );
  145. if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
  146. $xmp = new XMPReader();
  147. $xmp->parse( $array['text']['xmp']['x-default'] );
  148. $xmpRes = $xmp->getResults();
  149. foreach ( $xmpRes as $type => $xmpSection ) {
  150. $meta->addMetadata( $xmpSection, $type );
  151. }
  152. }
  153. unset( $array['text']['xmp'] );
  154. $meta->addMetadata( $array['text'], 'native' );
  155. unset( $array['text'] );
  156. $array['metadata'] = $meta->getMetadataArray();
  157. $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
  158. return $array;
  159. }
  160. /** function for gif images.
  161. *
  162. * They don't really have native metadata, so just merges together
  163. * XMP and image comment.
  164. *
  165. * @param $filename full path to file
  166. * @return Array metadata array
  167. */
  168. static public function GIF ( $filename ) {
  169. $meta = new self();
  170. $baseArray = GIFMetadataExtractor::getMetadata( $filename );
  171. if ( count( $baseArray['comment'] ) > 0 ) {
  172. $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
  173. }
  174. if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
  175. $xmp = new XMPReader();
  176. $xmp->parse( $baseArray['xmp'] );
  177. $xmpRes = $xmp->getResults();
  178. foreach ( $xmpRes as $type => $xmpSection ) {
  179. $meta->addMetadata( $xmpSection, $type );
  180. }
  181. }
  182. unset( $baseArray['comment'] );
  183. unset( $baseArray['xmp'] );
  184. $baseArray['metadata'] = $meta->getMetadataArray();
  185. $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
  186. return $baseArray;
  187. }
  188. }