GIF.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Handler for GIF images.
  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. * Handler for GIF images.
  25. *
  26. * @ingroup Media
  27. */
  28. class GIFHandler extends BitmapHandler {
  29. const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
  30. function getMetadata( $image, $filename ) {
  31. try {
  32. $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
  33. } catch ( Exception $e ) {
  34. // Broken file?
  35. wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
  36. return self::BROKEN_FILE;
  37. }
  38. return serialize( $parsedGIFMetadata );
  39. }
  40. /**
  41. * @param File $image
  42. * @param bool|IContextSource $context Context to use (optional)
  43. * @return array|bool
  44. */
  45. function formatMetadata( $image, $context = false ) {
  46. $meta = $this->getCommonMetaArray( $image );
  47. if ( count( $meta ) === 0 ) {
  48. return false;
  49. }
  50. return $this->formatMetadataHelper( $meta, $context );
  51. }
  52. /**
  53. * Return the standard metadata elements for #filemetadata parser func.
  54. * @param File $image
  55. * @return array|bool
  56. */
  57. public function getCommonMetaArray( File $image ) {
  58. $meta = $image->getMetadata();
  59. if ( !$meta ) {
  60. return [];
  61. }
  62. $meta = unserialize( $meta );
  63. if ( !isset( $meta['metadata'] ) ) {
  64. return [];
  65. }
  66. unset( $meta['metadata']['_MW_GIF_VERSION'] );
  67. return $meta['metadata'];
  68. }
  69. /**
  70. * @todo Add unit tests
  71. *
  72. * @param File $image
  73. * @return bool
  74. */
  75. function getImageArea( $image ) {
  76. $ser = $image->getMetadata();
  77. if ( $ser ) {
  78. $metadata = unserialize( $ser );
  79. return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
  80. } else {
  81. return $image->getWidth() * $image->getHeight();
  82. }
  83. }
  84. /**
  85. * @param File $image
  86. * @return bool
  87. */
  88. function isAnimatedImage( $image ) {
  89. $ser = $image->getMetadata();
  90. if ( $ser ) {
  91. $metadata = unserialize( $ser );
  92. if ( $metadata['frameCount'] > 1 ) {
  93. return true;
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * We cannot animate thumbnails that are bigger than a particular size
  100. * @param File $file
  101. * @return bool
  102. */
  103. function canAnimateThumbnail( $file ) {
  104. global $wgMaxAnimatedGifArea;
  105. $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
  106. return $answer;
  107. }
  108. function getMetadataType( $image ) {
  109. return 'parsed-gif';
  110. }
  111. function isMetadataValid( $image, $metadata ) {
  112. if ( $metadata === self::BROKEN_FILE ) {
  113. // Do not repetitivly regenerate metadata on broken file.
  114. return self::METADATA_GOOD;
  115. }
  116. Wikimedia\suppressWarnings();
  117. $data = unserialize( $metadata );
  118. Wikimedia\restoreWarnings();
  119. if ( !$data || !is_array( $data ) ) {
  120. wfDebug( __METHOD__ . " invalid GIF metadata\n" );
  121. return self::METADATA_BAD;
  122. }
  123. if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
  124. || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
  125. ) {
  126. wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
  127. return self::METADATA_COMPATIBLE;
  128. }
  129. return self::METADATA_GOOD;
  130. }
  131. /**
  132. * @param File $image
  133. * @return string
  134. */
  135. function getLongDesc( $image ) {
  136. global $wgLang;
  137. $original = parent::getLongDesc( $image );
  138. Wikimedia\suppressWarnings();
  139. $metadata = unserialize( $image->getMetadata() );
  140. Wikimedia\restoreWarnings();
  141. if ( !$metadata || $metadata['frameCount'] <= 1 ) {
  142. return $original;
  143. }
  144. /* Preserve original image info string, but strip the last char ')' so we can add even more */
  145. $info = [];
  146. $info[] = $original;
  147. if ( $metadata['looped'] ) {
  148. $info[] = wfMessage( 'file-info-gif-looped' )->parse();
  149. }
  150. if ( $metadata['frameCount'] > 1 ) {
  151. $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
  152. }
  153. if ( $metadata['duration'] ) {
  154. $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
  155. }
  156. return $wgLang->commaList( $info );
  157. }
  158. /**
  159. * Return the duration of the GIF file.
  160. *
  161. * Shown in the &query=imageinfo&iiprop=size api query.
  162. *
  163. * @param File $file
  164. * @return float The duration of the file.
  165. */
  166. public function getLength( $file ) {
  167. $serMeta = $file->getMetadata();
  168. Wikimedia\suppressWarnings();
  169. $metadata = unserialize( $serMeta );
  170. Wikimedia\restoreWarnings();
  171. if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
  172. return 0.0;
  173. } else {
  174. return (float)$metadata['duration'];
  175. }
  176. }
  177. }