GIFHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. public 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. public 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. if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
  80. return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
  81. } else {
  82. return $image->getWidth() * $image->getHeight();
  83. }
  84. } else {
  85. return $image->getWidth() * $image->getHeight();
  86. }
  87. }
  88. /**
  89. * @param File $image
  90. * @return bool
  91. */
  92. function isAnimatedImage( $image ) {
  93. $ser = $image->getMetadata();
  94. if ( $ser ) {
  95. $metadata = unserialize( $ser );
  96. if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
  97. return true;
  98. }
  99. }
  100. return false;
  101. }
  102. /**
  103. * We cannot animate thumbnails that are bigger than a particular size
  104. * @param File $file
  105. * @return bool
  106. */
  107. function canAnimateThumbnail( $file ) {
  108. global $wgMaxAnimatedGifArea;
  109. $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
  110. return $answer;
  111. }
  112. function getMetadataType( $image ) {
  113. return 'parsed-gif';
  114. }
  115. public function isMetadataValid( $image, $metadata ) {
  116. if ( $metadata === self::BROKEN_FILE ) {
  117. // Do not repetitivly regenerate metadata on broken file.
  118. return self::METADATA_GOOD;
  119. }
  120. Wikimedia\suppressWarnings();
  121. $data = unserialize( $metadata );
  122. Wikimedia\restoreWarnings();
  123. if ( !$data || !is_array( $data ) ) {
  124. wfDebug( __METHOD__ . " invalid GIF metadata\n" );
  125. return self::METADATA_BAD;
  126. }
  127. if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
  128. || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
  129. ) {
  130. wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
  131. return self::METADATA_COMPATIBLE;
  132. }
  133. return self::METADATA_GOOD;
  134. }
  135. /**
  136. * @param File $image
  137. * @return string
  138. */
  139. public function getLongDesc( $image ) {
  140. global $wgLang;
  141. $original = parent::getLongDesc( $image );
  142. Wikimedia\suppressWarnings();
  143. $metadata = unserialize( $image->getMetadata() );
  144. Wikimedia\restoreWarnings();
  145. if ( !$metadata || $metadata['frameCount'] <= 1 ) {
  146. return $original;
  147. }
  148. /* Preserve original image info string, but strip the last char ')' so we can add even more */
  149. $info = [];
  150. $info[] = $original;
  151. if ( $metadata['looped'] ) {
  152. $info[] = wfMessage( 'file-info-gif-looped' )->parse();
  153. }
  154. if ( $metadata['frameCount'] > 1 ) {
  155. $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
  156. }
  157. if ( $metadata['duration'] ) {
  158. $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
  159. }
  160. return $wgLang->commaList( $info );
  161. }
  162. /**
  163. * Return the duration of the GIF file.
  164. *
  165. * Shown in the &query=imageinfo&iiprop=size api query.
  166. *
  167. * @param File $file
  168. * @return float The duration of the file.
  169. */
  170. public function getLength( $file ) {
  171. $serMeta = $file->getMetadata();
  172. Wikimedia\suppressWarnings();
  173. $metadata = unserialize( $serMeta );
  174. Wikimedia\restoreWarnings();
  175. if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
  176. return 0.0;
  177. } else {
  178. return (float)$metadata['duration'];
  179. }
  180. }
  181. }