| 1 | <?php |
| 2 | /** |
| 3 | * Handler for Microsoft's bitmap format. |
| 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 | use MediaWiki\Libs\UnpackFailedException; |
| 25 | |
| 26 | /** |
| 27 | * Handler for Microsoft's bitmap format; getimagesize() doesn't |
| 28 | * support these files |
| 29 | * |
| 30 | * @ingroup Media |
| 31 | */ |
| 32 | class BmpHandler extends BitmapHandler { |
| 33 | /** |
| 34 | * @param File $file |
| 35 | * @return bool |
| 36 | */ |
| 37 | public function mustRender( $file ) { |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Render files as PNG |
| 43 | * |
| 44 | * @param string $ext |
| 45 | * @param string $mime |
| 46 | * @param array|null $params |
| 47 | * @return array |
| 48 | */ |
| 49 | public function getThumbType( $ext, $mime, $params = null ) { |
| 50 | return [ 'png', 'image/png' ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get width and height from the bmp header. |
| 55 | * |
| 56 | * @param MediaHandlerState $state |
| 57 | * @param string $filename |
| 58 | * @return array |
| 59 | */ |
| 60 | public function getSizeAndMetadata( $state, $filename ) { |
| 61 | $f = fopen( $filename, 'rb' ); |
| 62 | if ( !$f ) { |
| 63 | return []; |
| 64 | } |
| 65 | $header = fread( $f, 54 ); |
| 66 | fclose( $f ); |
| 67 | |
| 68 | // Extract binary form of width and height from the header |
| 69 | $w = substr( $header, 18, 4 ); |
| 70 | $h = substr( $header, 22, 4 ); |
| 71 | |
| 72 | // Convert the unsigned long 32 bits (little endian): |
| 73 | try { |
| 74 | $w = StringUtils::unpack( 'V', $w, 4 ); |
| 75 | $h = StringUtils::unpack( 'V', $h, 4 ); |
| 76 | } catch ( UnpackFailedException $e ) { |
| 77 | return []; |
| 78 | } |
| 79 | |
| 80 | return [ |
| 81 | 'width' => $w[1], |
| 82 | 'height' => $h[1] |
| 83 | ]; |
| 84 | } |
| 85 | } |