ImageHandler.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Media-handling base classes and generic functionality.
  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. * Media handler abstract base class for images
  25. *
  26. * @ingroup Media
  27. */
  28. abstract class ImageHandler extends MediaHandler {
  29. /**
  30. * @param File $file
  31. * @return bool
  32. */
  33. public function canRender( $file ) {
  34. return ( $file->getWidth() && $file->getHeight() );
  35. }
  36. public function getParamMap() {
  37. return [ 'img_width' => 'width' ];
  38. }
  39. public function validateParam( $name, $value ) {
  40. if ( in_array( $name, [ 'width', 'height' ] ) ) {
  41. if ( $value <= 0 ) {
  42. return false;
  43. } else {
  44. return true;
  45. }
  46. } else {
  47. return false;
  48. }
  49. }
  50. public function makeParamString( $params ) {
  51. if ( isset( $params['physicalWidth'] ) ) {
  52. $width = $params['physicalWidth'];
  53. } elseif ( isset( $params['width'] ) ) {
  54. $width = $params['width'];
  55. } else {
  56. throw new MediaTransformInvalidParametersException( 'No width specified to ' . __METHOD__ );
  57. }
  58. # Removed for ProofreadPage
  59. # $width = intval( $width );
  60. return "{$width}px";
  61. }
  62. public function parseParamString( $str ) {
  63. $m = false;
  64. if ( preg_match( '/^(\d+)px$/', $str, $m ) ) {
  65. return [ 'width' => $m[1] ];
  66. } else {
  67. return false;
  68. }
  69. }
  70. protected function getScriptParams( $params ) {
  71. return [ 'width' => $params['width'] ];
  72. }
  73. /**
  74. * @param File $image
  75. * @param array &$params
  76. * @return bool
  77. */
  78. public function normaliseParams( $image, &$params ) {
  79. $mimeType = $image->getMimeType();
  80. if ( !isset( $params['width'] ) ) {
  81. return false;
  82. }
  83. if ( !isset( $params['page'] ) ) {
  84. $params['page'] = 1;
  85. } else {
  86. $params['page'] = intval( $params['page'] );
  87. if ( $params['page'] > $image->pageCount() ) {
  88. $params['page'] = $image->pageCount();
  89. }
  90. if ( $params['page'] < 1 ) {
  91. $params['page'] = 1;
  92. }
  93. }
  94. $srcWidth = $image->getWidth( $params['page'] );
  95. $srcHeight = $image->getHeight( $params['page'] );
  96. if ( isset( $params['height'] ) && $params['height'] != -1 ) {
  97. # Height & width were both set
  98. if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
  99. # Height is the relative smaller dimension, so scale width accordingly
  100. $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
  101. if ( $params['width'] == 0 ) {
  102. # Very small image, so we need to rely on client side scaling :(
  103. $params['width'] = 1;
  104. }
  105. $params['physicalWidth'] = $params['width'];
  106. } else {
  107. # Height was crap, unset it so that it will be calculated later
  108. unset( $params['height'] );
  109. }
  110. }
  111. if ( !isset( $params['physicalWidth'] ) ) {
  112. # Passed all validations, so set the physicalWidth
  113. $params['physicalWidth'] = $params['width'];
  114. }
  115. # Because thumbs are only referred to by width, the height always needs
  116. # to be scaled by the width to keep the thumbnail sizes consistent,
  117. # even if it was set inside the if block above
  118. $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight,
  119. $params['physicalWidth'] );
  120. # Set the height if it was not validated in the if block higher up
  121. if ( !isset( $params['height'] ) || $params['height'] == -1 ) {
  122. $params['height'] = $params['physicalHeight'];
  123. }
  124. if ( !$this->validateThumbParams( $params['physicalWidth'],
  125. $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType )
  126. ) {
  127. return false;
  128. }
  129. return true;
  130. }
  131. /**
  132. * Validate thumbnail parameters and fill in the correct height
  133. *
  134. * @param int &$width Specified width (input/output)
  135. * @param int &$height Height (output only)
  136. * @param int $srcWidth Width of the source image
  137. * @param int $srcHeight Height of the source image
  138. * @param string $mimeType Unused
  139. * @return bool False to indicate that an error should be returned to the user.
  140. */
  141. function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) {
  142. $width = intval( $width );
  143. # Sanity check $width
  144. if ( $width <= 0 ) {
  145. wfDebug( __METHOD__ . ": Invalid destination width: $width\n" );
  146. return false;
  147. }
  148. if ( $srcWidth <= 0 ) {
  149. wfDebug( __METHOD__ . ": Invalid source width: $srcWidth\n" );
  150. return false;
  151. }
  152. $height = File::scaleHeight( $srcWidth, $srcHeight, $width );
  153. if ( $height == 0 ) {
  154. # Force height to be at least 1 pixel
  155. $height = 1;
  156. }
  157. return true;
  158. }
  159. /**
  160. * @param File $image
  161. * @param string $script
  162. * @param array $params
  163. * @return bool|MediaTransformOutput
  164. */
  165. function getScriptedTransform( $image, $script, $params ) {
  166. if ( !$this->normaliseParams( $image, $params ) ) {
  167. return false;
  168. }
  169. $url = wfAppendQuery( $script, $this->getScriptParams( $params ) );
  170. if ( $image->mustRender() || $params['width'] < $image->getWidth() ) {
  171. return new ThumbnailImage( $image, $url, false, $params );
  172. }
  173. }
  174. function getImageSize( $image, $path ) {
  175. Wikimedia\suppressWarnings();
  176. $gis = getimagesize( $path );
  177. Wikimedia\restoreWarnings();
  178. return $gis;
  179. }
  180. /**
  181. * Function that returns the number of pixels to be thumbnailed.
  182. * Intended for animated GIFs to multiply by the number of frames.
  183. *
  184. * If the file doesn't support a notion of "area" return 0.
  185. *
  186. * @param File $image
  187. * @return int
  188. */
  189. function getImageArea( $image ) {
  190. return $image->getWidth() * $image->getHeight();
  191. }
  192. /**
  193. * @param File $file
  194. * @return string
  195. */
  196. function getShortDesc( $file ) {
  197. global $wgLang;
  198. $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
  199. $widthheight = wfMessage( 'widthheight' )
  200. ->numParams( $file->getWidth(), $file->getHeight() )->escaped();
  201. return "$widthheight ($nbytes)";
  202. }
  203. /**
  204. * @param File $file
  205. * @return string
  206. */
  207. public function getLongDesc( $file ) {
  208. global $wgLang;
  209. $pages = $file->pageCount();
  210. $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
  211. if ( $pages === false || $pages <= 1 ) {
  212. $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(),
  213. $file->getHeight() )->params( $size,
  214. '<span class="mime-type">' . $file->getMimeType() . '</span>' )->parse();
  215. } else {
  216. $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(),
  217. $file->getHeight() )->params( $size,
  218. '<span class="mime-type">' . $file->getMimeType() . '</span>' )->numParams( $pages )->parse();
  219. }
  220. return $msg;
  221. }
  222. /**
  223. * @param File $file
  224. * @return string
  225. */
  226. function getDimensionsString( $file ) {
  227. $pages = $file->pageCount();
  228. if ( $pages > 1 ) {
  229. return wfMessage( 'widthheightpage' )
  230. ->numParams( $file->getWidth(), $file->getHeight(), $pages )->text();
  231. } else {
  232. return wfMessage( 'widthheight' )
  233. ->numParams( $file->getWidth(), $file->getHeight() )->text();
  234. }
  235. }
  236. public function sanitizeParamsForBucketing( $params ) {
  237. $params = parent::sanitizeParamsForBucketing( $params );
  238. // We unset the height parameters in order to let normaliseParams recalculate them
  239. // Otherwise there might be a height discrepancy
  240. if ( isset( $params['height'] ) ) {
  241. unset( $params['height'] );
  242. }
  243. if ( isset( $params['physicalHeight'] ) ) {
  244. unset( $params['physicalHeight'] );
  245. }
  246. return $params;
  247. }
  248. }