TiffHandler.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Handler for Tiff 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 Tiff images.
  25. *
  26. * @ingroup Media
  27. */
  28. class TiffHandler extends ExifBitmapHandler {
  29. const EXPENSIVE_SIZE_LIMIT = 10485760; // TIFF files over 10M are considered expensive to thumbnail
  30. /**
  31. * Conversion to PNG for inline display can be disabled here...
  32. * Note scaling should work with ImageMagick, but may not with GD scaling.
  33. *
  34. * Files pulled from an another MediaWiki instance via ForeignAPIRepo /
  35. * InstantCommons will have thumbnails managed from the remote instance,
  36. * so we can skip this check.
  37. *
  38. * @param File $file
  39. * @return bool
  40. */
  41. public function canRender( $file ) {
  42. global $wgTiffThumbnailType;
  43. return (bool)$wgTiffThumbnailType
  44. || $file->getRepo() instanceof ForeignAPIRepo;
  45. }
  46. /**
  47. * Browsers don't support TIFF inline generally...
  48. * For inline display, we need to convert to PNG.
  49. *
  50. * @param File $file
  51. * @return bool
  52. */
  53. public function mustRender( $file ) {
  54. return true;
  55. }
  56. /**
  57. * @param string $ext
  58. * @param string $mime
  59. * @param array|null $params
  60. * @return array
  61. */
  62. public function getThumbType( $ext, $mime, $params = null ) {
  63. global $wgTiffThumbnailType;
  64. return $wgTiffThumbnailType;
  65. }
  66. /**
  67. * @param File|FSFile $image
  68. * @param string $filename
  69. * @throws MWException
  70. * @return string
  71. */
  72. public function getMetadata( $image, $filename ) {
  73. global $wgShowEXIF;
  74. if ( $wgShowEXIF ) {
  75. try {
  76. $meta = BitmapMetadataHandler::Tiff( $filename );
  77. if ( !is_array( $meta ) ) {
  78. // This should never happen, but doesn't hurt to be paranoid.
  79. throw new MWException( 'Metadata array is not an array' );
  80. }
  81. $meta['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
  82. return serialize( $meta );
  83. } catch ( Exception $e ) {
  84. // BitmapMetadataHandler throws an exception in certain exceptional
  85. // cases like if file does not exist.
  86. wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
  87. return ExifBitmapHandler::BROKEN_FILE;
  88. }
  89. } else {
  90. return '';
  91. }
  92. }
  93. public function isExpensiveToThumbnail( $file ) {
  94. return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
  95. }
  96. }