ExifBitmapHandler.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Handler for bitmap images with exif metadata.
  4. *
  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.
  9. *
  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.
  14. *
  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
  19. *
  20. * @file
  21. * @ingroup Media
  22. */
  23. /**
  24. * Stuff specific to JPEG and (built-in) TIFF handler.
  25. * All metadata related, since both JPEG and TIFF support Exif.
  26. *
  27. * @ingroup Media
  28. */
  29. class ExifBitmapHandler extends BitmapHandler {
  30. const BROKEN_FILE = '-1'; // error extracting metadata
  31. const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
  32. function convertMetadataVersion( $metadata, $version = 1 ) {
  33. // basically flattens arrays.
  34. $version = intval( explode( ';', $version, 2 )[0] );
  35. if ( $version < 1 || $version >= 2 ) {
  36. return $metadata;
  37. }
  38. $avoidHtml = true;
  39. if ( !is_array( $metadata ) ) {
  40. $metadata = unserialize( $metadata );
  41. }
  42. if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
  43. return $metadata;
  44. }
  45. // Treat Software as a special case because in can contain
  46. // an array of (SoftwareName, Version).
  47. if ( isset( $metadata['Software'] )
  48. && is_array( $metadata['Software'] )
  49. && is_array( $metadata['Software'][0] )
  50. && isset( $metadata['Software'][0][0] )
  51. && isset( $metadata['Software'][0][1] )
  52. ) {
  53. $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
  54. . $metadata['Software'][0][1] . ')';
  55. }
  56. $formatter = new FormatMetadata;
  57. // ContactInfo also has to be dealt with specially
  58. if ( isset( $metadata['Contact'] ) ) {
  59. $metadata['Contact'] =
  60. $formatter->collapseContactInfo(
  61. $metadata['Contact'] );
  62. }
  63. foreach ( $metadata as &$val ) {
  64. if ( is_array( $val ) ) {
  65. $val = $formatter->flattenArrayReal( $val, 'ul', $avoidHtml );
  66. }
  67. }
  68. $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
  69. return $metadata;
  70. }
  71. /**
  72. * @param File $image
  73. * @param string $metadata
  74. * @return bool|int
  75. */
  76. public function isMetadataValid( $image, $metadata ) {
  77. global $wgShowEXIF;
  78. if ( !$wgShowEXIF ) {
  79. # Metadata disabled and so an empty field is expected
  80. return self::METADATA_GOOD;
  81. }
  82. if ( $metadata === self::OLD_BROKEN_FILE ) {
  83. # Old special value indicating that there is no Exif data in the file.
  84. # or that there was an error well extracting the metadata.
  85. wfDebug( __METHOD__ . ": back-compat version\n" );
  86. return self::METADATA_COMPATIBLE;
  87. }
  88. if ( $metadata === self::BROKEN_FILE ) {
  89. return self::METADATA_GOOD;
  90. }
  91. Wikimedia\suppressWarnings();
  92. $exif = unserialize( $metadata );
  93. Wikimedia\restoreWarnings();
  94. if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
  95. || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
  96. ) {
  97. if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
  98. && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
  99. ) {
  100. // back-compatible but old
  101. wfDebug( __METHOD__ . ": back-compat version\n" );
  102. return self::METADATA_COMPATIBLE;
  103. }
  104. # Wrong (non-compatible) version
  105. wfDebug( __METHOD__ . ": wrong version\n" );
  106. return self::METADATA_BAD;
  107. }
  108. return self::METADATA_GOOD;
  109. }
  110. /**
  111. * @param File $image
  112. * @param bool|IContextSource $context Context to use (optional)
  113. * @return array|bool
  114. */
  115. public function formatMetadata( $image, $context = false ) {
  116. $meta = $this->getCommonMetaArray( $image );
  117. if ( count( $meta ) === 0 ) {
  118. return false;
  119. }
  120. return $this->formatMetadataHelper( $meta, $context );
  121. }
  122. public function getCommonMetaArray( File $file ) {
  123. $metadata = $file->getMetadata();
  124. if ( $metadata === self::OLD_BROKEN_FILE
  125. || $metadata === self::BROKEN_FILE
  126. || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD
  127. ) {
  128. // So we don't try and display metadata from PagedTiffHandler
  129. // for example when using InstantCommons.
  130. return [];
  131. }
  132. $exif = unserialize( $metadata );
  133. if ( !$exif ) {
  134. return [];
  135. }
  136. unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
  137. return $exif;
  138. }
  139. function getMetadataType( $image ) {
  140. return 'exif';
  141. }
  142. /**
  143. * Wrapper for base classes ImageHandler::getImageSize() that checks for
  144. * rotation reported from metadata and swaps the sizes to match.
  145. *
  146. * @param File|FSFile $image
  147. * @param string $path
  148. * @return array|false
  149. */
  150. function getImageSize( $image, $path ) {
  151. $gis = parent::getImageSize( $image, $path );
  152. // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
  153. // This may mean we read EXIF data twice on initial upload.
  154. if ( $this->autoRotateEnabled() ) {
  155. $meta = $this->getMetadata( $image, $path );
  156. $rotation = $this->getRotationForExif( $meta );
  157. } else {
  158. $rotation = 0;
  159. }
  160. if ( $rotation == 90 || $rotation == 270 ) {
  161. $width = $gis[0];
  162. $gis[0] = $gis[1];
  163. $gis[1] = $width;
  164. }
  165. return $gis;
  166. }
  167. /**
  168. * On supporting image formats, try to read out the low-level orientation
  169. * of the file and return the angle that the file needs to be rotated to
  170. * be viewed.
  171. *
  172. * This information is only useful when manipulating the original file;
  173. * the width and height we normally work with is logical, and will match
  174. * any produced output views.
  175. *
  176. * @param File $file
  177. * @return int 0, 90, 180 or 270
  178. */
  179. public function getRotation( $file ) {
  180. if ( !$this->autoRotateEnabled() ) {
  181. return 0;
  182. }
  183. $data = $file->getMetadata();
  184. return $this->getRotationForExif( $data );
  185. }
  186. /**
  187. * Given a chunk of serialized Exif metadata, return the orientation as
  188. * degrees of rotation.
  189. *
  190. * @param string $data
  191. * @return int 0, 90, 180 or 270
  192. * @todo FIXME: Orientation can include flipping as well; see if this is an issue!
  193. */
  194. protected function getRotationForExif( $data ) {
  195. if ( !$data ) {
  196. return 0;
  197. }
  198. Wikimedia\suppressWarnings();
  199. $data = unserialize( $data );
  200. Wikimedia\restoreWarnings();
  201. if ( isset( $data['Orientation'] ) ) {
  202. # See http://sylvana.net/jpegcrop/exif_orientation.html
  203. switch ( $data['Orientation'] ) {
  204. case 8:
  205. return 90;
  206. case 3:
  207. return 180;
  208. case 6:
  209. return 270;
  210. default:
  211. return 0;
  212. }
  213. }
  214. return 0;
  215. }
  216. }